in packages/search-ui/src/actions/addFilter.ts [15:54]
export default function addFilter(
name: string,
value: FilterValue,
type: FilterType = "all",
persistent?: boolean
): void {
// eslint-disable-next-line no-console
if (this.debug) console.log("Search UI: Action", "addFilter", ...arguments);
const { filters } = this.state as RequestState;
const existingFilter =
filters.find((f) => f.field === name && f.type === type) || null;
const allOtherFilters =
filters.filter((f) => f.field !== name || f.type !== type) || [];
const existingFilterValues = existingFilter?.values || [];
const newFilterValues = existingFilterValues.find((existing) =>
doFilterValuesMatch(existing, value)
)
? existingFilterValues
: existingFilterValues.concat(value);
this._updateSearchResults({
current: 1,
filters: [
...allOtherFilters,
{ field: name, values: newFilterValues, type, persistent }
]
});
const events: Events = this.events;
events.emit({
type: "FacetFilterSelected",
field: name,
value: serialiseFilter(newFilterValues),
query: this.state.searchTerm
});
}