TreeSelect

Generates a hierarchical select input.

import { TreeSelect } from '@wordpress/components';

View on Storybook

View source on GitHub

PropsPermalink to this section

NameDefaultDescription
noOptionLabel

string

If this property is added, an option will be added with this label to represent empty selection.

onChange

(value: string, extra?: { event?: ChangeEvent<HTMLSelectElement> | undefined; } | undefined) => void

A function that receives the value of the new option that is being selected as input.

tree[]

Tree[]

An array containing the tree objects with the possible nodes the user can select.

selectedId

string

The id of the currently selected node.

label

ReactNode

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

children

ReactNode

As an alternative to the options prop, optgroups and options can be passed in as children for more customizability.

disabledfalse

boolean

If true, the input will be disabled.

prefix

ReactNode

Renders 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 `} />

__next40pxDefaultSize

boolean

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

size'default'

"small" | "default" | "compact"

Adjusts the size of the input.

variant'default'

"default" | "minimal"

The style variant of the control.

suffix

ReactNode

Renders 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 `} />

help

ReactNode

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

__nextHasNoMarginBottom

boolean

Start opting into the new margin-free styles that will become the default in a future version.

hideLabelFromVisionfalse

boolean

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

__next36pxDefaultSizefalse

boolean

Deprecated. Use __next40pxDefaultSize instead.

options

readonly ({ 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.

labelPosition'top'

"top" | "bottom" | "side" | "edge"

The position of the label.

ExamplesPermalink to this section

DefaultPermalink to this section

const Default = ( props ) => {
	const [ selection, setSelection ] =
		useState< ComponentProps< typeof TreeSelect >[ 'selectedId' ] >();

	return (
		<TreeSelect
			{ ...props }
			onChange={ setSelection }
			selectedId={ selection }
		/>
	);
};