import React, { Children } from 'react'; import Icon from "../../common/Icon"; import { Cancel } from '../../../src/images/icons'; import { SearchFacetsGroupProps, SearchFacetsItemProps, SearchFacetsProps } from './types'; import clsx from 'clsx'; const FacetsItem = ({ accessibleName, joinContent, onClick, title, ...props }: SearchFacetsItemProps) => { accessibleName = accessibleName ? accessibleName : title; return (
{joinContent && } {title}
); } const FacetsGroup = ({ children, joinContent = 'or', title, ...props }: SearchFacetsGroupProps) => { return (
{title.trim()}:
{ Children.map(children, (child, index) => { const thisChild = child as React.ReactElement return React.cloneElement(thisChild, { joinContent: index > 0 ? joinContent : undefined, key: 'facet' + index }); }) }
) } const Facets = ({ children, className, ...props }: SearchFacetsProps) => { let facetCount = 0; function processChild(item: React.JSX.Element) { if (item.type.displayName === 'Facets.Item') { facetCount = facetCount + 1; } else if (item.type.displayName === 'Facets.Group') { Children.forEach(item.props.children, child => { processChild(child); }); } } Children.forEach(children, child => { processChild(child as React.JSX.Element); }); return (

There {facetCount === 1 ? 'is' : 'are'} {facetCount} search {facetCount === 1 ? 'filter' : 'filters'} applied

{children}
{facetCount > 0 && }
); } Facets.displayName = 'Facets'; FacetsItem.displayName = 'Facets.Item'; FacetsGroup.displayName = 'Facets.Group'; Facets.Item = FacetsItem; Facets.Group = FacetsGroup; export default Facets;