CustomSelectControl


目录

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.

Design guidelines

These are the same as the ones for SelectControls.

Development guidelines

Usage

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
            label="Font Size"
            options={ options }
            onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
        />
    );
}

function MyControlledCustomSelectControl() {
    const [ fontSize, setFontSize ] = useState( options[ 0 ] );
    return (
        <CustomSelectControl
            label="Font Size"
            options={ options }
            onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) }
            value={ options.find( ( option ) => option.key === fontSize.key ) }
        />
    );
}

Props

className: string

Optional classname for the component.

hideLabelFromVision: boolean

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

describedBy: string

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.

label: string

Label for the control.

onChange: ( newValue: ChangeObject ) => void

Function 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.

Options can provide custom styles through style and className. Hovered and keyboard-active options use the design system’s highlight colors so text stays readable. Custom text and background colors apply while the option is inactive; other custom styling remains in effect.

size: 'default' | 'small'

The size of the control.

showSelectedHint: boolean

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

value: Option

Can 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.


版本号 #2
由 壳壳虫 创建于 2026-10-31 18:12:40 UTC
由 壳壳虫 更新于 2026-10-31 18:12:41 UTC