Skip to content

Commit

Permalink
✨ feat: add edge select
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangchu committed Oct 19, 2023
1 parent df21919 commit 1aeeeab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/ProFlow/demos/ProFlowDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,25 @@ const edges = [
{
id: 'a1-a2',
source: 'a1',
select: NodeSelect.WARNING,
target: 'a2',
},
{
id: 'a1-b1',
source: 'a1',
select: NodeSelect.WARNING,
target: 'b1',
},
{
id: 'a2-a3',
source: 'a2',
select: NodeSelect.WARNING,
target: 'a3',
},
{
id: 'a3-a4',
source: 'a3',
select: NodeSelect.WARNING,
target: 'a4',
},
];
Expand Down
31 changes: 14 additions & 17 deletions src/ProFlow/helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BloodNode from '@/BloodNode';
import Dagre from '@dagrejs/dagre';
import { cx } from 'antd-style';
import { Edge, Node, Position } from 'reactflow';
import { ProFlowEdge } from '..';
import {
EDGE_DANGER,
EDGE_SELECT,
Expand Down Expand Up @@ -108,27 +109,29 @@ function getEdgeClsFromNodeSelect(select: NodeSelect) {
}
}

function getRenderEdge(node: NodeMapItem, targetNode: NodeMapItem) {
const { id } = node;
const { id: targetId, select = NodeSelect.DEFAULT } = targetNode;
export function getRenderEdges(edges: ProFLowEdge[]) {
return edges.map((edge) => {
const { source, target, select = NodeSelect.DEFAULT } = edge;

return {
id: `${id}-${targetId}`,
source: id!,
target: targetId!,
type: 'radiusEdge',
className: getEdgeClsFromNodeSelect(select),
};
return {
id: `${source}-${target}`,
source,
target,
type: 'radiusEdge',
className: getEdgeClsFromNodeSelect(select),
};
});
}

export const getRenderData = (
mapping: NodeMapping,
edges: ProFlowEdge[],
): {
nodes: Node[];
edges: Edge[];
} => {
const renderNodes: Node[] = [];
const renderEdges: Edge[] = [];
const renderEdges: Edge[] = getRenderEdges(edges);
// const { styles, cx } = useStyles();

Object.keys(mapping).forEach((id) => {
Expand Down Expand Up @@ -168,12 +171,6 @@ export const getRenderData = (
),
},
});

if (node.right!.length) {
node.right!.forEach((targetId: string) => {
renderEdges.push(getRenderEdge(node, mapping[targetId]));
});
}
});

const { _nodes, _edges } = setNodePosition(renderNodes, renderEdges);
Expand Down
9 changes: 4 additions & 5 deletions src/ProFlow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ const Flow: React.FC<Partial<ProFlowProps>> = (props) => {
nodes: Node[];
edges: Edge[];
} => {
if (mapping) {
const { nodes, edges } = getRenderData(mapping);

if (mapping && edges!.length) {
const { nodes, edges: _edges } = getRenderData(mapping, edges!);
return {
nodes,
edges,
edges: _edges,
};
} else {
return {
nodes: [],
edges: [],
};
}
}, [mapping]);
}, [mapping, edges]);

// const [_edges] = useEdgesState(renderData.edges);

Expand Down

0 comments on commit 1aeeeab

Please sign in to comment.