@rc-component/tree

repository·master·Indexed 22 days ago

https://github.com/react-component/tree

A set of accessible tree view primitives for React, part of the Ant Design ecosystem. Version 1.4.0 supports controlled and uncontrolled expansion, selection, checking, drag and drop, and asynchronous loading via the loadData prop. It provides a Tree component for high-level configuration and a TreeNode component for manual construction, including support for virtual scrolling and custom switcher icons.

Tokens
9.3K
Snippets
7
Records
45
Agent score
77%

What's inside @rc-component/tree

  1. Performance note for async loading

    master

    When using asynchronous loading with loadData, the component caches some calculations to improve performance when checkable is enabled. To avoid issues with these cached states during async loading, you should conditionally render the tree only when your data is ready:

    {this.state.treeData.length ? <Tree ...>{this.state.treeData.map(t => <TreeNode ... />)}</Tree> : 'loading tree'}
  2. Keyboard navigation in Tree

    master

    The Tree component supports standard keyboard navigation for interacting with nodes. The behavior depends on whether the tree is in checkable (checkbox) mode or selectable mode.

    • ArrowUp / ArrowDown: Move the active focus up or down the list.
    • Home: Move focus to the first node in the tree.
    • End: Move focus to the last node in the tree.
    • ArrowLeft:
      • If the node is expandable and currently expanded: Collapse the node.
      • If the node is not expandable: Move focus to the parent node.
    • ArrowRight:
      • If the node is expandable and currently collapsed: Expand the node.
      • If the node is not expandable: Move focus to the first child node.

    Selection and Interaction

    • Enter:
      • If the node is expandable: Expand/Collapse the node.
      • If checkable is enabled: Toggle the checkbox.
      • If selectable is enabled: Select the node.
    • Space:
      • If checkable is enabled: Toggle the checkbox.
      • If selectable is enabled: Select the node.
  3. Define the data structure for Tree nodes

    master

    When providing data to the Tree component, you should follow the BasicDataNode interface. This interface defines the standard properties that the tree uses to manage node state and appearance. If you are using custom data structures, you can use FieldDataNode to wrap your custom type with these required tree properties.

    Key properties include:

    • key: A unique identifier for the node.
    • title: The content to display (can be a ReactNode or a function receiving the node data).
    • isLeaf: Boolean indicating if the node has no children.
    • selectable: Whether the node can be selected.
    • checkable: Whether the node can be checked (for checkbox trees).
    • disabled: Whether the node is interactive.
    • children: An array of child nodes.
  4. Implement drag and drop on TreeNodes

    master

    The TreeNode component supports native HTML5 drag and drop. While much of the logic is managed by the parent Tree context, the TreeNode handles the following event lifecycle:

    • onDragStart: Triggered when dragging begins.
    • onDragEnter / onDragOver / onDragLeave: Used to manage drag hover states and drop indicators.
    • onDrop: Triggered when a node is dropped onto the target.
    • onDragEnd: Triggered when the drag operation completes.

    Note that the draggable attribute is automatically applied to the node if the parent Tree context has draggable enabled and the specific node is not disabled.

  5. Expand and scroll to a nested node

    master

    This demo demonstrates the difference between controlled and uncontrolled approaches when expanding a node and scrolling to a nested node within the Tree component.

    In an uncontrolled approach, the Tree manages its own internal state for expansion and scrolling. In a controlled approach, you manage the expandedKeys and potentially other state properties externally, passing them back into the Tree to dictate its behavior.

  6. Implement a draggable tree using the Draggable demo example

    master
    To implement a tree with drag-and-drop functionality, refer to the draggable.jsx example. This implementation typically involves using the Tree component with specific props to enable dragging nodes and handling the reordering or movement of nodes within the tree structure.
  7. Basic Usage of @rc-component/tree

    master

    To use the tree component, import Tree and the DataNode type. You must also import the bundled CSS for the tree to render correctly. You can provide data via the treeData prop using an array of DataNode objects.

    import Tree from '@rc-component/tree';
    import type { DataNode } from '@rc-component/tree';
    import '@rc-component/tree/assets/index.css';
    
    const treeData: DataNode[] = [
      {
        key: '0',
        title: 'Documents',
        children: [{ key: '0-0', title: 'Roadmap' }],
      },
    ];
    
    export default () => (
      <Tree defaultExpandAll treeData={treeData} onSelect={keys => console.log(keys)} />
    );
  8. Implement Draggable Allow Drop in Tree

    master
    To implement a tree where nodes can be dragged and dropped onto specific targets (allowing/disallowing drops based on logic), refer to the draggable-allow-drop example. This pattern typically involves using the onDragStart and onDrop props (or similar drag-and-drop lifecycle events provided by the Tree component) to control the movement of nodes and validate whether a drop is permitted at a specific location.
  9. Implement a selectable tree using the Selectable demo example

    master
    To implement a tree where nodes can be selected, you can follow the pattern demonstrated in the selectable.jsx example. This typically involves using the @rc-component/tree component and managing the selection state (such as selectedKeys) via props. The example demonstrates how to handle user interactions to update the selection of specific nodes within the tree structure.
  10. Implement a custom switch icon in Tree

    master
    You can customize the icon used for expanding/collapsing tree nodes by providing a custom component or element to the switcher prop of the Tree component. This allows you to replace the default arrow icon with any custom UI element, such as a different icon library's symbol or a custom styled component.