块编辑器开发文档

ComboboxControl

💡 云策文档标注

概述

ComboboxControl 是 SelectControl 的增强版本,增加了通过搜索输入框筛选选项的功能。它适用于选项过多无法一次性滚动或加载的场景,允许用户基于输入进行过滤。

关键要点

  • ComboboxControl 继承自 SelectControl,但支持搜索功能,适合处理大量选项。
  • 组件通过 onFilterValueChange 属性处理搜索输入变化,动态过滤 options 数组。
  • 提供多种属性如 label、value、onChange、isLoading 等,用于控制显示、状态和交互。
  • 相关组件包括 CustomSelectControl(无搜索)、RadioControl(单选显示所有选项)、CheckboxControl(多选)和 ToggleControl(开关)。

代码示例

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
                    )
                )
            }
        />
    );
}

注意事项

  • options 属性为必需,类型为数组,包含 value 和 label 字段,可选 disabled 字段。
  • onFilterValueChange 函数在搜索输入值变化时调用,用于更新过滤后的选项列表。
  • expandOnFocus 默认为 true,控制聚焦时是否自动展开下拉菜单。
  • __next40pxDefaultSize 属性用于启用未来版本将成为默认的更大高度。
  • __experimentalRenderItem 允许自定义选项渲染,但为实验性功能。

📄 原文内容

ComboboxControl is an enhanced version of a SelectControl, with the addition of being able to search for options using a search input.

Design guidelines

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.

Development guidelines

Usage

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
                    )
                )
            }
        />
    );
}

Props

label

The label for the control.

  • Type: String
  • Required: Yes

hideLabelFromVision

If true, the label will only be visible to screen readers.

  • Type: Boolean
  • Required: No

help

If this property is added, a help text will be generated using help property as the content.

  • Type: String
  • Required: No

options

The options that can be chosen from.

  • Type: Array<{ value: string, label: string, disabled?: boolean }>
  • Required: Yes

onFilterValueChange

Function called when the control’s search input value changes. The argument contains the next input value.

  • Type: ( value: string ) => void
  • Required: No

onChange

Function called with the selected value changes.

  • Type: ( value: string | null | undefined ) => void
  • Required: No

value

The current value of the control.

  • Type: string | null
  • Required: No

expandOnFocus

Automatically expand the dropdown when the control is focused.
If the control is clicked, the dropdown will expand regardless of this prop.

  • Type: Boolean
  • Required: No
  • Default: true

placeholder

If passed, the combobox input will show a placeholder string if no values are present.

  • Type: string
  • Required: No

isLoading

Show a spinner (and hide the suggestions dropdown) while data about the matching suggestions (ie the options prop) is loading

  • Type: Boolean
  • Required: No

__experimentalRenderItem

Custom 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).

  • Type: ( args: { item: object } ) => ReactNode
  • Required: No

__next40pxDefaultSize

Start opting into the larger default height that will become the default size in a future version.

  • Type: Boolean
  • Required: No
  • Default: false

Related components

  • Like 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.

  • To select one or more items from a set, use the CheckboxControl component.
  • To toggle a single setting on or off, use the ToggleControl component.