diff --git a/packages/core-data/src/components/FacetStateContextProvider.js b/packages/core-data/src/components/FacetStateContextProvider.js index fd0cc21f..3aae9bcc 100644 --- a/packages/core-data/src/components/FacetStateContextProvider.js +++ b/packages/core-data/src/components/FacetStateContextProvider.js @@ -1,6 +1,7 @@ // @flow import React, { + useCallback, useEffect, useMemo, useState, @@ -46,11 +47,21 @@ type Props = { */ children: Node, + /** + * A list of facets to exclude. + */ + exclude?: Array, + /** * Search host URI. */ host: string, + /** + * A list of facets to include. + */ + include?: Array, + /** * Search index name. */ @@ -86,19 +97,38 @@ const TYPE_INT_ARRAY = 'int64[]'; const FacetStateContextProvider = (props: Props) => { const [facets, setFacets] = useState>([]); + /** + * 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.