refactor(hooks): rename useUpdateNodeData to useSetNodeData and adjust api
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import useReactFlow from './useReactFlow';
|
||||
import { Node } from '../types';
|
||||
|
||||
export function useSetNodeData<NodeType extends Node = Node>() {
|
||||
const { setNodes } = useReactFlow();
|
||||
|
||||
const setNodeData = useCallback(
|
||||
function setNodesData(
|
||||
id: string,
|
||||
dataUpdate: object | ((node: NodeType) => object),
|
||||
options: { replace: boolean } = { replace: true }
|
||||
) {
|
||||
setNodes((prevNodes) =>
|
||||
prevNodes.map((node) => {
|
||||
if (node.id === id) {
|
||||
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node as NodeType) : dataUpdate;
|
||||
return options.replace ? { ...node, data: nextData } : { ...node, data: { ...node.data, ...nextData } };
|
||||
}
|
||||
|
||||
return node;
|
||||
})
|
||||
);
|
||||
},
|
||||
[setNodes]
|
||||
);
|
||||
|
||||
return setNodeData;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import useReactFlow from './useReactFlow';
|
||||
|
||||
export function useUpdateNodeData() {
|
||||
const { setNodes } = useReactFlow();
|
||||
|
||||
const updateNodeData = useCallback((id: string, data: unknown) => {
|
||||
setNodes((prevNodes) => prevNodes.map((node) => (node.id === id ? { ...node, data } : node)));
|
||||
}, []);
|
||||
|
||||
return updateNodeData;
|
||||
}
|
||||
@@ -23,7 +23,7 @@ export { default as useOnViewportChange, type UseOnViewportChangeOptions } from
|
||||
export { default as useOnSelectionChange, type UseOnSelectionChangeOptions } from './hooks/useOnSelectionChange';
|
||||
export { default as useNodesInitialized, type UseNodesInitializedOptions } from './hooks/useNodesInitialized';
|
||||
export { useHandleConnections } from './hooks/useHandleConnections';
|
||||
export { useUpdateNodeData } from './hooks/useUpdateNodeData';
|
||||
export { useSetNodeData } from './hooks/useSetNodeData';
|
||||
export { useNodesData } from './hooks/useNodesData';
|
||||
export { useNodeId } from './contexts/NodeIdContext';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user