Skip to main content

Tree tree display

Effect demonstration

1. Default effect

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];

<Tree treeData={treeData} />;

2. Expand all by default

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];

<Tree treeData={treeData} expandAll />;

3. No selection allowed

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];

<Tree treeData={treeData} checkable={false} />;

4. Expand certain nodes by default

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];
<Tree treeData={treeData} defaultExpandedKeys={["0-0"]} />;

5. Select some nodes by default

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];

<Tree treeData={treeData} defaultCheckedKeys={["0-0-0-1"]} />;

6. Disable some nodes

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
disabled: true,
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
disabled: true,
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];

<Tree treeData={treeData} expandAll />;

7. Custom expand icon

const CustomExpandIcon: FC<{ progress: Animated.SharedValue<number> }> = ({
progress,
}) => {
const theme = useTheme<AppTheme>();
const style = useAnimatedStyle(() => ({
transform: [{ rotateZ: `${mix(progress.value, 0, Math.PI / 2)}rad` }],
}));

return (
<Animated.View style={style}>
<SvgIcon name="bells" color={theme.colors.warningColor} />
</Animated.View>
);
};

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];
<Tree
treeData={treeData}
customExpandIcon={(progress) => <CustomExpandIcon {...{ progress }} />}
/>;

8. Custom node style

const treeData = [
{
text: "parent 1",
id: "0-0",
items: [
{
text: "parent 1-0",
id: "0-0-0",
items: [
{
text: "leaf1",
id: "0-0-0-0",
style: { backgroundColor: "red" },
textStyle: { color: "white" },
},
{
text: "leaf2",
id: "0-0-0-1",
},
],
},
{
text: "parent 1-1",
id: "0-0-1",
items: [{ text: "sss", id: "0-0-1-0" }],
},
],
},
];
<Tree treeData={treeData} nodeStyle={{ backgroundColor: "orange" }} />;

API

TreeProps

PropertiesRequiredDescriptionTypeDefault Value
datatrueTree node dataTreeItemProps[]
checkablefalseWhether it can be selectedbooleantrue
checkedKeysfalseselected node (controlled)string[]
defaultCheckedKeysfalseThe key selected by default is valid when loaded for the first timestring[]
expandAllfalseExpand allbooleanfalse
defaultExpandedKeysfalseDefault expanded nodesstring[]
expandedKeysfalseexpanded nodestring[]
onCheckfalseSelected event callback(keys: string[]) => void
onExpandfalseExpand event callback(keys: string[]) => void
customExpandIconfalseCustom expansion icon(progress: Animated.SharedValue<number>) => ReactElement
activeOpacityfalseThe opacity of the tree node when clickednumber0.6
stylefalsetree styleStyleProp<ViewStyle>
nodeStylefalseTree node styleStyleProp<ViewStyle>

TreeItemProps

PropertiesRequiredDescriptionTypeDefault Value
idtrueUnique identifier of the nodenumber
texttruenode textstring
itemsfalsechild nodesTreeItemProps[]
disabledfalseWhether to disablenumberfalse
onPressfalseCallback for clicking a tree node(id: string) => void
customCheckIconfalseCustom check icon(checked: 'all' | 'half' | 'none') => ReactElement
stylefalse Node styleStyleProp<ViewStyle>
textStylefalseNode text styleStyleProp<TextStyle>