react-arborist icon indicating copy to clipboard operation
react-arborist copied to clipboard

Can't `selectMulti` nonvisible nodes

Open victorvianaom opened this issue 2 years ago • 3 comments
trafficstars

Hello There. Using the tree api I can .select and .deselect a nonvisible node, but I'm not able to .selectMulti a node that is not in the view. Inspecting the tree-api code, I found out that .selectMulti uses this.get() method, which cannot access nonvisible nodes. I modified the .selectMulti method to retrieve the node id with identify(identity);, similar to what .select and .deselect do, and it worked, this way I can multi select nonvisible nodes.

  select(node: Identity, opts: { align?: Align } = {}) {
    if (!node) return;
    const id = identify(node);
    this.dispatch(focus(id));
    this.dispatch(selection.only(id));
    this.dispatch(selection.anchor(id));
    this.dispatch(selection.mostRecent(id));
    this.scrollTo(id, opts.align);
    if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode);
    safeRun(this.props.onSelect, this.selectedNodes);
  }

  deselect(node: Identity) {
    if (!node) return;
    const id = identify(node);
    this.dispatch(selection.remove(id));
  }

  selectMulti(identity: Identity) {
    // const node = this.get(identifyNull(identity));
    if (!identity) return;
    const id = identify(identity); // new line
    this.dispatch(focus(id)); // previous node.id
    this.dispatch(selection.add(id)); // previous node.id
    this.dispatch(selection.anchor(id)); // previous node.id
    this.dispatch(selection.mostRecent(id)); // previous node.id
    this.scrollTo(identity);
    if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode);
    safeRun(this.props.onSelect, this.selectedNodes);
  }

My question is. What's the reason for using this.get() instead of directly calling identify() for getting the id? And, is there any workaround for selecting multiple nonvisible nodes, other than modifying the lib?

victorvianaom avatar Mar 13 '23 13:03 victorvianaom