Sort (Search Engine)
Sort (Search Engine)
|
|
Note
This component was introduced in version |
Example implementation
sort.fn.tsx
import {
buildCriterionExpression,
Sort as HeadlessSort,
SortCriterion,
} from '@coveo/headless';
import {useEffect, useState, FunctionComponent} from 'react';
interface SortProps {
controller: HeadlessSort;
criteria: [string, SortCriterion][];
}
export const Sort: FunctionComponent<SortProps> = (props) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(() => controller.subscribe(() => setState(controller.state)), []);
const getCriterionFromName = (name: string) =>
props.criteria.find(([criterionName]) => criterionName === name)!;
const getCurrentCriterion = () =>
props.criteria.find(
([, criterion]) =>
state.sortCriteria === buildCriterionExpression(criterion)
)!;
return (
<select
value={getCurrentCriterion()[0]}
onChange={(e) =>
controller.sortBy(getCriterionFromName(e.target.value)[1])
}
>
{props.criteria.map(([criterionName]) => (
<option key={criterionName} value={criterionName}>
{criterionName}
</option>
))}
</select>
);
};
// usage
/**
* ```tsx
* const criteria: [string, SortCriterion][] = [
* ['Relevance', buildRelevanceSortCriterion()],
* ['Date (Ascending)', buildDateSortCriterion(SortOrder.Ascending)],
* ['Date (Descending)', buildDateSortCriterion(SortOrder.Descending)],
* ['Size (Ascending)', buildFieldSortCriterion('size', SortOrder.Ascending)],
* ['Size (Descending)', buildFieldSortCriterion('size', SortOrder.Descending)],
* ];
* const initialCriterion = criteria[0][1];
* const controller = buildSort(engine, {
* initialState: {criterion: initialCriterion},
* });
*
* <Sort controller={controller} criteria={criteria} />;
* ```
*/
The Sort controller manages how the results are sorted.
Methods
isSortedBy
Verifies whether the specified sort criterion is the currently active one.
Parameters
-
criterion:
SortByRelevancy | SortByQRE | SortByDate | SortByField | SortByNoSortThe sort criterion to evaluate.
Returns boolean: true if the specified sort criterion is the currently active one; false otherwise.
sortBy
Updates the sort criterion and executes a new search.
Parameters
-
criterion:
SortByRelevancy | SortByQRE | SortByDate | SortByField | SortByNoSortThe new sort criterion.
subscribe
Adds a callback that’s invoked on state change.
Parameters
-
listener:
() => voidA callback that’s invoked on state change.
Returns Unsubscribe: A function to remove the listener.
Attributes
state
A scoped and simplified part of the headless state that is relevant to the Sort controller.
Properties
-
sortCriteria:
stringThe current sort criteria.
Initialize
buildSort
Creates a Sort controller instance.
Parameters
-
engine:
SearchEngineThe headless engine.
-
props:
SortPropsThe configurable
Sortcontroller properties.
Returns Sort
SortProps
The configurable Sort controller properties.
Properties
-
initialState?:
SortInitialStateThe initial state that should be applied to this
Sortcontroller.
SortInitialState
The initial state that should be applied to this Sort controller.
Properties
-
criterion?:
SortCriterion | SortCriterion[]The initial sort criterion to register in state.
Related types
Unsubscribe
Call signatures
-
(): void;