/** * External dependencies */ import classNames from 'classnames'; import { __, _n, sprintf } from '@wordpress/i18n'; import { SearchListControl, SearchListItem, } from '@woocommerce/editor-components/search-list-control'; import { SelectControl } from '@wordpress/components'; import { withInstanceId } from '@wordpress/compose'; import useProductAttributes from '@woocommerce/base-context/hooks/use-product-attributes'; import ErrorMessage from '@woocommerce/editor-components/error-placeholder/error-message'; import ExpandableSearchListItem from '@woocommerce/editor-components/expandable-search-list-item/expandable-search-list-item'; import { renderItemArgs, SearchListControlProps, SearchListItem as SearchListItemProps, } from '@woocommerce/editor-components/search-list-control/types'; import { convertAttributeObjectToSearchItem } from '@woocommerce/utils'; /** * Internal dependencies */ import './style.scss'; interface Props extends Omit< SearchListControlProps, 'isSingle' | 'list' | 'selected' > { instanceId?: string; /** * Callback to update the category operator. If not passed in, setting is not used. */ onOperatorChange?: () => void; /** * Setting for whether products should match all or any selected categories. */ operator: 'all' | 'any'; /** * The list of currently selected attribute ids. */ selected: { id: number }[]; } const ProductAttributeTermControl = ( { onChange, onOperatorChange, instanceId, isCompact = false, messages = {}, operator = 'any', selected, type = 'text', }: Props ) => { const { errorLoadingAttributes, isLoadingAttributes, productsAttributes } = useProductAttributes( true ); const renderItem = ( args: renderItemArgs ) => { const { item, search, depth = 0 } = args; const classes = [ 'woocommerce-product-attributes__item', 'woocommerce-search-list__item', { 'is-searching': search.length > 0, 'is-skip-level': depth === 0 && item.parent !== 0, }, ]; if ( ! item.breadcrumbs.length ) { return ( ); } const itemName = `${ item.breadcrumbs[ 0 ] }: ${ item.name }`; return ( ); }; const list = productsAttributes.reduce( ( acc, curr ) => { const { terms, ...props } = curr; return [ ...acc, convertAttributeObjectToSearchItem( props ), ...terms.map( convertAttributeObjectToSearchItem ), ]; }, [] as SearchListItemProps[] ); messages = { clear: __( 'Clear all product attributes', 'woo-gutenberg-products-block' ), noItems: __( "Your store doesn't have any product attributes.", 'woo-gutenberg-products-block' ), search: __( 'Search for product attributes', 'woo-gutenberg-products-block' ), selected: ( n: number ) => sprintf( /* translators: %d is the count of attributes selected. */ _n( '%d attribute selected', '%d attributes selected', n, 'woo-gutenberg-products-block' ), n ), updated: __( 'Product attribute search results updated.', 'woo-gutenberg-products-block' ), ...messages, }; if ( errorLoadingAttributes ) { return ; } return ( <> list.find( ( term ) => term.id === id ) ) .filter( Boolean ) as SearchListItemProps[] } type={ type } /> { !! onOperatorChange && ( ) } ); }; export default withInstanceId( ProductAttributeTermControl );