From b1a0106f066572e2947d3139b35b370de534c696 Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 30 Apr 2021 17:23:03 +0200 Subject: [PATCH] chore(examples): useUpdateNodeInterals handle position --- .../src/UseUpdateNodeInternals/CustomNode.tsx | 12 ++++++-- example/src/UseUpdateNodeInternals/index.tsx | 29 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/example/src/UseUpdateNodeInternals/CustomNode.tsx b/example/src/UseUpdateNodeInternals/CustomNode.tsx index ecae9f4f..969a4c3c 100644 --- a/example/src/UseUpdateNodeInternals/CustomNode.tsx +++ b/example/src/UseUpdateNodeInternals/CustomNode.tsx @@ -9,9 +9,17 @@ const CustomNode: FC = ({ data }) => { () => Array.from({ length: data.handleCount }, (x, i) => { const handleId = `handle-${i}`; - return ; + return ( + + ); }), - [data.handleCount] + [data.handleCount, data.handlePosition] ); return ( diff --git a/example/src/UseUpdateNodeInternals/index.tsx b/example/src/UseUpdateNodeInternals/index.tsx index 8840d338..b0673adc 100644 --- a/example/src/UseUpdateNodeInternals/index.tsx +++ b/example/src/UseUpdateNodeInternals/index.tsx @@ -11,6 +11,7 @@ import ReactFlow, { ElementId, useUpdateNodeInternals, Position, + isEdge, } from 'react-flow-renderer'; import CustomNode from './CustomNode'; @@ -20,7 +21,7 @@ const initialElements: Elements = [ { id: '1', type: 'custom', - data: { label: 'Node 1', handleCount: initialHandleCount }, + data: { label: 'Node 1', handleCount: initialHandleCount, handlePosition: 0 }, position: { x: 250, y: 5 }, }, ]; @@ -35,7 +36,6 @@ let id = 5; const getId = (): ElementId => `${id++}`; const UpdateNodeInternalsFlow = () => { - const handleCount = useRef(initialHandleCount); const [elements, setElements] = useState(initialElements); const updateNodeInternals = useUpdateNodeInternals(); const onConnect = (params: Connection | Edge) => setElements((els) => addEdge(params, els)); @@ -54,9 +54,29 @@ const UpdateNodeInternalsFlow = () => { ), [project] ); + const toggleHandleCount = useCallback(() => { - handleCount.current = handleCount.current === 1 ? 2 : 1; - setElements((els) => els.map((el) => ({ ...el, data: { ...el.data, handleCount: handleCount.current } }))); + setElements((els) => + els.map((el) => { + if (isEdge(el)) { + return el; + } + + return { ...el, data: { ...el.data, handleCount: el.data?.handleCount === 1 ? 2 : 1 } }; + }) + ); + }, []); + + const toggleHandlePosition = useCallback(() => { + setElements((els) => + els.map((el) => { + if (isEdge(el)) { + return el; + } + + return { ...el, data: { ...el.data, handlePosition: el.data?.handlePosition === 0 ? 1 : 0 } }; + }) + ); }, []); const updateNode = useCallback(() => updateNodeInternals('1'), [updateNodeInternals]); @@ -65,6 +85,7 @@ const UpdateNodeInternalsFlow = () => {
+