ComboboxControl 是 SelectControl 的增强版本,增加了通过搜索输入框筛选选项的功能。它适用于选项过多无法一次性滚动或加载的场景,允许用户基于输入进行过滤。
import { useState } from 'react';
import { ComboboxControl } from '@wordpress/components';
const options = [
{
value: 'small',
label: 'Small',
},
{
value: 'normal',
label: 'Normal',
},
{
value: 'large',
label: 'Large',
},
];
function MyComboboxControl() {
const [ fontSize, setFontSize ] = useState();
const [ filteredOptions, setFilteredOptions ] = useState( options );
return (
<ComboboxControl
__next40pxDefaultSize
label="Font Size"
value={ fontSize }
onChange={ setFontSize }
isLoading={ isLoading }
options={ filteredOptions }
onFilterValueChange={ ( inputValue ) =>
setFilteredOptions(
options.filter( ( option ) =>
option.value === inputValue
)
)
}
/>
);
}ComboboxControl is an enhanced version of a SelectControl, with the addition of being able to search for options using a search input.
These are the same as the ones for SelectControls, but this component is better suited for when there are too many items to scroll through or load at once so you need to filter them based on user input.
import { useState } from 'react';
import { ComboboxControl } from '@wordpress/components';
const options = [
{
value: 'small',
label: 'Small',
},
{
value: 'normal',
label: 'Normal',
},
{
value: 'large',
label: 'Large',
},
];
function MyComboboxControl() {
const [ fontSize, setFontSize ] = useState();
const [ filteredOptions, setFilteredOptions ] = useState( options );
return (
<ComboboxControl
__next40pxDefaultSize
label="Font Size"
value={ fontSize }
onChange={ setFontSize }
isLoading={ isLoading }
options={ filteredOptions }
onFilterValueChange={ ( inputValue ) =>
setFilteredOptions(
options.filter( ( option ) =>
option.value === inputValue
)
)
}
/>
);
}
The label for the control.
StringIf true, the label will only be visible to screen readers.
BooleanIf this property is added, a help text will be generated using help property as the content.
StringThe options that can be chosen from.
Array<{ value: string, label: string, disabled?: boolean }>Function called when the control’s search input value changes. The argument contains the next input value.
( value: string ) => voidFunction called with the selected value changes.
( value: string | null | undefined ) => voidThe current value of the control.
string | nullAutomatically expand the dropdown when the control is focused.
If the control is clicked, the dropdown will expand regardless of this prop.
BooleantrueIf passed, the combobox input will show a placeholder string if no values are present.
stringShow a spinner (and hide the suggestions dropdown) while data about the matching suggestions (ie the options prop) is loading
BooleanCustom renderer invoked for each option in the suggestion list. The render prop receives as its argument an object containing, under the item key, the single option’s data (directly from the array of data passed to the options prop).
( args: { item: object } ) => ReactNodeStart opting into the larger default height that will become the default size in a future version.
BooleanfalseLike this component, but without a search input, the CustomSelectControl component.
To select one option from a set, when you want to show all the available options at once, use the RadioControl component.
CheckboxControl component.ToggleControl component.