Usage
Tree component is used to display hierarchical data. Tree component
has minimal styling by default, you can customize styles with Styles API.
- src
- node_modules
- package.json
- tsconfig.json
Data prop
Data passed to the data prop should follow these rules:
- Data must be an array
- Each item in the array represents a node in the tree
- Each node must be an object with
valueandlabelkeys - Each node can have
childrenkey with an array of child nodes - The
valueof each node must be unique
Valid data example:
Invalid data example:
Data type
You can import TreeNodeData type to define data type for your tree:
renderNode
Use renderNode prop to customize node rendering.
renderNode function receives an object with the following properties as a single argument:
- src
- node_modules
- package.json
- tsconfig.json
useTree hook
useTree hook can be used to control selected and expanded state of the tree.
The hook accepts an object with the following properties:
And returns an object with the following properties:
You can pass the value returned by the useTree hook to the tree prop of the Tree component
to control tree state:
- src
- node_modules
- package.json
- tsconfig.json
Checked state
Tree can be used to display checked state with checkboxes.
To implement checked state, you need to render Checkbox.Indicator in the renderNode function:
- src
- node_modules
- package.json
- tsconfig.json
To check/uncheck nodes, use checkAllNodes and uncheckAllNodes functions:
- src
- components
- Accordion.tsx
- Tree.tsx
- Button.tsx
- node_modules
- react
- index.d.ts
- package.json
- @mantine
- core
- index.d.ts
- package.json
- hooks
- index.d.ts
- package.json
- form
- index.d.ts
- package.json
- package.json
- tsconfig.json
Initial expanded state
Expanded state is an object of node.value and boolean values that represent nodes expanded state.
To change initial expanded state, pass initialExpandedState to the useTree hook.
To generate expanded state from data with expanded nodes, you can use getTreeExpandedState function:
it accepts data and an array of expanded nodes values and returns expanded state object.
If '*' is passed as the second argument to getTreeExpandedState, all nodes will be expanded:
- src
- components
- Accordion.tsx
- Tree.tsx
- Button.tsx
- node_modules
- package.json
- tsconfig.json
Example: files tree
- src
- node_modules
- package.json
- tsconfig.json