Tree view
Examples
Check out the Usage section for details about how to design a tree view properly, and the different configuration options we provide.
Basic example
A basic tree view is a TreeView component that wraps one or more TreeNode components. Each tree node can also contain its own nested TreeNode components.
<TreeView>
<TreeNode
id="1"
label="Fruit">
<TreeNode
id="2"
label="Banana"
/>
<TreeNode
id="3"
label="Apple"
/>
</TreeNode>
<TreeNode
id="4"
label="Vegetables (coming soon!)"
/>
</TreeView>
Nodes prop example
You can use the nodes property to pass the nodes for the tree view as an array of objects. See the tree node properties table for more information about the values that can be passed to this property.
const foods = [
{
id: 'fruit',
label: { id: 'foods.Fruit', defaultMessage: 'Fruit' },
nodes: [
{
id: 'banana',
label: { id: 'foods.Banana', defaultMessage: 'Banana' },
},
{
id: 'apple',
label: { id: 'foods.Apple', defaultMessage: 'Apple' },
},
],
},
{
id: 'vegetables',
label: { id: 'foods.Vegetables', defaultMessage: 'Vegetables (coming soon!)' },
},
];
<TreeView nodes={foods} />;
Render nodes example
If you want to override something about your nodes when using the nodes props, you can do it with the renderNodes property.
The props object of renderNodes contains the following:
nodeId- theidassigned to the tree nodenodes- the nodes object passed to the tree node
The nodes object contains nested tree nodes, so you need to iterate recursively to render the entire tree.
For example, if you want to apply some styles to each node you can create a custom component that takes the list of nodes and renders them as tree nodes recursively while applying the style to each one.
const foods = [ ... ]
const TomatoNodes = ({ nodes }) => {
return (
<>
{nodes.map(({ id, label, nodes: childNodes }) => (
<TreeNode
id={id}
label={label}
style={{
backgroundColor: 'tomato',
color: 'blue',
border: '1px solid yellow',
}}>
{childNodes && <TomatoNodes nodes={childNodes} />}
</TreeNode>
))}
</>
);
};
<TreeView
nodes={foods}
renderNodes={TomatoNodes}
/>
Filter example
If you want to include a filter field in your tree view, add the showFilter prop. The field uses a default filtering function, and if you want to override it, use the filterCallback prop.
The filterCallback function takes two arguments:
- the translated label of the tree element
- the filtering query that the user entered in the filter field
The function needs to return an array of indices that show ranges where the label matches the query, for example [{ start: 2, end: 4 }, { start: 7, end: 9 }].
If the query does not match, the function needs to return null.
The example below matches items only if the label starts with the query (case sensitive).
const foods = [ ... ]
const filterFunction (value, query) => {
if (value.startsWith(query)) {
return [{ start: 0, end: query.length }];
}
return null;
}
<TreeView
showFilter
nodes={foods}
filterCallback={filterFunction}
/>
Filter with props example
The filter field is a standard Jutro text input. You can pass additional props using the filterProps property.
const foods = [ ... ]
const filterFunction (value, query) => { ... }
<TreeView
showFilter
filterCallback={filterFunction}
filterProps={{
placeholder: 'Item starts with...',
}}
/>
Usage
Coming soon.
Code
<TreeView>
<TreeNode
id="1"
label="Fruit">
<TreeNode
id="2"
label="Banana"
/>
<TreeNode
id="3"
label="Apple"
/>
</TreeNode>
<TreeNode
id="4"
label="Vegetables (coming soon!)"
/>
</TreeView>
Import statement
import { TreeView, TreeNode, TreeNodeHeader } from '@jutro/components';
Component contract
Make sure to understand the Design System components API surface, and the implications and trade-offs. Learn more in our introduction to the component API.
Properties
Tree view properties
children- Type
React.ReactNode[]DescriptionReact nodes to be rendered in a tree view. This is usually tree node components wrapped by the tree view component. Either
nodesorchildrenis required. nodes- Type
TreeNodeData[]DescriptionReact nodes to be rendered in a tree view. This is usually an array of objects with the same properties as tree node components. Either
nodesorchildrenis required. className- Type
stringDescriptionCSS class name for this component.
defaultEndIcon- Type
string | IconDescriptionDefault icon to be rendered on the last node of the tree. The value is an Icon component or an icon name. For example,
CheckIconor'gw-check'. defaultExpandIcon- Type
string | IconDescriptionDefault icon to be rendered when a tree node can be expanded. The value is an Icon component or an icon name. For example,
CheckIconor'gw-check'. defaultIcon- Type
string | IconDescriptionDefault icon component. The value is an Icon component or an icon name. For example,
CheckIconor'gw-check'. defaultParentIcon- Type
string | IconDescriptionDefault icon to be rendered when node is a parent. The value is an Icon component or an icon name. For example,
CheckIconor'gw-check'. defaultRenderChildren- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean }>DescriptionDefault children render function.
defaultRenderHeader- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean }>DescriptionDefault header render function or component.
filterCallback- Type
(value: string, query: string) => Array<{ start: number, end: number }> | undefined;DescriptionCustom comparison function, which takes translated label value as first argument and filtering query as second. It must return null if value is not matched to query, otherwise it returns an array of matching parts indices.
filterProps- Type
objectDescriptionProperties passed to filter input.
hideCollapseAll- Type
booleanDescriptionIf set to
true, the 'Collapse/Expand all' button will be hidden. onClick- Type
function (React.MouseEvent<HTMLElement>)DescriptionOn click event handler.
renderCollapseAll- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean; }>DescriptionFunction or component used to render a custom 'Collapse/Expand all' button.
renderFilter- Type
(onChange: TreeViewFilterProps['onChange']) => ReactNodeDescriptionCustom function for rendering the filter component.
renderNodes- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean }>DescriptionThe nodes render function. This is only used when using nodes config.
showFilter- Type
booleanDescriptionIf set to
true, a filter input will be visible. defaultRenderExpandIcondeprecated- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean }>DescriptionDefault expand icon render function or component. Deprecated: since 10.9.0, please use
defaultExpandIconprop instead. defaultRenderIcondeprecated- Type
JSXElementConstructor<{ nodeId: string, expanded: boolean, isEndNode: boolean }>DescriptionDefault icon render function or component. Deprecated: since 10.9.0, please use
defaultIconprop instead.
Tree node properties
idrequired- Type
stringDescriptionUnique identifier for the component.
labelrequired- TypeDescription
Text label for the node.
className- Type
stringDescriptionCSS class name for this component.
disabled- Type
booleanDescriptionIf set to
true, the node is disabled. expandIcon- Type
JSXElementConstructor<{ 'data-nodeid': string, expanded: boolean, isEndNode: boolean }>DescriptionRender function for the expand icon header render function or component.
hideIcon- Type
booleanDescriptionIf set to
true, the icon will be hidden. icon- Type
string | IconDescriptionAn Icon component to render on the component. The value must be an
Iconcomponent or the icon's name. For example,CheckIconor'gw-check'. nodeHeaderClassName- Type
stringDescriptionCSS class name for the node header.
onClick- Type
(args: { id: string, hasNodes: boolean, expanded?: boolean | undefined }) => voidDescriptionThe tree node's handler for click events.
renderChildren- Type
JSXElementConstructor<{ nodeId: string, children: ReactNode, expanded: boolean }>DescriptionThe tree node's render function or component. Used only when using nodes config.
renderHeader- Description
The tree node's header render function or component. .
renderExpandIcondeprecated- Type
JSXElementConstructor<{ icon?: string | Icon, nodeId: string }>;DescriptionThe node's expand icon render function or component. Deprecated since 10.9.0, please use the
iconprop instead. renderIcondeprecated- Type
JSXElementConstructor<{ icon?: string | Icon, nodeId: string }>;DescriptionThe node's header icon render function or component. Will be passed to the
renderHeaderproperty. Deprecated since 10.9.0, please use theiconprop instead.
Tree node header properties
idrequired- Type
stringDescriptionUnique identifier for the component.
labelrequired- Type
stringDescriptionThe header's translated label.
nodeIdrequired- Type
stringDescriptionThe id for the current node.
hideIcon- Type
booleanDescriptionIf set to
true, the icon will be hidden. icon- Type
string | IconDescriptionAn Icon component to render on the component. The value must be an
Iconcomponent or the icon's name. For example,CheckIconor'gw-check'. labelMatches- Type
Array<{ start: number, end: number }>DescriptionArray of indices to highlight.
renderIcondeprecated- Type
JSXElementConstructor<{ icon?: string | Icon, nodeId: string }>;DescriptionThe node's header icon render function or component. Will be passed to the
renderHeaderproperty. Deprecated since 10.9.0, please use theiconprop instead.
Hooks
No hooks are available for tree view.
Translation keys
There are six translation key associated to the tree view component:
| Key | Used for |
|---|---|
| jutro-components.TreeView.filterLabel | aria-label value for the tree view filter. |
| jutro-components.TreeView.filterPlaceholder | Placeholder text for the tree view filter. |
| jutro-components.TreeView.collapseAll | Text label for the Collapse all button. |
| jutro-components.TreeView.expandAll | Text label for the Expand all button. |
| jutro-components.TreeView.noMatchesFound | Text displayed when filter finds no matches. |
| jutro-components.TreeView.clearFilter | aria-label value for the Clear filter button. |
Escape hatches
For more information, see our documentation about escape hatches.
Changelog
10.9.0
-
A new opt-in feature was introduced that disables automatic event publishing for the TreeView component. You can enable this feature by adding
JUTRO_DISABLE_AUTO_EVENTS_PUBLISHING=trueto the.envfile in the root of your Jutro application. When this is enabled, legacy components no longer publish events by default. For more information on events and how to set up new events, see the Events documentation.
icon property types extended
The type of the 'defaultParentIcon' and 'defaultEndIcon' properties for TreeView and the icon property for TreeNode have been extended to string | React.ComponentType.
Deprecations
Passing a string as a value to the the 'defaultParentIcon' and 'defaultEndIcon' properties for TreeView and the icon property for TreeNode have been deprecated.