TreeSelect 是 WordPress 组件库中的一个 React 组件,用于生成层次化的选择输入控件,支持树形结构数据展示和交互。
import { useState } from 'react';
import { TreeSelect } from '@wordpress/components';
const MyTreeSelect = () => {
const [ page, setPage ] = useState( 'p21' );
return (
<TreeSelect
__next40pxDefaultSize
label="Parent page"
noOptionLabel="No parent page"
onChange={ ( newPage ) => setPage( newPage ) }
selectedId={ page }
tree={ [
{
name: 'Page 1',
id: 'p1',
children: [
{ name: 'Descend 1 of page 1', id: 'p11' },
{ name: 'Descend 2 of page 1', id: 'p12' },
],
},
{
name: 'Page 2',
id: 'p2',
children: [
{
name: 'Descend 1 of page 2',
id: 'p21',
children: [
{
name: 'Descend 1 of Descend 1 of page 2',
id: 'p211',
},
],
},
],
},
] }
/>
);
}See the WordPress Storybook for more detailed, interactive documentation.
Generates a hierarchical select input.
import { useState } from 'react';
import { TreeSelect } from '@wordpress/components';
const MyTreeSelect = () => {
const [ page, setPage ] = useState( 'p21' );
return (
<TreeSelect
__next40pxDefaultSize
label="Parent page"
noOptionLabel="No parent page"
onChange={ ( newPage ) => setPage( newPage ) }
selectedId={ page }
tree={ [
{
name: 'Page 1',
id: 'p1',
children: [
{ name: 'Descend 1 of page 1', id: 'p11' },
{ name: 'Descend 2 of page 1', id: 'p12' },
],
},
{
name: 'Page 2',
id: 'p2',
children: [
{
name: 'Descend 1 of page 2',
id: 'p21',
children: [
{
name: 'Descend 1 of Descend 1 of page 2',
id: 'p211',
},
],
},
],
},
] }
/>
);
}
__next40pxDefaultSizebooleanfalseStart opting into the larger default height that will become the default size in a future version.
childrenReactNodeAs an alternative to the options prop, optgroups and options can be
passed in as children for more customizability.
disabledbooleanfalseIf true, the input will be disabled.
hideLabelFromVisionbooleanfalseIf true, the label will only be visible to screen readers.
helpReactNodeAdditional description for the control.
Only use for meaningful description or instructions for the control. An element containing the description will be programmatically associated to the BaseControl by the means of an aria-describedby attribute.
labelReactNodeIf this property is added, a label will be generated using label property as the content.
labelPosition"top" | "bottom" | "side" | "edge"'top'The position of the label.
noOptionLabelstringIf this property is added, an option will be added with this label to represent empty selection.
onChange((value: string, extra?: { event?: ChangeEvent<HTMLSelectElement>; }) => void) | undefinedA function that receives the value of the new option that is being selected as input.
optionsreadonly ({ label: string; value: string; } & Omit<OptionHTMLAttributes<HTMLOptionElement>, "label" | "value">)[]An array of option property objects to be rendered,
each with a label and value property, as well as any other
<option> attributes.
prefixReactNodeRenders an element on the left side of the input.
By default, the prefix is aligned with the edge of the input border, with no padding.
If you want to apply standard padding in accordance with the size variant, wrap the element in
the provided <InputControlPrefixWrapper> component.
import {
__experimentalInputControl as InputControl,
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
} from '@wordpress/components';
<InputControl
prefix={<InputControlPrefixWrapper>@</InputControlPrefixWrapper>}
/>
selectedIdstringThe id of the currently selected node.
size"default" | "small" | "compact" | "__unstable-large"'default'Adjusts the size of the input.
suffixReactNodeRenders an element on the right side of the input.
By default, the suffix is aligned with the edge of the input border, with no padding.
If you want to apply standard padding in accordance with the size variant, wrap the element in
the provided <InputControlSuffixWrapper> component.
import {
__experimentalInputControl as InputControl,
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper,
} from '@wordpress/components';
<InputControl
suffix={<InputControlSuffixWrapper>%</InputControlSuffixWrapper>}
/>
treeTree[]An array containing the tree objects with the possible nodes the user can select.
variant"default" | "minimal"'default'The style variant of the control.