Skip to content

Commit

Permalink
refactor(useUpdateNodeInternals): make it easier to use #2008
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Aug 16, 2022
1 parent bb00e87 commit cb69897
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 56 deletions.
33 changes: 18 additions & 15 deletions example/src/UseUpdateNodeInternals/CustomNode.tsx
@@ -1,30 +1,33 @@
import React, { memo, FC, useMemo, CSSProperties } from 'react';
import { Handle, Position, NodeProps } from 'react-flow-renderer';
import React, { memo, FC, useMemo, CSSProperties, useState } from 'react';
import { Handle, Position, NodeProps, useUpdateNodeInternals } from 'react-flow-renderer';

const nodeStyles: CSSProperties = { padding: 10, border: '1px solid #ddd' };

const CustomNode: FC<NodeProps> = ({ data }) => {
const CustomNode: FC<NodeProps> = ({ id }) => {
const [handleCount, setHandleCount] = useState(1);
const updateNodeInternals = useUpdateNodeInternals();

const handles = useMemo(
() =>
Array.from({ length: data.handleCount }, (x, i) => {
Array.from({ length: handleCount }, (x, i) => {
const handleId = `handle-${i}`;
return (
<Handle
key={handleId}
type="source"
position={Position.Right}
id={handleId}
style={{ top: 10 * i + data.handlePosition * 10 }}
/>
);
return <Handle key={handleId} type="source" position={Position.Right} id={handleId} style={{ top: 10 * i }} />;
}),
[data.handleCount, data.handlePosition]
[handleCount]
);

return (
<div style={nodeStyles}>
<Handle type="target" position={Position.Left} />
<div>output handle count: {data.handleCount}</div>
<div>output handle count: {handleCount}</div>
<button
onClick={() => {
setHandleCount((c) => c + 1);
updateNodeInternals(id);
}}
>
add handle
</button>
{handles}
</div>
);
Expand Down
37 changes: 3 additions & 34 deletions example/src/UseUpdateNodeInternals/index.tsx
@@ -1,4 +1,4 @@
import { useCallback, CSSProperties, MouseEvent } from 'react';
import { useCallback, MouseEvent } from 'react';

import ReactFlow, {
NodeTypes,
Expand All @@ -8,26 +8,21 @@ import ReactFlow, {
Node,
Connection,
Edge,
useUpdateNodeInternals,
Position,
useNodesState,
useEdgesState,
} from 'react-flow-renderer';
import CustomNode from './CustomNode';

const initialHandleCount = 1;

const initialNodes: Node[] = [
{
id: '1',
type: 'custom',
data: { label: 'Node 1', handleCount: initialHandleCount, handlePosition: 0 },
data: { label: 'Node 1' },
position: { x: 250, y: 5 },
},
];

const buttonWrapperStyles: CSSProperties = { position: 'absolute', right: 10, top: 10, zIndex: 10 };

const nodeTypes: NodeTypes = {
custom: CustomNode,
};
Expand All @@ -40,7 +35,6 @@ const UpdateNodeInternalsFlow = () => {
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
const onConnect = (params: Edge | Connection) => setEdges((els) => addEdge(params, els));

const updateNodeInternals = useUpdateNodeInternals();
const { project } = useReactFlow();

const onPaneClick = useCallback(
Expand All @@ -56,25 +50,6 @@ const UpdateNodeInternalsFlow = () => {
),
[project, setNodes]
);

const toggleHandleCount = useCallback(() => {
setNodes((nds) =>
nds.map((node) => {
return { ...node, data: { ...node.data, handleCount: node.data?.handleCount === 1 ? 2 : 1 } };
})
);
}, [setNodes]);

const toggleHandlePosition = useCallback(() => {
setNodes((nds) =>
nds.map((node) => {
return { ...node, data: { ...node.data, handlePosition: node.data?.handlePosition === 0 ? 1 : 0 } };
})
);
}, [setNodes]);

const updateNode = useCallback(() => updateNodeInternals('1'), [updateNodeInternals]);

return (
<ReactFlow
nodes={nodes}
Expand All @@ -84,13 +59,7 @@ const UpdateNodeInternalsFlow = () => {
nodeTypes={nodeTypes}
onConnect={onConnect}
onPaneClick={onPaneClick}
>
<div style={buttonWrapperStyles}>
<button onClick={toggleHandleCount}>toggle handle count</button>
<button onClick={toggleHandlePosition}>toggle handle position</button>
<button onClick={updateNode}>update node internals</button>
</div>
</ReactFlow>
/>
);
};

Expand Down
9 changes: 2 additions & 7 deletions src/hooks/useUpdateNodeInternals.ts
Expand Up @@ -10,15 +10,10 @@ function useUpdateNodeInternals(): UpdateNodeInternals {
const updateNodeDimensions = useStore(selector);

return useCallback<UpdateNodeInternals>((id: string) => {
const { domNode } = store.getState();
if (!domNode) {
return;
}

const nodeElement = domNode.querySelector(`.react-flow__node[data-id="${id}"]`) as HTMLDivElement;
const nodeElement = store.getState().domNode?.querySelector(`.react-flow__node[data-id="${id}"]`) as HTMLDivElement;

if (nodeElement) {
updateNodeDimensions([{ id, nodeElement, forceUpdate: true }]);
requestAnimationFrame(() => updateNodeDimensions([{ id, nodeElement, forceUpdate: true }]));
}
}, []);
}
Expand Down

0 comments on commit cb69897

Please sign in to comment.