/** * External dependencies */ import classNames from 'classnames'; import { CheckboxControl } from '@wordpress/components'; import { useCallback } from '@wordpress/element'; import { arrayDifferenceBy, arrayUnionBy } from '@woocommerce/utils'; import { decodeEntities } from '@wordpress/html-entities'; /** * Internal dependencies */ import type { renderItemArgs, SearchListItem as SearchListItemProps, } from './types'; import { getHighlightedName, getBreadcrumbsForDisplay } from './utils'; const Count = ( { label }: { label: string | React.ReactNode | number } ) => { return ( { label } ); }; const ItemLabel = ( props: { item: SearchListItemProps; search: string } ) => { const { item, search } = props; const hasBreadcrumbs = item.breadcrumbs && item.breadcrumbs.length; return ( { hasBreadcrumbs ? ( { getBreadcrumbsForDisplay( item.breadcrumbs ) } ) : null } { getHighlightedName( decodeEntities( item.name ), search ) } ); }; export const SearchListItem = ( { countLabel, className, depth = 0, controlId = '', item, isSelected, isSingle, onSelect, search = '', selected, useExpandedPanelId, ...props }: renderItemArgs ): JSX.Element => { const [ expandedPanelId, setExpandedPanelId ] = useExpandedPanelId; const showCount = countLabel !== undefined && countLabel !== null && item.count !== undefined && item.count !== null; const hasBreadcrumbs = !! item.breadcrumbs?.length; const hasChildren = !! item.children?.length; const isExpanded = expandedPanelId === item.id; const classes = classNames( [ 'woocommerce-search-list__item', `depth-${ depth }`, className ], { 'has-breadcrumbs': hasBreadcrumbs, 'has-children': hasChildren, 'has-count': showCount, 'is-expanded': isExpanded, 'is-radio-button': isSingle, } ); const name = props.name || `search-list-item-${ controlId }`; const id = `${ name }-${ item.id }`; const togglePanel = useCallback( () => { setExpandedPanelId( isExpanded ? -1 : Number( item.id ) ); }, [ isExpanded, item.id, setExpandedPanelId ] ); return hasChildren ? (