Back to Woocommerce

SelectControl

packages/js/components/src/select-control/README.md

10.8.0-dev6.3 KB
Original Source

SelectControl

A search box which filters options while typing, allowing a user to select from an option from a filtered list.

Usage

jsx
const options = [
	{
		key: 'apple',
		label: 'Apple',
		value: { id: 'apple' },
	},
	{
		key: 'apricot',
		label: 'Apricot',
		value: { id: 'apricot' },
	},
];

<SelectControl
	label="Single value"
	onChange={ ( selected ) => setState( { singleSelected: selected } ) }
	options={ options }
	placeholder="Start typing to filter options..."
	selected={ singleSelected }
/>;

Props

NameTypeDefaultDescription
classNamestringnullClass name applied to parent div
excludeSelectedOptionsbooleantrueExclude already selected options from the options list
onFilterfunctionidentityAdd or remove items to the list of options after filtering, passed the array of filtered options and should return an array of options.
getSearchExpressionfunctionidentityFunction to add regex expression to the filter the results, passed the search query
helpstring|nodenullHelp text to be appended beneath the input
inlineTagsbooleanfalseRender tags inside input, otherwise render below input
labelstringnullA label to use for the main input
onChangefunctionnoopFunction called when selected results change, passed result list
onSearchfunctionnoopFunction to run after the search query is updated, passed the search query
optionsarraynull(required) An array of objects for the options list. The option along with its key, label and value will be returned in the onChange event
placeholderstringnullA placeholder for the search input
selectedarray[]An array of objects describing selected values. If the label of the selected value is omitted, the Tag of that value will not be rendered inside the search box
maxResultsnumber0A limit for the number of results shown in the options menu. Set to 0 for no limit
multiplebooleanfalseAllow multiple option selections
showClearButtonbooleanfalseRender a 'Clear' button next to the input box to remove its contents
hideBeforeSearchbooleanfalseOnly show list options after typing a search query
staticListbooleanfalseRender results list positioned statically instead of absolutely
virtualScrollbooleanfalseEnable virtual scrolling for large lists
virtualItemHeightnumber35Height in pixels for each virtual item
virtualListHeightnumber300Maximum height in pixels for the virtualized list

onChange value

The onChange value defaults to an array of the selected option(s), but will also reflect what has been passed in the selected prop. If the selected prop has the value set as a string, the onChange method will also be called with a string value - the key of the selected option (if multiple is false).

Only string or array are the supported types here.

Virtualized Lists for Large Datasets

When dealing with a large number of options (thousands), you can enable virtualization for better performance. This feature uses the react-window library to render only the items that are visible in the viewport, significantly improving performance.

jsx
<SelectControl
	label="Select from large dataset"
	onChange={ handleSelect }
	options={ largeDataset } // Array with thousands of options
	placeholder="Start typing to search..."
	isSearchable={ true }
	virtualScroll={ true } // Enable virtualization
	virtualItemHeight={ 35 } // Height of each option in pixels
	virtualListHeight={ 300 } // Maximum height of the dropdown
/>