CustomSelectControl 是一个 WordPress 组件,允许用户从单选项菜单中选择项目,类似于 SelectControl,但支持为菜单中的每个项目提供自定义样式。由于不使用原生
import { useState } from 'react';
import { CustomSelectControl } from '@wordpress/components';
const options = [
{
key: 'small',
name: 'Small',
style: { fontSize: '50%' },
},
{
key: 'normal',
name: 'Normal',
style: { fontSize: '100%' },
},
{
key: 'large',
name: 'Large',
style: { fontSize: '200%' },
},
{
key: 'huge',
name: 'Huge',
style: { fontSize: '300%' },
},
];
function MyCustomSelectControl() {
const [ , setFontSize ] = useState();
return (
<CustomSelectControl
__next40pxDefaultSize
label="Font Size"
options={ options }
onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
/>
);
}
function MyControlledCustomSelectControl() {
const [ fontSize, setFontSize ] = useState( options[ 0 ] );
return (
<CustomSelectControl
__next40pxDefaultSize
label="Font Size"
options={ options }
onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
value={ options.find( ( option ) => option.key === fontSize.key ) }
/>
);
}CustomSelectControl allows users to select an item from a single-option menu just like SelectControl, with the addition of being able to provide custom styles for each item in the menu. This means it does not use a native <select>, so should only be used if the custom styling is necessary.
These are the same as the ones for SelectControls.
import { useState } from 'react';
import { CustomSelectControl } from '@wordpress/components';
const options = [
{
key: 'small',
name: 'Small',
style: { fontSize: '50%' },
},
{
key: 'normal',
name: 'Normal',
style: { fontSize: '100%' },
},
{
key: 'large',
name: 'Large',
style: { fontSize: '200%' },
},
{
key: 'huge',
name: 'Huge',
style: { fontSize: '300%' },
},
];
function MyCustomSelectControl() {
const [ , setFontSize ] = useState();
return (
<CustomSelectControl
__next40pxDefaultSize
label="Font Size"
options={ options }
onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
/>
);
}
function MyControlledCustomSelectControl() {
const [ fontSize, setFontSize ] = useState( options[ 0 ] );
return (
<CustomSelectControl
__next40pxDefaultSize
label="Font Size"
options={ options }
onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
value={ options.find( ( option ) => option.key === fontSize.key ) }
/>
);
}
className: stringOptional classname for the component.
hideLabelFromVision: booleanHide the label visually, while keeping available to assistive technology.
describedBy: stringDescription 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.
label: stringLabel for the control.
onChange: ( newValue: ChangeObject ) => voidFunction called with the control’s internal state changes. The selectedItem property contains the next selected item.
options: Array< Option >The list of options that can be chosen from.
size: 'default' | 'small' | '__unstable-large'The size of the control.
'default'showSelectedHint: booleanShow the hint of the selected item in the trigger button.
value: OptionCan be used to externally control the value of the control, like in the MyControlledCustomSelectControl example above.
onMouseOver: MouseEventHandler< HTMLButtonElement >A handler for mouseover events on the trigger button.
onMouseOut: MouseEventHandler< HTMLButtonElement >A handler for mouseout events on the trigger button.
onFocus: FocusEventHandler< HTMLButtonElement >A handler for focus events on the trigger button.
onBlur: FocusEventHandler< HTMLButtonElement >A handler for blur events on the trigger button.
__next40pxDefaultSize: booleanStart opting into the larger default height that will become the default size in a future version.
falseLike this component, but implemented using a native <select> for when custom styling is not necessary, the SelectControl component.
To select one option from a set, when you want to show all the available options at once, use the Radio component.
CheckboxControl component.To toggle a single setting on or off, use the ToggleControl component.
If you have a lot of items, ComboboxControl might be a better fit.