Tree View
A tree view widget presents a hierarchical list.
Tree views can be used to represent a file system navigator displaying folders and files, an item representing a folder can be expanded to reveal the contents of the folder, which may be files, folders, or both.
- Applications
- Documents
- Applications
- Documents
- Applications
- Documents
Rich object
While the TreeView
/TreeItem
component API maximizes flexibility, an extra step is needed to handle a rich object.
Let's consider a data variable with the following shape, recursion can be used to handle it.
<span class="token keyword">const</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token string">'root'</span><span class="token punctuation">,</span>
<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Parent'</span><span class="token punctuation">,</span>
<span class="token literal-property property">children</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span>
<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Child - 1'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment">// …</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
- Child - 1
- Child - 3
ContentComponent prop
You can use the ContentComponent
prop and the useTreeItem
hook to further customize the behavior of the TreeItem.
Such as limiting expansion to clicking the icon:
- Applications
- Documents
Or increasing the width of the state indicator:
- Applications
- Documents
- Hello
- Subtree with children
- World
- Something something
All Mail
Trash
Social
Updates
Forums
Promotions
History
- One
- Five
- Seven
The behavior of disabled tree items depends on the disabledItemsFocusable
prop.
If it is false:
- Arrow keys will not focus disabled items and, the next non-disabled item will be focused.
- Typing the first character of a disabled item's label will not focus the item.
- Mouse or keyboard interaction will not expand/collapse disabled items.
- Mouse or keyboard interaction will not select disabled items.
- Shift + arrow keys will skip disabled items and, the next non-disabled item will be selected.
- Programmatic focus will not focus disabled items.
If it is true:
- Arrow keys will focus disabled items.
- Typing the first character of a disabled item's label will focus the item.
- Mouse or keyboard interaction will not expand/collapse disabled items.
- Mouse or keyboard interaction will not select disabled items.
- Shift + arrow keys will not skip disabled items but, the disabled item will not be selected.
- Programmatic focus will focus disabled items.
Accessibility
(WAI-ARIA: https://www.w3.org/WAI/ARIA/apg/patterns/treeview/)
The component follows the WAI-ARIA authoring practices.
To have an accessible tree view you must use aria-labelledby
or aria-label
to reference or provide a label on the TreeView, otherwise screen readers will announce it as "tree", making it hard to understand the context of a specific tree item.