Loading problem…
Build a Nested Checkbox Tree component in React that displays a hierarchical tree structure with checkboxes. The component should handle parent-child checkbox relationships correctly.
After any toggle, every ancestor must be recomputed from its direct children:
The indeterminate state must be applied using the native HTML checkbox indeterminate DOM property (via a ref + effect). It cannot be set via JSX attributes.
type TriState = boolean | "indeterminate";
interface TreeNode {
id: number;
label: string;
checked: TriState;
children?: TreeNode[];
}const exampleData: TreeNode[] = [
{
id: 1,
label: "Electronics",
checked: false,
children: [
{
id: 2,
label: "Mobile phones",
checked: false,
children: [
{ id: 3, label: "iPhone", checked: false },
{ id: 4, label: "Samsung", checked: false },
],
},
{
id: 5,
label: "Laptops",
checked: false,
children: [
{ id: 6, label: "MacBook", checked: false },
{ id: 7, label: "ThinkPad", checked: false },
],
},
],
},
{
id: 8,
label: "Clothing",
checked: false,
children: [
{ id: 9, label: "Shirts", checked: false },
{ id: 10, label: "Pants", checked: false },
],
},
{ id: 11, label: "Books", checked: false },
];