A simple and elegant checkbox tree for React.
npm install reactjs-checkbox-tree--vars) for styling.
bash
yarn add reactjs-checkbox-tree
or
npm install reactjs-checkbox-tree --save
`
Usage
Import the pure CSS style:
`jsx
import 'reactjs-checkbox-tree/lib/reactjs-checkbox-tree.css';
//Basic example:
import React, { useState } from 'react';
import CheckboxTree from 'reactjs-checkbox-tree';
const nodes = [{
value: 'mars',
label: 'Mars',
children: [
{ value: 'phobos', label: 'Phobos' },
{ value: 'deimos', label: 'Deimos' },
],
}];
function Widget() {
const [checked, setChecked] = useState([]);
const [expanded, setExpanded] = useState([]);
return (
nodes={nodes}
checked={checked}
expanded={expanded}
onCheck={setChecked}
onExpand={setExpanded}
/>
);
}
`
Icons customization
Use iconsClass="fa4" for FontAwesome 4 support, or override icons with the icons prop:
`jsx
...
iconsClass="fa4"
icons={{
check: ,
uncheck: ,
halfCheck: ,
expandClose: ,
expandOpen: ,
expandAll: ,
collapseAll: ,
parentClose: ,
parentOpen: ,
leaf: ,
}}
/>
`
Or integrate with @fortawesome/reactjs-fontawesome:
`jsx
import { FontAwesomeIcon } from '@fortawesome/reactjs-fontawesome'
...
icons={{
check: ,
uncheck: ,
halfCheck: ,
expandClose: ,
expandOpen: ,
expandAll: ,
collapseAll: ,
parentClose: ,
parentOpen: ,
leaf:
}}
/>
`
TypeScript typings (new)
`ts
import { CHECK_MODEL } from "../components/constants";
export interface Icons {
check: React.ReactNode;
uncheck: React.ReactNode;
halfCheck: React.ReactNode;
expandClose: React.ReactNode;
expandOpen: React.ReactNode;
expandAll: React.ReactNode;
collapseAll: React.ReactNode;
parentClose: React.ReactNode;
parentOpen: React.ReactNode;
leaf: React.ReactNode;
}
export interface Language {
collapseAll: string;
expandAll: string;
toggle: string;
}
export interface TreeNodeInfo {
value: string;
checked?: boolean;
halfCheck?: boolean;
expanded?: boolean;
disabled?: boolean;
}
export interface CheckboxTreeNode extends TreeNodeInfo {
label: string;
title?: string;
children?: CheckboxTreeNode[];
showCheckbox?: boolean;
className?: string;
parent?: CheckboxTreeNode;
icon?: React.ReactNode;
isChild?: boolean;
isParent?: boolean;
isLeaf?: boolean;
treeDepth?: number;
index?: number;
}
export interface NodeModelProps {
disabled?: boolean;
noCascade?: boolean;
}
export interface FlatNodes {
[value: string]: CheckboxTreeNode;
}
export interface CheckboxTreeProps {
nodes: CheckboxTreeNode[];
checkModel?: typeof CHECK_MODEL.LEAF | typeof CHECK_MODEL.ALL;
checked?: string[];
direction?: "ltr" | "rtl";
disabled?: boolean;
expandDisabled?: boolean;
expandOnClick?: boolean;
expanded?: string[];
icons?: Partial;
id?: string | null;
lang?: Language;
nativeCheckboxes?: boolean;
noCascade?: boolean;
onlyLeafCheckboxes?: boolean;
optimisticToggle?: boolean;
showExpandAllButtons?: boolean;
showNodeIcon?: boolean;
showNodeTitle?: boolean;
onCheck?: (checked: string[], node: CheckboxTreeNode) => void;
onClick?: (node: CheckboxTreeNode) => void;
onExpand?: (expanded: string[], node?: CheckboxTreeNode) => void;
}
``