0.0.10 • Published 1 year ago

@henliwu1491/react-tree v0.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React Tree

npm Build Status GitHub license

React Tree is a tree data structure ui library designed for easily building tree components.

Demo

Note: This project is for testing purposes only and is not intended for production use.

Features

  • 🪝  Hook provided: useTreeView hooks provide tree expanded & selected state for easily render tree component

  • 🖼️  Custom Icons: Supports custom icons using React nodes for enhanced visual appeal.

Installation

Using npm, Yarn, or PNPM:

  1. Install the package with npm:
npm install @henliwu1491/react-tree
  1. Install the package with Yarn:
yarn add @henliwu1491/react-tree
  1. Install the package with PNPM:
pnpm add @henliwu1491/react-tree

Choose the package manager that you prefer for installation.

Include CSS

import "@henliwu1491/react-tree/dist/style.css"

Usage

Example:

<TreeView />

import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return <TreeView initialState={{ selectedId, expandedId }} data={data} />;
}
import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      initialState={{ selectedId, expandedId }}
      data={data}
      getLabel={(item) => {
        if (item.type === 'leaf') {
          return (
            <div className="flex">
              <div>Leaf: {item.label}</div>
            </div>
          );
        }
        return item.label;
      }}
    />
  );
}
import { TreeView } from '@henliwu1491/react-tree';

export default function Tree() {
  const [selectedId] = React.useState(['12']);
  const [expandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      initialState={{ selectedId, expandedId }}
      data={data}
      icon={{
        expand: '▲',
        collapse: '▼',
        leaf: '🌱',
        checked: '☑',
        unchecked: '☐',
        indeterminate: '-', // Or <span>your custom React component</span>
      }}
    />
  );
}
export default function Tree() {
  const [selectedId, setSelectedId] = React.useState(['12']);
  const [expandedId, setExpandedId] = React.useState(['2', '21', '221']);

  return (
    <TreeView
      value={{ selectedId, expandedId }}
      data={data}
      onExpand={(item) => {
        setExpandedId((prev) =>
          prev.length === 0
            ? [item.value]
            : prev.indexOf(item.value) === -1
              ? [...prev, item.value]
              : prev.filter((id) => id !== item.value)
        );
      }}
      onSelect={(item) => {
        setSelectedId((prev) =>
          prev.length === 0
            ? [item.value]
            : prev.indexOf(item.value) === -1
              ? [...prev, item.value]
              : prev.filter((id) => id !== item.value)
        );
      }}
    />
  );
}

useTreeView Props

Options

Below are the available configuration options for the hook:

NameTypeDescriptionDefault
initialStateTreeInitialStateOptional.
dataTreeRawData[]Required. Your raw tree structure data. (must contain id, label and value key)
onExpandfunctionOptional. Callback function you can get node item from the parameter.
onSelectfunctionOptional. Callback function you can get node item from the parameter.
leafKeystringOptional. Customized leaf node.'leaf'

Instance

NameTypeDescriptionDefault
expandedIdstring[]
selectedIdstring[]
dataTreeRawData[]
onExpandfunction
onSelectfunction
setExpand(string) => void
checkNodeAndChildren(string) => void
checkSingleNode(string) => void
setInitialState(TreeInitialState) => void

<TreeView /> Props

Options

Below are the available configuration options for the component:

NameTypeDescriptionDefault
initialStateTreeInitialStateOptional.
dataTreeRawData[]Required. Your raw tree structure data. (must contain id, label and value key)
onExpandfunctionOptional. Callback function you can get node item from the parameter.
onSelectfunctionOptional. Callback function you can get node item from the parameter.
valueTreeInitialStateOptional. Control your own state.
iconIconConfigOptional. Provide your custom icon, React.ReactNode only.
getLabelfunctionOptional. Your own label render function.
leafKeystringOptional. Customized leaf node.'leaf'

initialState

export type TreeInitialState = {
  expandedId: string[];
  selectedId: string[];
};

data

id, label, value are required. And if nested data is provided, children is also required.

export type TreeRawData = {
  id: string;
  label: string;
  value: string;
  children?: TreeRawData[];
  [key: string]: any;
};

Helper functions

NameTypeDescriptionDefault
getLeafNodesfunction(data: TreeData[], leafkey?: string) => TreeData[]
getNormalizedNodesfunction(data: TreeRawData[], level = 0) => TreeRawData[]
getFlattenNodesfunction(data: TreeRawData[]) => TreeRawData[]
getExpandedNodesfunction(nodes: TreeData[], expandedId: TreeData['value'][]) => TreeData[]
getSelectedNodesfunction(nodes: TreeData[], selectedId: TreeData['value'][]) => TreeData[]
getSelectedIdWithChildrenfunction(nodes: TreeData[], selectedIds: TreeData['value'][], checkId: string, set: Set<string or number> = new Set(selectedIds)) => TreeData[]

🤝Contributing

We welcome contributions! If you find a bug or have an idea for improvement, please open an issue or submit a pull request on Github.

  1. Fork it
  2. Create your feature branch (git checkout -b new-feature)
  3. Commit your changes (git commit -am 'Add feature')
  4. Push to the branch (git push origin new-feature)
  5. Create a new Pull Request

Author ✨

💻   Henry Wu(吳亨利)

Licence

This project is licensed under the MIT License.

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.5

1 year ago

0.0.6

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago