SelectControl allows users to select from a single or multiple option menu.
It functions as a wrapper around the browser’s native <select> element.
Import
import { SelectControl } from '@wordpress/components';
Examples
Default
const Default = ( props ) => {
const [ selection, setSelection ] = useState< string[] >();
if ( props.multiple ) {
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple
value={ selection }
onChange={ ( value ) => {
setSelection( value );
props.onChange?.( value );
} }
/>
);
}
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple={ false }
value={ selection?.[ 0 ] }
onChange={ ( value ) => {
setSelection( [ value ] );
props.onChange?.( value );
} }
/>
);
};
With Label And Help Text
const WithLabelAndHelpText = ( props ) => {
const [ selection, setSelection ] = useState< string[] >();
if ( props.multiple ) {
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple
value={ selection }
onChange={ ( value ) => {
setSelection( value );
props.onChange?.( value );
} }
/>
);
}
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple={ false }
value={ selection?.[ 0 ] }
onChange={ ( value ) => {
setSelection( [ value ] );
props.onChange?.( value );
} }
/>
);
};
With Custom Children
As an alternative to the options prop, optgroups and options can be passed in as children for more customizeability.
const WithCustomChildren = ( props ) => {
const [ selection, setSelection ] = useState< string[] >();
if ( props.multiple ) {
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple
value={ selection }
onChange={ ( value ) => {
setSelection( value );
props.onChange?.( value );
} }
/>
);
}
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple={ false }
value={ selection?.[ 0 ] }
onChange={ ( value ) => {
setSelection( [ value ] );
props.onChange?.( value );
} }
/>
);
};
With Prefix
By default, the prefix is aligned with the edge of the input border, with no padding. If you want to apply standard padding in accordance with the size variant, wrap the element in the <InputControlPrefixWrapper> component.
const WithPrefix = ( props ) => {
const [ selection, setSelection ] = useState< string[] >();
if ( props.multiple ) {
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple
value={ selection }
onChange={ ( value ) => {
setSelection( value );
props.onChange?.( value );
} }
/>
);
}
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple={ false }
value={ selection?.[ 0 ] }
onChange={ ( value ) => {
setSelection( [ value ] );
props.onChange?.( value );
} }
/>
);
};
Minimal
const Minimal = ( props ) => {
const [ selection, setSelection ] = useState< string[] >();
if ( props.multiple ) {
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple
value={ selection }
onChange={ ( value ) => {
setSelection( value );
props.onChange?.( value );
} }
/>
);
}
return (
<SelectControl
__next40pxDefaultSize
{ ...props }
multiple={ false }
value={ selection?.[ 0 ] }
onChange={ ( value ) => {
setSelection( [ value ] );
props.onChange?.( value );
} }
/>
);
};