react-component / react-component/tree

How to have a reference to a tree node with current item

Open
#484 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1.3k
Forks
490
Avg merge
3d 17h
Merged PRs (30d)
5

Description

Here I have a tree select that I have implement using antd 3.x and now I'm trying to migrate rc-tree. So I rendered my tree using a list with the type of TreeNodeType[] type as follow

type.ts

export interface TreeItem {
  compositeKey: string;
  title?: string;
  totalLeafs?: number;
  level: number;
  totalChildren?: number;
}

export interface TreeNodeType extends TreeItem {
  title: string;
  parent?: TreeNodeType;
  children?: TreeNodeType[];
  allChildrenLoaded?: boolean;
  hasChildren?: boolean;
}

TreeView.ts

const renderTreeNodes = useCallback(
    (nodes: TreeNodeType[], level: number = 1): React.ReactNode =>
      nodes
        .filter(item => !hiddenKeys.includes(item.compositeKey))
        .map(item => {
          return (
            <TreeNode
              title={treeNodeRenderFn ? treeNodeRenderFn(item) : item.title}
              key={item.compositeKey}
              isLeaf={
                level === treeDepth ||
                (item.allChildrenLoaded && isEmpty(item.children)) || !item.hasChildren
              }
              // here we can't have itemRef with rc-tree but we can in antd3.x
              itemRef={item}
            >
              {item.children && renderTreeNodes(item.children, level + 1)}
            </TreeNode>
          );
        }),
    [treeDepth, hiddenKeys, treeNodeRenderFn, loading]
  );

But thing is we can keep a reference to each node like itemRef. But rc-tree not support to that. But rendering as following I need to have a reference to each and every tree node(like itemRef in the above code) as I have to use it as following.

const handleExpand = (_: string[], info: AntTreeNodeExpandedEvent) => {
    dispatch({
      type: ActionType.changeExpandedNodes,
      payload: {
        expanded: info.expanded,
        keys: [info.node.props.itemRef.compositeKey],
      },
    });
    onExpand && onExpand(info.node.props.itemRef, info.expanded);
  };

rc-tree component doesn't support itemRef like reference like in antd3.x

So, I tried many ways to overcome this problem. pls help me to find solution.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with type.ts and TreeView.ts, especially the TreeNode rendering and handleExpand callback. Read the rc-tree TreeNode API and compare it with the antd 3.x itemRef behavior shown in the issue. Done means each expanded node's current TreeNodeType data can be accessed by the expansion handler without unsupported props.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.