Tree
Tree
is a versatile component designed for selecting items from a nested structure. It allows users to navigate through branches and leaves, providing a clear and intuitive interface for selecting categories or other hierarchical data.
Prerequisites
Data
To use the Tree
component, you need to provide it with recurring data type matching:
type BranchType = {
id: string | number
name: string
children?: BranchType[]
}
In this example the utils function buildTree
is used to convert the data to the required format. Keep in mind this function is expecting certain input data structure.
Controls
- By default the
Tree
component is not providing any controls for the user to interact with. You can use theonLeafClick
andonBranchClick
props to handle the user interaction and provide your own controls. - If you provide
onClear
prop, the component will render a clear button whenvalue
is selected.
Styling
You can use the cx
object prop to style the Tree
component. Each section of component can be styled separately:
- placeholder
- value
- container:
- inner (value and buttons)
- outer (whole component)
Example
import { Tree, BranchType, buildTree } from '@creation-ui/react'
import { useMemo, useState } from 'react'
import data from './tree-data.json'
export const TreeExample = () => {
const tree = useMemo(() => buildTree(data), [data])
const [value, setValue] = (useState < BranchType) | (null > null)
const handleClear = () => setValue(null)
const handleLeafClick = (leaf: BranchType) => {
setValue(leaf)
console.log('Leaf clicked:', leaf.name)
}
const handleBranchClick = (branch: BranchType) => {
console.log('Branch clicked:', branch.name)
}
return (
<Tree
placeholder='Select category...'
tree={tree}
value={value}
onClear={handleClear}
onLeafClick={handleLeafClick}
onBranchClick={handleBranchClick}
cx={{
container: {
inner: 'w-48',
},
}}
/>
)
}
Component API
Prop | Default | Description |
cx | — | {
placeholder?: 'string',
value?: 'string',
container?: {
inner?: 'string',
outer?: 'string',
}
} Class names manipulation API. Each property ("value", "placeholder", etc.) is assigned to corresponding part of the component. |
onClear | — | () => void Callback to clear the selected value. |
onBranchClick | — | (branch: BranchType) => void Callback to handle branch click. |
onLeafClick | — | (leaf: BranchType) => void Callback to handle leaf click. |
tree | — | TreeType = BranchType[] Tree data structure. |
value | — | BranchType | null Selected value. |
placeholder | Select... | string Placeholder text. |
getItemLabel | item => item.name | (item: BranchType) => string Custom label getter. |
getItemOffset | (level: number) => level > 0 ? level * TREE_OFFSET_MULTIPLIER /*=16*/ : MIN_ITEM_PADDING /*=5*/ | (level: number) => number Custom offset getter. Used to apply `paddingLeft` to items. `level` starts from `0`. |
zIndex | — | { list?: number} Escape hatch for z-index of dropdown list. Helpful when using with modals and drawers that might have their own, higher z-index. |