make node dleete work

This commit is contained in:
Simon Larsen
2023-02-06 18:43:18 +00:00
parent aa957a8fa6
commit f9da5c7ece
3 changed files with 67 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
import { JSONObject } from 'Common/Types/JSON';
import React, { FunctionComponent, useState } from 'react';
import { Handle, Position, Connection } from 'reactflow';
import Icon, { IconProp } from '../Icon/Icon';
import Icon, { IconProp, ThickProp } from '../Icon/Icon';
export interface NodeDataProp {
nodeData: JSONObject;
@@ -10,6 +10,7 @@ export interface NodeDataProp {
description: string;
icon: IconProp;
isTrigger: boolean;
onDeleteClick: (id: string) => void;
}
export interface ComponentProps {
@@ -45,10 +46,39 @@ const Node: FunctionComponent<ComponentProps> = (props: ComponentProps) => {
'0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)',
}}
>
{isHovering && <div style={{
"width": "20px",
height: "20px",
borderRadius: "100px",
background: "#ef4444",
position: "absolute",
top: "-9px",
left: "228px",
cursor: "pointer"
}} onClick={() => {
if (props.data.onDeleteClick) {
props.data.onDeleteClick(props.data.id);
}
}} >
<Icon
icon={IconProp.Close}
style={{
color: "white",
width: '1rem',
height: '1rem',
textAlign: 'center',
margin: 'auto',
marginTop: "2px"
}}
thick={ThickProp.Thick}
/>
</div>}
{!props.data.isTrigger && (
<Handle
type="target"
onConnect={(_params: Connection) => {}}
onConnect={(_params: Connection) => { }}
isConnectable={true}
position={Position.Top}
style={handleStyle}
@@ -114,7 +144,7 @@ const Node: FunctionComponent<ComponentProps> = (props: ComponentProps) => {
<Handle
type="source"
id="a"
onConnect={(_params: Connection) => {}}
onConnect={(_params: Connection) => { }}
isConnectable={true}
position={Position.Bottom}
style={handleStyle}

View File

@@ -14,6 +14,7 @@ import ReactFlow, {
ProOptions,
NodeTypes,
OnConnect,
getConnectedEdges,
} from 'reactflow';
// 👇 you need to import the reactflow styles
import 'reactflow/dist/style.css';
@@ -46,7 +47,33 @@ export interface ComponentProps {
const Workflow: FunctionComponent<ComponentProps> = (props: ComponentProps) => {
const edgeUpdateSuccessful: any = useRef(true);
const [nodes, _setNodes, onNodesChange] = useNodesState(props.initialNodes);
const deleteNode = (id: string) => {
// remove the node.
const nodesToDelete = [...nodes].filter((node) => {
return node.data.id === id
})
const edgeToDelete = getConnectedEdges(nodesToDelete, edges);
setNodes((nds) => {
return nds.filter((node) => {
return node.data.id !== id
});
})
setNodes((eds) => {
return eds.filter((edge) => {
const idsToDelete = edgeToDelete.map((e) => e.id);
return !idsToDelete.includes(edge.id);
})
})
}
const [nodes, setNodes, onNodesChange] = useNodesState(props.initialNodes.map((node) => {
node.data.onDeleteClick = deleteNode;
return node;
}));
const [edges, setEdges, onEdgesChange] = useEdgesState(
props.initialEdges.map((edge: Edge) => {
// add style.
@@ -113,6 +140,9 @@ const Workflow: FunctionComponent<ComponentProps> = (props: ComponentProps) => {
edgeUpdateSuccessful.current = true;
}, []);
return (
<div className="h-[48rem]">
<ReactFlow
@@ -129,7 +159,7 @@ const Workflow: FunctionComponent<ComponentProps> = (props: ComponentProps) => {
>
<MiniMap />
<Controls />
<Background />
<Background color='#111827' />
</ReactFlow>
</div>
);

View File

@@ -33,7 +33,7 @@ const Delete: FunctionComponent<PageComponentProps> = (
type: 'node',
position: { x: 100, y: 300 },
data: {
id: 'slack-1',
id: 'slack-2',
title: 'Slack',
description: 'Open a channel',
icon: IconProp.Add,
@@ -45,7 +45,7 @@ const Delete: FunctionComponent<PageComponentProps> = (
type: 'addNewNode',
position: { x: 100, y: 500 },
data: {
id: 'slack-1',
id: 'slack-3',
title: 'Slack',
description: 'Open a channel',
icon: IconProp.Add,