Skip to content

Commit

Permalink
Merge pull request #315 from performant-software/feature/cdp23_facets…
Browse files Browse the repository at this point in the history
…_show_more

CDP #23 - Facets show more
  • Loading branch information
dleadbetter authored Jan 9, 2025
2 parents eb7655b + 952ddc5 commit fb30589
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/core-data/src/components/FacetListsGrouped.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ const FacetListsGrouped = (props: Props) => {
const label = props.resolveLabel(id);

let group = _.findWhere(value, { id });

// If we cannot find a value by ID, lookup the value by label
if (!group) {
group = _.findWhere(value, { label });
}

// If we cannot find a value by ID or label, create new group
if (!group) {
group = { id, label, items: [] };
value.push(group);
Expand Down
38 changes: 34 additions & 4 deletions packages/core-data/src/components/FacetStateContextProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import React, {
useCallback,
useEffect,
useMemo,
useState,
Expand Down Expand Up @@ -46,11 +47,21 @@ type Props = {
*/
children: Node,

/**
* A list of facets to exclude.
*/
exclude?: Array<string>,

/**
* Search host URI.
*/
host: string,

/**
* A list of facets to include.
*/
include?: Array<string>,

/**
* Search index name.
*/
Expand Down Expand Up @@ -86,19 +97,38 @@ const TYPE_INT_ARRAY = 'int64[]';
const FacetStateContextProvider = (props: Props) => {
const [facets, setFacets] = useState<Array<string>>([]);

/**
* Returns true if the passed field should be included as a facet.
*
* @type {(function(*): (*|boolean))|*}
*/
const isValid = useCallback((field) => {
if (!_.isEmpty(props.include)) {
return _.contains(props.include, field.name);
}

if (!_.isEmpty(props.exclude)) {
return !_.contains(props.exclude, field.name);
}

return true;
}, [props.exclude, props.include]);

/**
* Memo-ize the refinement list facets.
*/
const listFacets = useMemo(() => (
_.filter(facets, (field: any) => field.facet && field.type !== TYPE_AUTO && field.type !== TYPE_INT_ARRAY)
), [facets]);
_.filter(facets, (field: any) => isValid(field)
&& field.facet && field.type !== TYPE_AUTO
&& field.type !== TYPE_INT_ARRAY)
), [facets, isValid]);

/**
* Memo-ize the range facets.
*/
const rangeFacets = useMemo(() => (
_.filter(facets, (field: any) => field.facet && field.type === TYPE_INT_ARRAY)
), [facets]);
_.filter(facets, (field: any) => isValid(field) && field.facet && field.type === TYPE_INT_ARRAY)
), [facets, isValid]);

/**
* Backwards compatability for consumers using the "attributes" return value.
Expand Down

0 comments on commit fb30589

Please sign in to comment.