Socket
Socket
Sign inDemoInstall

rc-tree

Package Overview
Dependencies
14
Maintainers
11
Versions
302
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    rc-tree

tree ui component for react


Version published
Weekly downloads
1.1M
decreased by-15.94%
Maintainers
11
Install size
1.18 MB
Created
Weekly downloads
 

Package description

What is rc-tree?

The rc-tree npm package is a React component for displaying tree views. It provides flexible and efficient tree components with customizable nodes, drag-and-drop functionality, checkable nodes, and event handling for various actions like select, check, load data asynchronously, and more. It's suitable for implementing features like file systems, nested menus, or any hierarchical data representation.

What are rc-tree's main functionalities?

Basic Tree Structure

This feature allows the creation of a basic tree structure with nested nodes. The `treeData` prop defines the hierarchical structure of the tree.

import Tree from 'rc-tree';

const treeData = [{
  title: 'parent 1',
  key: '0-0',
  children: [{
    title: 'child 1-1',
    key: '0-0-1',
    children: [{
      title: 'leaf',
      key: '0-0-1-1'
    }]
  }, {
    title: 'child 1-2',
    key: '0-0-2',
    children: [{
      title: 'leaf',
      key: '0-0-2-1'
    }]
  }]
}];

<Tree treeData={treeData} />

Drag-and-Drop

Enables drag-and-drop functionality within the tree, allowing users to reorder nodes by dragging them. Event handlers like `onDragEnter` and `onDrop` can be used to manage the drag-and-drop process.

import Tree from 'rc-tree';

<Tree
  treeData={treeData}
  draggable
  onDragEnter={info => console.log('enter:', info)}
  onDrop={info => console.log('drop:', info)}
/>

Checkable Nodes

This feature adds checkboxes to tree nodes, making them selectable. The `onCheck` event handler is triggered when checkboxes are checked or unchecked, allowing for custom handling of these events.

import Tree from 'rc-tree';

<Tree
  checkable
  onCheck={checkedKeys => console.log('onCheck', checkedKeys)}
  treeData={treeData}
/>

Async Data Loading

Supports loading data asynchronously for nodes. This is useful for loading child nodes on demand, for example, when a node is expanded. The `loadData` function is called to fetch child nodes asynchronously.

import Tree from 'rc-tree';

const loadData = treeNode =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });

<Tree loadData={loadData} treeData={treeData} />

Other packages similar to rc-tree

Readme

Source

rc-tree

Tree component.

NPM version dumi build status Test coverage Dependencies DevDependencies npm download bundle size

Screenshots

Feature

  • Support all popular browsers, including Internet Explorer 9 and above.

Example

http://localhost:9001/

online example: https://tree.react-component.now.sh/

Install

rc-tree

Usage

Note: import "rc-tree/assets/index.css"

see examples

API

Tree props

