woocommerce/packages/js/components/src/experimental-select-control
Fernando Marichal 914348d887
Add clear button to `Categories` dropdown (#49036)
* Add clear button to Categories dropdown

* Add changelog

* Add component changelog

* Fix button styles

* Rename methods and prop
2024-07-04 13:35:57 -03:00
..
hooks Add async filtering support to the select-control component (#35839) 2022-12-07 14:24:32 -03:00
stories Upstream changes from experimental-select-control back to @woocommerce/components (#36521) 2023-03-29 17:18:43 +08:00
test Add async filtering support to the select-control component (#35839) 2022-12-07 14:24:32 -03:00
README.md Add core profiler user profile page (#38328) 2023-05-22 11:21:16 +08:00
combo-box.scss Template API: Conditional disabling support (#41307) 2023-11-14 13:17:34 -03:00
combo-box.tsx Show comma separated list in ready only mode of select tree control (#38052) 2023-05-08 04:17:52 -03:00
index.ts Add async filtering support to the select-control component (#35839) 2022-12-07 14:24:32 -03:00
menu-item.scss Components: Select Control dropdown styling issues (#39989) 2023-09-04 14:25:03 -03:00
menu-item.tsx [Grouped products] Product list (#41653) 2023-11-28 22:21:17 -03:00
menu.scss Fix when adding new attributes, input fields keep the focus styling (#40519) 2023-09-29 17:31:21 -03:00
menu.tsx Components: Select Control dropdown styling issues (#39989) 2023-09-04 14:25:03 -03:00
select-control.scss Show comma separated list in ready only mode of select tree control (#38052) 2023-05-08 04:17:52 -03:00
select-control.tsx Regenerate PNPM Lock File & Fix Errors (#41830) 2023-12-05 00:36:30 -08:00
selected-items.scss Use default cursor for everything in select and tree components (#49048) 2024-07-04 10:14:32 -03:00
selected-items.tsx Decode HTML escaped string for tree-item and selected-items components (#40047) 2023-09-11 09:41:17 -03:00
suffix-icon.scss Add async filtering support to the select-control component (#35839) 2022-12-07 14:24:32 -03:00
suffix-icon.tsx Add clear button to `Categories` dropdown (#49036) 2024-07-04 13:35:57 -03:00
types.ts Create experimental SelectTree component (#37319) 2023-03-24 14:49:45 -03:00
utils.ts Update/experimental select control (#34620) 2022-10-06 11:40:41 -03:00

README.md

SelectControl

A component that allows searching and selection of one or more items, providing accessibility for item and menu interaction.

Usage

SelectControl expects an array of item objects with value and label properties by default, with the option of passing your own ItemType to the component. Using the getItemLabel and getItemValue props will allow you to pass a function to determine the respective label and value used and customize this object's shape.

const [ selected, setSelected ] = useState< SelectedType >( [] );

const items = [
	{ value: 'item-1', label: 'Item 1' },
	{ value: 'item-2', label: 'Item 2' },
];

<SelectControl
    multiple
    items={ items }
    label="My select control"
    selected={ selected }
    onSelect={ ( item ) => item && setSelected( [ ...selected, item ] ) }
    onRemove={ () => setSelected( selected.filter( ( i ) => i !== item ) ) }
/>

And with a Custom Type to describe the item shape:

const [ selected, setSelected ] =
		useState< SelectedType< Array< CustomItemType > > >( null );

const customItems = [
  { id: 1, name: 'Joe', email: 'joe@notreally.com' },
  { id: 2, name: 'Jen' },
];

<SelectControl < CustomItemType >
  multiple
  items={ customItems }
  label="CustomItemType value"
  selected={ selected }
  onSelect={ ( item ) =>
    setSelected(
      Array.isArray( selected )
        ? [ ...selected, item ]
        : [ item ]
    )
  }
  onRemove={ ( item ) =>
    setSelected( selected.filter( ( i ) => i !== item ) )
  }
  getItemLabel={ ( item ) => item?.name }
  getItemValue={ ( item ) => String( item?.id ) }
/>

By default, the menu will render selectable items based on the provided items, but by passing a child function you can determine the render of those items.

<SelectControl
    label="Custom render"
    items={ items }
    selected={ selected }
    onSelect={ ( item ) => setSelectedItem( item ) }
    onRemove={ () => setSelectedItem( null ) }
>
    { ( {
        items,
        isOpen,
        highlightedIndex,
        getItemProps,
        getMenuProps,
        getItemLabel,
        getItemValue
    } ) => {
        return (
            <ul { ...getMenuProps() }>
                { isOpen && items.map( ( item, index: number ) => (
                    <li 
                        key={ `${ getItemValue(item) }${ index }` }
                        { ...getItemProps() }
                    >
                        { getItemLabel(item) }
                    </li>
                ) ) }
            </ul>
        );
    } }
</SelectControl>

Props

Name Type Default Description
children Function ( { items, highlightedIndex, getItemProps, getMenuProps, isOpen } ) => JSX.Element A function that renders the menu and menu items
multiple Boolean false Whether the input should allow multiple selections or a single selection
items Array undefined The items used in the dropdown as an array of objects with value and label properties
label String undefined A string shown above the input
getItemLabel Function ( item ) => item.label A function used to determine the label for an item
getItemValue Function ( item ) => item.value A function used to determine the value for an item
getFilteredItems Function ( allItems, inputValue, selectedItems ) => allItems.filter( ( item ) => selectedItems.indexOf( item ) < 0 && item.label.toLowerCase().startsWith( inputValue.toLowerCase() ) ) A function to determine how items should be filtered based on user input and previously selected items
onInputChange Function () => null A callback that fires when the user input has changed
onRemove Function () => null A callback that fires when a selected item has been removed
onSelect Function () => null A callback that fires when an item has been selected
selected Array or Item undefined An array of selected items or a single selected item\
onKeyDown Function () => null A callback that fires when a key is pressed
readOnlyWhenClosed Boolean false Whether the input should be read-only when the menu is closed