TreeSelect is a form component to choose from hierarchical data.
import { TreeSelect } from 'primereact/treeselect';
TreeSelect is used as a controlled component with value and onChange properties along with an options collection. Internally Tree component is used so the options model is based on TreeNode API.
In single selection mode, value binding should be the key value of a node.
<TreeSelect value={selectedNodeKey} onChange={(e) => setSelectedNodeKey(e.value)} options={nodes}
className="md:w-20rem w-full" placeholder="Select Item"></TreeSelect>
More than one node is selectable by setting selectionMode to multiple. By default in multiple selection mode, metaKey press (e.g. ⌘) is necessary to add to existing selections however this can be configured with disabling the metaKeySelection property. Note that in touch enabled devices, TreeSelect always ignores metaKey.
In multiple selection mode, value binding should be a key-value pair where key is the node key and value is a boolean to indicate selection.
{
'0-0': true,
'0-1-0': true
}
<TreeSelect value={selectedNodeKeys} onChange={(e) => setSelectedNodeKeys(e.value)} options={nodes} metaKeySelection={false}
className="md:w-20rem w-full" selectionMode="multiple" placeholder="Select Items"></TreeSelect>
Selection of multiple nodes via checkboxes is enabled by configuring selectionMode as checkbox.
In checkbox selection mode, value binding should be a key-value pair where key is the node key and value is an object that has checked and partialChecked properties to represent the checked state of a node obje to indicate selection.
{
'0-0': {
partialChecked: false,
checked: true
}
}
<TreeSelect value={selectedNodeKeys} onChange={(e) => setSelectedNodeKeys(e.value)} options={nodes} metaKeySelection={false}
className="md:w-20rem w-full" selectionMode="checkbox" display="chip" placeholder="Select Items"></TreeSelect>
Filtering is enabled by adding the filter property, by default label property of a node is used to compare against the value in the text field, in order to customize which field(s) should be used during search define filterBy property. In addition filterMode specifies the filtering strategy. In lenient mode when the query matches a node, children of the node are not searched further as all descendants of the node are included. On the other hand, in strict mode when the query matches a node, filtering continues on all descendants.
<TreeSelect value={selectedNodeKey} onChange={(e) => setSelectedNodeKey(e.value)} options={nodes}
filter className="md:w-20rem w-full" placeholder="Select Item"></TreeSelect>
Expanded state of nodes is managed programmatically with expandedKeys and onToggle properties.
<TreeSelect value={selectedNodeKey} onChange={(e) => setSelectedNodeKey(e.value)} options={nodes}
className="md:w-20rem w-full" placeholder="Select Item"
expandedKeys={expandedKeys} onToggle={(e) => setExpandedKeys(e.value)} panelHeaderTemplate={headerTemplate}></TreeSelect>
A floating label appears on top of the input field when focused.
<span className="p-float-label">
<TreeSelect inputId="treeselect" value={selectedNodeKey} onChange={(e) => setSelectedNodeKey(e.value)} options={nodes}
className="md:w-20rem w-full"></TreeSelect>
<label htmlFor="treeselect">TreeSelect</label>
</span>
Invalid state style is added using the p-invalid class to indicate a failed validation.
<TreeSelect value={selectedNodeKey} onChange={(e) => setSelectedNodeKey(e.value)} options={nodes}
className="md:w-20rem w-full p-invalid" placeholder="Select Item"></TreeSelect>
When disabled is present, the element cannot be edited and focused.
<TreeSelect disabled placeholder="Select Item" className="md:w-20rem w-full" />
Compatibility with popular React form libraries.
Formik is a popular library for handling forms in React.
<Toast ref={toast} />
<TreeSelect
id="item"
name="item"
value={formik.values.item}
options={node}
placeholder="Select Item"
className={classNames('w-full md:w-20rem', { 'p-invalid': isFormFieldInvalid('item') })}
onChange={(e) => {
formik.setFieldValue('item', e.value);
}}
/>
{getFormErrorMessage('item')}
<Button type="submit" label="Submit" />
React Hook Form is another popular React library to handle forms.
<Toast ref={toast} />
<Controller
name="value"
control={form.control}
rules={{ required: 'Value is required.' }}
render={({ field, fieldState }) => (
<>
<TreeSelect id={field.name} value={field.value} onChange={field.onChange} inputRef={field.ref} options={nodes} placeholder="Select Item" className={classNames('w-full md:w-20rem', { 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
/>
<Button label="Submit" type="submit" icon="pi pi-check" />
Following is the list of structural style classes
Name | Element |
---|---|
p-treeselect | Container element. |
p-treeselect-label-container | Container of the label to display selected items. |
p-treeselect-label | Label to display selected items. |
p-treeselect-trigger | Dropdown button. |
p-treeselect-panel | Overlay panel for items. |
p-treeselect-items-wrapper | List container of items. |
Value to describe the component can either be provided with aria-labelledby or aria-label props. The treeselect element has a combobox role in addition to aria-haspopup and aria-expanded attributes. The relation between the combobox and the popup is created with aria-controls that refers to the id of the popup.
The popup list has an id that refers to the aria-controls attribute of the combobox element and uses tree as the role. Each list item has a treeitem role along with aria-label, aria-selected and aria-expanded attributes. In checkbox selection, aria-checked is used instead of aria-selected. Checkbox and toggle icons are hidden from screen readers as their parent element with treeitem role and attributes are used instead for readers and keyboard support. The container element of a treenode has the group role. The aria-setsize, aria-posinset and aria-level attributes are calculated implicitly and added to each treeitem.
If filtering is enabled, filterInputProps can be defined to give aria-* props to the filter input element.
<span id="dd1">Options</span>
<TreeSelect aria-labelledby="dd1" />
<TreeSelect aria-label="Options" />
Key | Function |
---|---|
tab | Moves focus to the treeselect element. |
space | Opens the popup and moves visual focus to the selected treenode, if there is none then first treenode receives the focus. |
down arrow | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
Key | Function |
---|---|
tab | Moves focus to the next focusable element in the popup, if there is none then first focusable element receives the focus. |
shift + tab | Moves focus to the previous focusable element in the popup, if there is none then last focusable element receives the focus. |
enter | Selects the focused option, closes the popup if selection mode is single. |
space | Selects the focused option, closes the popup if selection mode is single. |
escape | Closes the popup, moves focus to the treeselect element. |
down arrow | Moves focus to the next treenode. |
up arrow | Moves focus to the previous treenode. |
right arrow | If node is closed, opens the node otherwise moves focus to the first child node. |
left arrow | If node is open, closes the node otherwise moves focus to the parent node. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the treeselect element. |
escape | Closes the popup and moves focus to the treeselect element. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the treeselect element. |
space | Closes the popup and moves focus to the treeselect element. |
escape | Closes the popup and moves focus to the treeselect element. |