antvis / Graphin

A React toolkit for graph visualization based on G6
https://graphin.antv.antgroup.com
MIT License
980 stars 262 forks source link

Legend.Node in Graphin package assumes bindType is Node in click handler. Throws `item.getEdges` is not a function error on item click. #472

Open msnyder-msft opened 1 year ago

msnyder-msft commented 1 year ago

Describe the bug

When you use the Legend component from the graphin library (not the graphin-components package) and you pass bindType='edge' it throws an exception onClick because the click handler assumes the legend items are related to a node. You can see the bug here in the Legend/Node.tsx file.

// from graphin/src/components/Legend/Node.tsx#L50
const handleClick = (option: OptionType) => {
    const nodes = dataMap.get(checkedValue.value);
    ...
    // @ts-ignore
    nodes.forEach((node: any) => {
      graph.setItemState(node.id, 'active', checkedValue.checked);
      graph.setItemState(node.id, 'inactive', !checkedValue.checked);
      const { id } = node;
      const item = graph.findById(id) **as INode**;
      const edges = item.getEdges();
      edges.forEach(edge => {
        graph.setItemState(edge, 'normal', checkedValue.checked);
        graph.setItemState(edge, 'inactive', !checkedValue.checked);
      })
    });

    onChange(checkedValue, result);
  };

The assumption that the item is an INode is the problem here since an IEdge would not have the getEdges function. It results in an error like this TypeError: item.getEdges is not a function image

Secondarily, if you use bindType as node the click handler breaks with a different error TypeError: onChange is not a function image

Update: This callback was introduced in this commit

Your Example Website or App

https://codesandbox.io/s/legend-bug-repro-package-2-0pzsjd?file=/App.tsx

Steps to Reproduce the Bug or Issue

Open the sandbox Click a legend item to filter

NOTE: if you switch the bindType to node the click handler throws a different error

Expected behavior

Should be able to have a legend for either edges or nodes and be able to filter them on the graph accordingly.

Screenshots or Videos

Legend Bug repro

Platform

Additional context

No response

pomelo-nwu commented 11 months ago

@msnyder-msft Thanks for your feedback, we will fix it as soon as possible

pomelo-nwu commented 11 months ago