feat(hooks): useUpdateNodeInternals #916
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
import React, { memo, FC, useMemo, CSSProperties } from 'react';
|
||||||
|
|
||||||
|
import { Handle, Position, NodeProps } from 'react-flow-renderer';
|
||||||
|
|
||||||
|
const nodeStyles: CSSProperties = { padding: 10, border: '1px solid #ddd' };
|
||||||
|
|
||||||
|
const CustomNode: FC<NodeProps> = ({ data }) => {
|
||||||
|
const handles = useMemo(
|
||||||
|
() =>
|
||||||
|
Array.from({ length: data.handleCount }, (x, i) => {
|
||||||
|
const handleId = `handle-${i}`;
|
||||||
|
return <Handle key={handleId} type="source" position={Position.Right} id={handleId} style={{ top: 10 * i }} />;
|
||||||
|
}),
|
||||||
|
[data.handleCount]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={nodeStyles}>
|
||||||
|
<Handle type="target" position={Position.Left} />
|
||||||
|
<div>output handle count: {data.handleCount}</div>
|
||||||
|
{handles}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default memo(CustomNode);
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import React, { useState, useCallback, CSSProperties, useRef } from 'react';
|
||||||
|
|
||||||
|
import ReactFlow, {
|
||||||
|
NodeTypesType,
|
||||||
|
addEdge,
|
||||||
|
useZoomPanHelper,
|
||||||
|
ReactFlowProvider,
|
||||||
|
Elements,
|
||||||
|
Connection,
|
||||||
|
Edge,
|
||||||
|
ElementId,
|
||||||
|
useUpdateNodeInternals,
|
||||||
|
Position,
|
||||||
|
} from 'react-flow-renderer';
|
||||||
|
import CustomNode from './CustomNode';
|
||||||
|
|
||||||
|
const initialHandleCount = 1;
|
||||||
|
|
||||||
|
const initialElements: Elements = [
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'custom',
|
||||||
|
data: { label: 'Node 1', handleCount: initialHandleCount },
|
||||||
|
position: { x: 250, y: 5 },
|
||||||
|
className: 'light',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const buttonWrapperStyles: CSSProperties = { position: 'absolute', right: 10, top: 10, zIndex: 10 };
|
||||||
|
|
||||||
|
const nodeTypes: NodeTypesType = {
|
||||||
|
custom: CustomNode,
|
||||||
|
};
|
||||||
|
|
||||||
|
let id = 5;
|
||||||
|
const getId = (): ElementId => `${id++}`;
|
||||||
|
|
||||||
|
const UpdateNodeInternalsFlow = () => {
|
||||||
|
const handleCount = useRef<number>(initialHandleCount);
|
||||||
|
const [elements, setElements] = useState<Elements>(initialElements);
|
||||||
|
const updateNodeInternals = useUpdateNodeInternals();
|
||||||
|
const onConnect = (params: Connection | Edge) => setElements((els) => addEdge(params, els));
|
||||||
|
const { project } = useZoomPanHelper();
|
||||||
|
|
||||||
|
const onPaneClick = useCallback(
|
||||||
|
(evt) =>
|
||||||
|
setElements((els) =>
|
||||||
|
els.concat({
|
||||||
|
id: getId(),
|
||||||
|
position: project({ x: evt.clientX, y: evt.clientY - 40 }),
|
||||||
|
data: { label: 'new node' },
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
[project]
|
||||||
|
);
|
||||||
|
const toggleHandleCount = useCallback(() => {
|
||||||
|
handleCount.current = handleCount.current === 1 ? 2 : 1;
|
||||||
|
setElements((els) => els.map((el) => ({ ...el, data: { ...el.data, handleCount: handleCount.current } })));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const updateNode = useCallback(() => updateNodeInternals('1'), [updateNodeInternals]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ReactFlow elements={elements} nodeTypes={nodeTypes} onConnect={onConnect} onPaneClick={onPaneClick}>
|
||||||
|
<div style={buttonWrapperStyles}>
|
||||||
|
<button onClick={toggleHandleCount}>toggle handle count</button>
|
||||||
|
<button onClick={updateNode}>update node internals</button>
|
||||||
|
</div>
|
||||||
|
</ReactFlow>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const WrappedFlow = () => (
|
||||||
|
<ReactFlowProvider>
|
||||||
|
<UpdateNodeInternalsFlow />
|
||||||
|
</ReactFlowProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default WrappedFlow;
|
||||||
@@ -22,6 +22,7 @@ import DragNDrop from './DragNDrop';
|
|||||||
import Layout from './Layouting';
|
import Layout from './Layouting';
|
||||||
import SwitchFlows from './Switch';
|
import SwitchFlows from './Switch';
|
||||||
import UseZoomPanHelper from './UseZoomPanHelper';
|
import UseZoomPanHelper from './UseZoomPanHelper';
|
||||||
|
import UseUpdateNodeInternals from './UseUpdateNodeInternals';
|
||||||
|
|
||||||
import './index.css';
|
import './index.css';
|
||||||
|
|
||||||
@@ -106,6 +107,10 @@ const routes = [
|
|||||||
path: '/usezoompanhelper',
|
path: '/usezoompanhelper',
|
||||||
component: UseZoomPanHelper,
|
component: UseZoomPanHelper,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/useupdatenodeinternals',
|
||||||
|
component: UseUpdateNodeInternals,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const Header = withRouter(({ history, location }) => {
|
const Header = withRouter(({ history, location }) => {
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
|
import { useStoreActions } from '../store/hooks';
|
||||||
|
import { ElementId, UpdateNodeInternals } from '../types';
|
||||||
|
|
||||||
|
function useUpdateNodeInternals(): UpdateNodeInternals {
|
||||||
|
const updateNodeDimensions = useStoreActions((actions) => actions.updateNodeDimensions);
|
||||||
|
|
||||||
|
return useCallback<UpdateNodeInternals>((id: ElementId) => {
|
||||||
|
const nodeElement = document.querySelector(`.react-flow__node[data-id="${id}"]`);
|
||||||
|
|
||||||
|
if (nodeElement) {
|
||||||
|
updateNodeDimensions([{ id, nodeElement, forceUpdate: true }]);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useUpdateNodeInternals;
|
||||||
@@ -20,6 +20,7 @@ export {
|
|||||||
getTransformForBounds,
|
getTransformForBounds,
|
||||||
} from './utils/graph';
|
} from './utils/graph';
|
||||||
export { default as useZoomPanHelper } from './hooks/useZoomPanHelper';
|
export { default as useZoomPanHelper } from './hooks/useZoomPanHelper';
|
||||||
|
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
|
||||||
|
|
||||||
export * from './additional-components';
|
export * from './additional-components';
|
||||||
export * from './store/hooks';
|
export * from './store/hooks';
|
||||||
|
|||||||
@@ -425,3 +425,5 @@ export interface ReactFlowState {
|
|||||||
onConnectStop?: OnConnectStopFunc;
|
onConnectStop?: OnConnectStopFunc;
|
||||||
onConnectEnd?: OnConnectEndFunc;
|
onConnectEnd?: OnConnectEndFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type UpdateNodeInternals = (nodeId: ElementId) => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user