CustomSelectControl

View on Storybook

View source on GitHub

Import

import { CustomSelectControl } from '@wordpress/components';

Props

NameTypeDefaultRequiredDescription
classNamestring

Optional classname for the component.

hideLabelFromVisionboolean

Hide the label visually, while keeping available to assistive technology.

describedBystring

Description for the select trigger button used by assistive technology. If no value is passed, the text “Currently selected: selectedItem.name” will be used fully translated.

labelstringYes

Label for the control.

onChange( newValue: CustomSelectChangeObject< NoInfer< T > > ) => void

Function called with the control’s internal state changes. The selectedItem property contains the next selected item.

optionsReadonlyArray< T >Yes

The list of options that can be chosen from.

size'default' | 'small' | '__unstable-large'

The size of the control.

@default ‘default’

valueNoInfer< T >

Can be used to externally control the value of the control.

showSelectedHintboolean

Show the hint of the selected item in the trigger button.

@default false

__next40pxDefaultSizeboolean

Start opting into the larger default height that will become the default size in a future version.

@default false

Examples

Default

const Default = ( props ) => {
	const [ value, setValue ] = useState( props.options[ 0 ] );

	const onChange: React.ComponentProps<
		typeof CustomSelectControl
	>[ 'onChange' ] = ( changeObject ) => {
		setValue( changeObject.selectedItem );
		props.onChange?.( changeObject );
	};

	return (
		<CustomSelectControl
			__next40pxDefaultSize
			{ ...props }
			onChange={ onChange }
			value={ value }
		/>
	);
};

With Long Labels

const WithLongLabels = ( props ) => {
	const [ value, setValue ] = useState( props.options[ 0 ] );

	const onChange: React.ComponentProps<
		typeof CustomSelectControl
	>[ 'onChange' ] = ( changeObject ) => {
		setValue( changeObject.selectedItem );
		props.onChange?.( changeObject );
	};

	return (
		<CustomSelectControl
			__next40pxDefaultSize
			{ ...props }
			onChange={ onChange }
			value={ value }
		/>
	);
};

With Hints

const WithHints = ( props ) => {
	const [ value, setValue ] = useState( props.options[ 0 ] );

	const onChange: React.ComponentProps<
		typeof CustomSelectControl
	>[ 'onChange' ] = ( changeObject ) => {
		setValue( changeObject.selectedItem );
		props.onChange?.( changeObject );
	};

	return (
		<CustomSelectControl
			__next40pxDefaultSize
			{ ...props }
			onChange={ onChange }
			value={ value }
		/>
	);
};