1- import React , { useCallback , useMemo , useState } from "react" ;
1+ import React , { useCallback , useState } from "react" ;
22import { Meta , StoryFn } from "@storybook/react" ;
3- import { Edge , Node , Position , ReactFlow } from "@xyflow/react" ;
3+ import { addEdge , Edge , OnConnect , Position , ReactFlow , useEdgesState , useNodesState } from "@xyflow/react" ;
44
55import { NodeDefaultV12 } from "../../nodes/NodeDefaultV12" ;
66import { EdgeDefaultV12 , EdgeDefaultV12DataProps as EdgeData } from "../EdgeDefaultV12" ;
7+ import { EdgeDefs } from "../EdgeDefs" ;
78
89import { EdgeLabel , EdgeLabelObject } from "./../../../../../index" ;
910
@@ -30,67 +31,66 @@ export default {
3031const EdgeDefault = ( args : Edge ) => {
3132 const [ reactflowInstance , setReactflowInstance ] = useState ( null ) ;
3233
33- const [ nodes , edges ] = useMemo < [ Node [ ] , Edge [ ] ] > ( ( ) => {
34- return [
35- [
36- {
37- id : args . source ,
38- type : "default" ,
39- data : {
40- label : "Default source" ,
41- content : "Example content." ,
42- minimalShape : "none" ,
43- handles : [
44- {
45- id : args . source ,
46- type : "source" ,
47- tooltip : "this is a source handle" ,
48- position : Position . Right ,
49- onClick : ( params ) => {
50- // eslint-disable-next-line no-console
51- console . log ( "onClick source" , params ) ;
52- } ,
53- } ,
54- ] ,
34+ const [ nodes , , onNodesChange ] = useNodesState ( [
35+ {
36+ id : args . source ,
37+ type : "default" ,
38+ data : {
39+ label : "Default source" ,
40+ content : "Example content." ,
41+ minimalShape : "none" ,
42+ handles : [
43+ {
44+ id : args . source ,
45+ type : "source" ,
46+ tooltip : "this is a source handle" ,
47+ position : Position . Left ,
48+ onClick : ( params ) => {
49+ // eslint-disable-next-line no-console
50+ console . log ( "onClick source" , params ) ;
51+ } ,
5552 } ,
56- position : { x : 50 , y : 0 } ,
57- } ,
58- {
59- id : args . target ,
60- type : "default" ,
61- data : {
62- label : "Default target " ,
63- content : "Example content." ,
64- minimalShape : "none " ,
65- handles : [
66- {
67- id : args . target ,
68- type : "target" ,
69- tooltip : "this is a target handle" ,
70- position : Position . Left ,
71- onClick : ( params ) => {
72- // eslint-disable-next-line no-console
73- console . log ( " onClick target" , params ) ;
74- } ,
75- } ,
76- ] ,
53+ ] ,
54+ } ,
55+ position : { x : 50 , y : 0 } ,
56+ } ,
57+ {
58+ id : args . target ,
59+ type : "default " ,
60+ data : {
61+ label : "Default target " ,
62+ content : "Example content." ,
63+ minimalShape : "none" ,
64+ handles : [
65+ {
66+ id : args . target ,
67+ type : "target" ,
68+ tooltip : "this is a target handle" ,
69+ position : Position . Right ,
70+ onClick : ( params ) => {
71+ // eslint-disable-next-line no-console
72+ console . log ( "onClick target" , params ) ;
73+ } ,
7774 } ,
78- position : { x : 300 , y : 0 } ,
79- } ,
80- ] as Node [ ] ,
81- [
82- {
83- ...args ,
84- sourceX : 150 ,
85- sourceY : 0 ,
86- targetX : 250 ,
87- targetY : 0 ,
88- sourcePosition : Position . Right ,
89- targetPosition : Position . Left ,
90- } ,
91- ] ,
92- ] ;
93- } , [ args ] ) ;
75+ ] ,
76+ } ,
77+ position : { x : 300 , y : 0 } ,
78+ } ,
79+ ] ) ;
80+
81+ const [ edges , setEdges , onEdgesChange ] = useEdgesState ( [
82+ {
83+ ...args ,
84+ // sourceX: 150,
85+ // sourceY: 0,
86+ // targetX: 250,
87+ // targetY: 0,
88+ sourcePosition : Position . Left ,
89+ targetPosition : Position . Right ,
90+ } ,
91+ ] ) ;
92+
93+ const onConnect : OnConnect = useCallback ( ( params ) => setEdges ( ( eds ) => addEdge ( params , eds ) ) , [ ] ) ;
9494
9595 const onLoad = useCallback (
9696 ( rfi ) => {
@@ -103,12 +103,17 @@ const EdgeDefault = (args: Edge) => {
103103
104104 return (
105105 < div style = { { width : "1000px" , height : "800px" } } >
106+ < EdgeDefs />
106107 < ReactFlow
107- defaultNodes = { nodes }
108- defaultEdges = { edges }
108+ nodes = { nodes }
109+ edges = { edges }
109110 onLoad = { onLoad }
110111 edgeTypes = { edgeTypes }
111112 nodeTypes = { nodeTypes }
113+ onNodesChange = { onNodesChange }
114+ onEdgesChange = { onEdgesChange }
115+ onConnect = { onConnect }
116+ fitView
112117 />
113118 </ div >
114119 ) ;
0 commit comments