@wordpress/components
Version:
UI components for WordPress.
160 lines (106 loc) • 4.35 kB
Markdown
Used to render a customizable select control component.
`CustomSelectControlV2` can be used in an uncontrolled mode, where the component manages its own state. In this mode, the `defaultValue` prop can be used to set the initial selected value. If this prop is not set, the first value from the children will be selected by default.
```jsx
const UncontrolledCustomSelectControlV2 = () => (
<CustomSelectControlV2 label="Colors">
<CustomSelectControlV2.Item value="Blue">
{ /* The `defaultValue` since it wasn't defined */ }
<span style={ { color: 'blue' } }>Blue</span>
</CustomSelectControlV2.Item>
<CustomSelectControlV2.Item value="Purple">
<span style={ { color: 'purple' } }>Purple</span>
</CustomSelectControlV2.Item>
<CustomSelectControlV2.Item value="Pink">
<span style={ { color: 'deeppink' } }>Pink</span>
</CustomSelectControlV2.Item>
</CustomSelectControlV2>
);
```
`CustomSelectControlV2` can also be used in a controlled mode, where the parent component specifies the `value` and the `onChange` props to control selection.
```jsx
const ControlledCustomSelectControlV2 = () => {
const [ value, setValue ] = useState< string | string[] >();
const renderControlledValue = ( renderValue: string | string[] ) => (
<>
{ /* Custom JSX to display `renderValue` item */ }
</>
);
return (
<CustomSelectControlV2
{ ...props }
onChange={ ( nextValue ) => {
setValue( nextValue );
props.onChange?.( nextValue );
} }
value={ value }
>
{ [ 'blue', 'purple', 'pink' ].map( ( option ) => (
<CustomSelectControlV2.Item key={ option } value={ option }>
{ renderControlledValue( option ) }
</CustomSelectControlV2.Item>
) ) }
</CustomSelectControlV2>
);
};
```
Multiple selection can be enabled by using an array for the `value` and
`defaultValue` props. The argument of the `onChange` function will also change accordingly.
```jsx
const MultiSelectCustomSelectControlV2 = () => (
<CustomSelectControlV2 defaultValue={ [ 'blue', 'pink' ] } label="Colors">
{ [ 'blue', 'purple', 'pink' ].map( ( item ) => (
<CustomSelectControlV2.Item key={ item } value={ item }>
{ item }
</CustomSelectControlV2.Item>
) ) }
</CustomSelectControlV2>
);
```
`CustomSelectControlV2` is comprised of two individual components:
- `CustomSelectControlV2`: a wrapper component and context provider. It is responsible for managing the state of the `CustomSelectControlV2.Item` children.
- `CustomSelectControlV2.Item`: renders a single select item. The first `CustomSelectControlV2.Item` child will be used as the `defaultValue` when `defaultValue` is undefined.
The component accepts the following props:
The child elements. This should be composed of `CustomSelectControlV2.Item` components.
- Required: yes
An optional default value for the control. If left `undefined`, the first non-disabled item will be used.
- Required: no
Used to visually hide the label. It will always be visible to screen readers.
- Required: no
- Default: `false`
Label for the control.
- Required: yes
A function that receives the new value of the input.
- Required: no
Can be used to render select UI with custom styled values.
- Required: no
The size of the control.
- Required: no
- Default: `'default'`
Can be used to externally control the value of the control.
- Required: no
Used to render a select item.
The component accepts the following props:
The value of the select item. This will be used as the children if children are left `undefined`.
- Required: yes
The children to display for each select item. The `value` will be used if left `undefined`.
- Required: no