Merge pull request #5052 from xyflow/refactor/nodes-init-warning
Refactor/nodes init warning
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
'@xyflow/react': patch
|
||||||
|
'@xyflow/system': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Show an error if user drags uninitialized node
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import Basic from '../examples/Basic';
|
import Basic from '../examples/Basic';
|
||||||
import Backgrounds from '../examples/Backgrounds';
|
import Backgrounds from '../examples/Backgrounds';
|
||||||
|
import BrokenNodes from '../examples/BrokenNodes';
|
||||||
import ColorMode from '../examples/ColorMode';
|
import ColorMode from '../examples/ColorMode';
|
||||||
import ClickDistance from '../examples/ClickDistance';
|
import ClickDistance from '../examples/ClickDistance';
|
||||||
import ControlledUncontrolled from '../examples/ControlledUncontrolled';
|
import ControlledUncontrolled from '../examples/ControlledUncontrolled';
|
||||||
@@ -77,6 +78,11 @@ const routes: IRoute[] = [
|
|||||||
path: 'backgrounds',
|
path: 'backgrounds',
|
||||||
component: Backgrounds,
|
component: Backgrounds,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Broken Nodes',
|
||||||
|
path: 'broken-nodes',
|
||||||
|
component: BrokenNodes,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'Color Mode',
|
name: 'Color Mode',
|
||||||
path: 'color-mode',
|
path: 'color-mode',
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import { useCallback, useState } from 'react';
|
||||||
|
import { ReactFlow, addEdge, Node, Connection, Edge, OnNodeDrag } from '@xyflow/react';
|
||||||
|
|
||||||
|
const nodesInit: Node[] = [
|
||||||
|
{
|
||||||
|
id: '1a',
|
||||||
|
type: 'input',
|
||||||
|
data: { label: 'Node 1' },
|
||||||
|
position: { x: 250, y: 5 },
|
||||||
|
className: 'light',
|
||||||
|
ariaLabel: 'Input Node 1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2a',
|
||||||
|
data: { label: 'Node 2' },
|
||||||
|
position: { x: 100, y: 100 },
|
||||||
|
className: 'light',
|
||||||
|
ariaLabel: 'Default Node 2',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3a',
|
||||||
|
data: { label: 'Node 3' },
|
||||||
|
position: { x: 400, y: 100 },
|
||||||
|
className: 'light',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '4a',
|
||||||
|
data: { label: 'Node 4' },
|
||||||
|
position: { x: 400, y: 200 },
|
||||||
|
className: 'light',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const edgesInit: Edge[] = [
|
||||||
|
{ id: 'e1-2', source: '1a', target: '2a', ariaLabel: undefined },
|
||||||
|
{ id: 'e1-3', source: '1a', target: '3a' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const onNodesChange = () => {};
|
||||||
|
const onEdgesChange = () => {};
|
||||||
|
const BasicFlow = () => {
|
||||||
|
const [nodes, setNodes] = useState(nodesInit);
|
||||||
|
const [edges, setEdges] = useState(edgesInit);
|
||||||
|
|
||||||
|
const onConnect = useCallback((params: Connection | Edge) => setEdges((eds) => addEdge(params, eds)), [setEdges]);
|
||||||
|
|
||||||
|
const onNodeDrag: OnNodeDrag = useCallback((e, node) => {
|
||||||
|
if (isNaN(node.position.x) || isNaN(node.position.y)) {
|
||||||
|
console.log('received NaN', node.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
setNodes((nds) => {
|
||||||
|
return nds.map((item) => {
|
||||||
|
if (item.id === node.id) {
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
position: {
|
||||||
|
x: node.position.x,
|
||||||
|
y: node.position.y,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ReactFlow
|
||||||
|
nodes={nodes}
|
||||||
|
edges={edges}
|
||||||
|
onNodesChange={onNodesChange}
|
||||||
|
onEdgesChange={onEdgesChange}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onNodeDrag={onNodeDrag}
|
||||||
|
></ReactFlow>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default BasicFlow;
|
||||||
@@ -74,8 +74,8 @@ function ResizeControl({
|
|||||||
|
|
||||||
if (node && node.expandParent && node.parentId) {
|
if (node && node.expandParent && node.parentId) {
|
||||||
const origin = node.origin ?? nodeOrigin;
|
const origin = node.origin ?? nodeOrigin;
|
||||||
const width = change.width ?? node.measured.width!;
|
const width = change.width ?? node.measured.width ?? 0;
|
||||||
const height = change.height ?? node.measured.height!;
|
const height = change.height ?? node.measured.height ?? 0;
|
||||||
|
|
||||||
const child: ParentExpandChild = {
|
const child: ParentExpandChild = {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ const createStore = ({
|
|||||||
* setNodes() is called exclusively in response to user actions:
|
* setNodes() is called exclusively in response to user actions:
|
||||||
* - either when the `<ReactFlow nodes>` prop is updated in the controlled ReactFlow setup,
|
* - either when the `<ReactFlow nodes>` prop is updated in the controlled ReactFlow setup,
|
||||||
* - or when the user calls something like `reactFlowInstance.setNodes()` in an uncontrolled ReactFlow setup.
|
* - or when the user calls something like `reactFlowInstance.setNodes()` in an uncontrolled ReactFlow setup.
|
||||||
*
|
*
|
||||||
* When this happens, we take the note objects passed by the user and extend them with fields
|
* When this happens, we take the note objects passed by the user and extend them with fields
|
||||||
* relevant for internal React Flow operations.
|
* relevant for internal React Flow operations.
|
||||||
*/
|
*/
|
||||||
@@ -161,9 +161,9 @@ const createStore = ({
|
|||||||
type: 'position',
|
type: 'position',
|
||||||
position: expandParent
|
position: expandParent
|
||||||
? {
|
? {
|
||||||
x: Math.max(0, dragItem.position.x),
|
x: Math.max(0, dragItem.position.x),
|
||||||
y: Math.max(0, dragItem.position.y),
|
y: Math.max(0, dragItem.position.y),
|
||||||
}
|
}
|
||||||
: dragItem.position,
|
: dragItem.position,
|
||||||
dragging,
|
dragging,
|
||||||
};
|
};
|
||||||
@@ -174,8 +174,8 @@ const createStore = ({
|
|||||||
parentId: dragItem.parentId!,
|
parentId: dragItem.parentId!,
|
||||||
rect: {
|
rect: {
|
||||||
...dragItem.internals.positionAbsolute,
|
...dragItem.internals.positionAbsolute,
|
||||||
width: dragItem.measured.width!,
|
width: dragItem.measured.width ?? 0,
|
||||||
height: dragItem.measured.height!,
|
height: dragItem.measured.height ?? 0,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ export const errorMessages = {
|
|||||||
`It seems that you haven't loaded the styles. Please import '@xyflow/${lib}/dist/style.css' or base.css to make sure everything is working properly.`,
|
`It seems that you haven't loaded the styles. Please import '@xyflow/${lib}/dist/style.css' or base.css to make sure everything is working properly.`,
|
||||||
error014: () =>
|
error014: () =>
|
||||||
'useNodeConnections: No node ID found. Call useNodeConnections inside a custom Node or provide a node ID.',
|
'useNodeConnections: No node ID found. Call useNodeConnections inside a custom Node or provide a node ID.',
|
||||||
|
error015: () =>
|
||||||
|
'It seems that you are trying to drag a node that is not initialized. Please use onNodesChange as explained in the docs.',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const infiniteExtent: CoordinateExtent = [
|
export const infiniteExtent: CoordinateExtent = [
|
||||||
|
|||||||
@@ -428,10 +428,14 @@ export function calculateNodePosition<NodeType extends NodeBase>({
|
|||||||
? clampPosition(nextPosition, extent, node.measured)
|
? clampPosition(nextPosition, extent, node.measured)
|
||||||
: nextPosition;
|
: nextPosition;
|
||||||
|
|
||||||
|
if (node.measured.width === undefined || node.measured.height === undefined) {
|
||||||
|
onError?.('015', errorMessages['error015']());
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
position: {
|
position: {
|
||||||
x: positionAbsolute.x - parentX + node.measured.width! * origin[0],
|
x: positionAbsolute.x - parentX + (node.measured.width ?? 0) * origin[0],
|
||||||
y: positionAbsolute.y - parentY + node.measured.height! * origin[1],
|
y: positionAbsolute.y - parentY + (node.measured.height ?? 0) * origin[1],
|
||||||
},
|
},
|
||||||
positionAbsolute,
|
positionAbsolute,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user