AutomaticFacetGenerator

This is for:

Developer

Example Implementation

automatic-facet-generator.fn.tsx

import {AutomaticFacetGenerator as HeadlessAutomaticFacetGenerator} from '@coveo/headless';
import {FunctionComponent, useEffect, useState} from 'react';
import {AutomaticFacet as AutomaticFacetFn} from '../automatic-facet/automatic-facet.fn';
 
interface AutomaticFacetGeneratorProps {
  controller: HeadlessAutomaticFacetGenerator;
}
 
export const AutomaticFacetGenerator: FunctionComponent<
  AutomaticFacetGeneratorProps
> = (props) => {
  const {controller} = props;
  const [state, setState] = useState(controller.state);
 
  useEffect(() => controller.subscribe(() => setState(controller.state)), []);
 
  const automaticFacets = state.automaticFacets.map((facet) => {
    return (
      <AutomaticFacetFn
        key={facet.state.field}
        controller={facet}
      ></AutomaticFacetFn>
    );
  });
  return <div>{automaticFacets}</div>;
};
 
// usage
 
/**
 * ```tsx
 * const props: AutomaticFacetGeneratorProps = {desiredCount: 5}
 * const controller = buildAutomaticFacetGenerator(engine, props);
 *
 * <AutomaticFacetGenerator controller={controller} />;
 * ```
 */

automatic-facet.fn.tsx

import {AutomaticFacet as HeadlessAutomaticFacet} from '@coveo/headless';
import {FunctionComponent, useEffect, useState} from 'react';
 
interface AutomaticFacetProps {
  controller: HeadlessAutomaticFacet;
}
 
export const AutomaticFacet: FunctionComponent<AutomaticFacetProps> = (
  props
) => {
  const {controller} = props;
  const [state, setState] = useState(controller.state);
 
  useEffect(() => controller.subscribe(() => setState(controller.state)), []);
 
  return (
    <ul>
      {state.values.map((value) => (
        <li key={value.value}>
          <input
            type="checkbox"
            checked={value.state === 'selected'}
            onChange={() => controller.toggleSelect(value)}
          />
          {value.value} ({value.numberOfResults} results)
        </li>
      ))}
    </ul>
  );
};

The AutomaticFacetGenerator headless controller offers a high-level interface for rendering automatic facets.

Methods

subscribe

Adds a callback that’s invoked on state change.

Parameters

  • listener: () => void

    A callback that’s invoked on state change.

Returns Unsubscribe: A function to remove the listener.

Attributes

state

The state of the AutomaticFacetGenerator controller.

Properties

  • automaticFacets: AutomaticFacet[]

    • This property is part of the automatic facets feature. Automatic facets are currently in beta testing and should be available soon.

      The list of automatic facet controllers.

Initialize

buildAutomaticFacetGenerator

  • This function is part of the automatic facets feature. Automatic facets are currently in beta testing and should be available soon.

Creates a AutomaticFacetGenerator instance.

Parameters

  • engine: SearchEngine

    The headless engine.

  • props: AutomaticFacetGeneratorProps

    The automatic facets props. Automatic facets are currently in beta testing and should be available soon.

Returns AutomaticFacetGenerator

AutomaticFacetGeneratorProps

The automatic facets props. Automatic facets are currently in beta testing and should be available soon.

Properties

  • desiredCount: number

    • This property is part of the automatic facets feature. Automatic facets are currently in beta testing and should be available soon.

      The desired count of automatic facets. Must be a positive integer.

AutomaticFacet

Properties

  • state: AutomaticFacetState

    The state of the AutomaticFacet controller.

  • deselectAll: function

    Deselects all automatic facet values.

  • toggleSelect: function

    Toggles the specified automatic facet value.

    Parameters

    • selection: FacetValue

      The facet value to toggle.

  • subscribe: function

    Adds a callback that’s invoked on state change.

    Parameters

    • listener: () ⇒ void

      A callback that’s invoked on state change.

      Returns Unsubscribe: A function to remove the listener.

AutomaticFacetState

Properties

  • field: string

    The automatic facet field.

  • label: string

    The automatic facet label.

  • values: FacetValue[]

    The automatic facet values.

FacetValue

Properties

  • numberOfResults: number

    The number of results that have the facet value.

  • state: 'idle' | 'selected' | 'excluded'

    Whether a facet value is filtering results (selected) or not (idle).

  • value: string

    The facet value.

Unsubscribe

Call signatures

  • (): void