namedescriptiontypedefault
autoExpandParentwhether auto expand parent treeNodesboolfalse
checkablewhether support checkedbool/React Nodefalse
checkedKeysControlled checked treeNodes(After setting, defaultCheckedKeys will not work). Note: parent and children nodes are associated, if the parent node's key exists, it all children node will be checked, and vice versa. When set checkable and checkStrictly, it should be an object, which contains checked array and halfChecked array.String[]/{checked:Array,halfChecked:Array}[]
checkStrictlycheck node precisely, parent and children nodes are not associatedboolfalse
classNameadditional css class of root dom nodeString''
defaultCheckedKeysdefault checked treeNodesString[][]
defaultExpandedKeysexpand specific treeNodesString[][]
defaultExpandAllexpand all treeNodesboolfalse
defaultExpandParentauto expand parent treeNodes when initbooltrue
defaultSelectedKeysdefault selected treeNodesString[][]
disabledwhether disabled the treeboolfalse
draggablewhether can drag treeNode. (drag events are not supported in Internet Explorer 8 and earlier versions or Safari 5.1 and earlier versions.)bool | ({ node }) => booleanfalse
expandedKeysControlled expand specific treeNodesString[]-
filterTreeNodefilter some treeNodes as you need. it should return truefunction(node)-
iconcustomize icon. When you pass component, whose render will receive full TreeNode props as component propselement/Function(props)-
loadedKeysMark node is loaded when loadData is trueString[]-
loadDataload data asynchronously and the return value should be a promisefunction(node)-
multiplewhether multiple selectboolfalse
prefixClsprefix classString'rc-tree'
selectablewhether can be selectedbooltrue
selectedKeysControlled selected treeNodes(After setting, defaultSelectedKeys will not work)String[][]
showIconwhether show iconbooltrue
showLinewhether show lineboolfalse
treeDatatreeNodes data Array, if set it then you need not to construct children TreeNode. (value should be unique across the whole array)array<{key,title,children, [disabled, selectable]}>-
onCheckclick the treeNode/checkbox to firefunction(checkedKeys, e:{checked: bool, checkedNodes, node, event, nativeEvent})-
onExpandfire on treeNode expand or notfunction(expandedKeys, {expanded: bool, node, nativeEvent})-
onDragEndit execs when fire the tree's dragend eventfunction({event,node})-
onDragEnterit execs when fire the tree's dragenter eventfunction({event,node,expandedKeys})-
onDragLeaveit execs when fire the tree's dragleave eventfunction({event,node})-
onDragOverit execs when fire the tree's dragover eventfunction({event,node})-
onDragStartit execs when fire the tree's dragstart eventfunction({event,node})-
onDropit execs when fire the tree's drop eventfunction({event, node, dragNode, dragNodesKeys})-
onLoadTrigger when a node is loaded. If you set the loadedKeys, you must handle onLoad to avoid infinity loopfunction(loadedKeys, {event, node})-
onMouseEntercall when mouse enter a treeNodefunction({event,node})-
onMouseLeavecall when mouse leave a treeNodefunction({event,node})-
onRightClickselect current treeNode and show customized contextmenufunction({event,node})-
onSelectclick the treeNode to firefunction(selectedKeys, e:{selected: bool, selectedNodes, node, event, nativeEvent})-
switcherIconspecific the switcher icon.ReactNode / (props: TreeNodeAttribute) => ReactNode-
virtualDisable virtual scroll when falseboolean-
allowDropWhether to allow drop on node({ dragNode, dropNode, dropPosition }) => boolean-
dropIndicatorRenderThe indicator to render when dragging({ dropPosition, dropLevelOffset, indent: number, prefixCls }) => ReactNode-
directionDisplay direction of the tree, it may affect dragging behaviorltr | rtl-
expandActionTree open logic, optional: false | click | doubleClickstring | booleanclick

TreeNode props

note: if you have a lot of TreeNode, like more than 1000,
make the parent node is collapsed by default, will obvious effect, very fast.
Because the children hide TreeNode will not insert into dom.

namedescriptiontypedefault
classNameadditional class to treeNodeString''
checkablecontrol node checkable if Tree is checkableboolfalse
styleset style to treeNodeObject''
disabledwhether disabled the treeNodeboolfalse
disableCheckboxwhether disable the treeNode' checkboxboolfalse
titletree/subTree's titleString/element/((data: DataNode) => React.ReactNode)'---'
keyit's used with tree props's (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys. you'd better to set it, and it must be unique in the tree's all treeNodesStringtreeNode's position
isLeafwhether it's leaf nodeboolfalse
iconcustomize icon. When you pass component, whose render will receive full TreeNode props as component propselement/Function(props)-
switcherIconspecific the switcher icon.ReactNode / (props: TreeNodeAttribute) => ReactNode-

Note

The number of treeNodes can be very large, but when enable checkable, it will spend more computing time, so we cached some calculations(e.g. this.treeNodesStates), to avoid double computing. But, this bring some restrictions, when you async load treeNodes, you should render tree like this {this.state.treeData.length ? <Tree ...>{this.state.treeData.map(t => <TreeNode ... />)}</Tree> : 'loading tree'}

Development

npm install
npm start

Test Case

http://localhost:8018/tests/runner.html?coverage

Coverage

http://localhost:8018/node_modules/rc-server/node_modules/node-jscover/lib/front-end/jscoverage.html?w=http://localhost:8018/tests/runner.html?coverage

License

rc-tree is released under the MIT license.

Other tree views

Keywords

FAQs

Last updated on 07 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc