Skip to main content

Overview

The TreeDropDownList component is a customizable dropdown that displays hierarchical data in a tree structure. It supports features like searching, clearing selection, and displaying icons.

Import

import TreeDropDownList from "@components/tree-drop-down-list/TreeDropDownList";

Usage

import React from "react";
import { FolderFilled } from "@tag/tag-icons";
import TreeDropDownList from "./TreeDropDownList";

const TreeDropDownListDemo = () => {
const data = [
{
id: 1,
name: "Parent 1",
children: [
{
id: 2,
name: "Child 1",
children: [{ id: 3, name: "Grandchild 1" }],
},
],
},
{
id: 4,
name: "Parent 2",
children: [
{
id: 5,
name: "Child 2",
children: [{ id: 6, name: "Grandchild 2" }],
},
],
},
];
return (
<TreeDropDownList
data={data}
textField="name"
valueField="id"
childrenField="children"
onSelect={(value) => console.log("Selected:", value)}
defaultValue={1}
icon={<FolderFilled />}
clearButton={true}
disabled={false}
readOnly={false}
searchPlaceholder="Search items..."
/>
);
};

export default TreeDropDownListDemo;

Result

Props

PropTypeDefaultDescription
dataArrayRequiredThe hierarchical data structure to display in the dropdown
onSelectFunction-Callback function called when an item is selected
defaultValueany-The default selected value
textFieldstringRequiredThe field name to display in the dropdown
valueFieldstringRequiredThe field name to use as the value
childrenFieldstringRequiredThe field name containing child items
iconReactNode-Optional icon to display next to the selected value
clearButtonbooleanfalseWhether to show a clear button
disabledbooleanfalseWhether the dropdown is disabled
readOnlybooleanfalseWhether the dropdown is read-only
searchPlaceholderstring""Placeholder text for the search input

Features

  • Hierarchical Display: Shows data in a tree structure with expandable/collapsible nodes
  • Search Functionality: Allows filtering items through a search input
  • Clear Selection: Option to clear the selected value
  • Custom Icons: Support for displaying custom icons next to selected values

Data Structure

The component expects data in a hierarchical structure where each item can have child items. Example:

const data = [
{
id: 1,
name: "Root Item",
children: [
{
id: 2,
name: "Child Item",
children: [{ id: 3, name: "Leaf Item" }],
},
],
},
];