2.0.3 • Published 6 months ago

@asup/tree-of-nodes v2.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

npm size npm bundle size License: MIT

@asup/tree-of-notes

REACT treeview, because I couldn't quite find what I wanted.

Works with/needs bootstrap, react-bootstrap, react-bootstrap-icons. Uses a context menu to facilitate Add/Rename/Remove actions.

Installation

# with npm
npm install @asup/tree-of-nodes

Tree-of-nodes Usage

import { iNodeUpdate, TreeNodeData, TreeOfNodes } from '@asup/tree-of-nodes';
import '@asup/tree-of-nodes/dist/style.css';

... inside REACT component

<TreeOfNodes<T>
  id: string;
  nodeList: TreeNodeData<T>[];
  roots: Key[];
  multiSelect?: boolean;
  selectedIds?: Key | Key[];
  handleSelect?: (ret: Key | Key []) => void;
  onAdd?: (parentId: Key, newName: string) => Promise<iNodeUpdate>;
  onRemove?: (childId: Key) => Promise<iNodeUpdate>;
  onRename?: (childId: Key, newName: string) => Promise<iNodeUpdate>;
  canRemoveRoot?: boolean;
  canRenameRoot?: boolean;
  canAddChildren?: boolean;
  canRemoveChildren?: boolean;
  canRenameChildren?: boolean;
  nodeHighlight?: string;
  textHighlight?: string;
  />

The component expects a list of nodes of type TreeNodeData<T> with unique React.Keys.

Properties

PropDescriptionDefault
idHTML id attribute
nodeListArray of node data
rootsOne or more node Keys to use as the root of the tree
multiSelectAllow selection of more than 1 idfalse
selectedIdsCurrently selected list
handleSelectFunction called when a node is clicked (selected), this should be used to update the selectedId() => {return;}
onAddFunction called when a new node is added, this should be used to update the nodeList
onRemoveFunction called when a node is removed, this should be used to update the nodeList
onRenameFunction called when a node is renamed, this should be used to update the nodeList
canRemoveRootAllows removal of a root node in combination with specification of onRemove functionfalse
canRenameRootAllows renaming of a root node in combination with specification of onRename functionfalse
canAddChildrenAllows addition of children in combination with specification of onAdd functionfalse
canRemoveChildrenAllows renaming of non-root nodes in combination with specification of onRemove functionfalse
canRenameChildrenAllows renaming of non-root nodes in combination with specification of onRename functionfalse
nodeHighlightSelected node highlight colourred
textHighlightSelected text highlight colourrgba(255,0,0,0.2
spellCheckUse/disable browser spell check'true'

TreeNodeData

Required format for list of nodes, includes a holder for any type of data, which is there specified in the TreeOfNodes specification.

export type TreeNodeData<T> = {
  id: Key;
  label: string;
  parentId?: Key;
  data: T;
};

iNodeUpdate

Use this format to return from update functions.

export interface iNodeUpdate {
  success: boolean;
  ErrorText?: string;
}

Handling clicks

Context menu clicks on a node will result in a context menu being displayed with "Add", "Rename" and "Delete" options being displayed.

Clicks on an expander will either expand or close the node's clidren

Clicks on a name will select that item

Clicks on a checkbox will select that item and all its descendents.

Single item selection

To handle one (and only one) item being selected, it is suggested to use these base options

  <TreeOfNodes<T>
    id={'...'}
    nodeList={[...]}
    roots={[...]}
    selected={selected}
    handleSelect={async (i) => {
      setSelected([i]);
    }}
  />

Multiple item selection

To handle multiple selections, it is suggested to use these base options

  <TreeOfNodes<T>
    id={'...'}
    nodeList={[...]}
    roots={[...]}
    showCheckBox
    selected={selected}
    handleSelect={async (i) => {
      if (Array.isArray(i)) {
        setSelected(
          selected.includes(i[0])
            ? selected.filter((s) => !i.includes(s))
            : [...selected, ...i.filter((n) => !selected.includes(n))],
        );
      } else {
        setSelected(
          selected.includes(i) ? selected.filter((s) => s !== i) : [...selected, i],
        );
      }
    }}
  />
2.0.3

6 months ago

1.0.8

10 months ago

2.0.2

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.0.7

12 months ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.9.1

2 years ago

0.9.0

2 years ago