refactor(errors): show error when user drags uninitialized node #5014

This commit is contained in:
moklick
2025-02-25 10:52:54 +01:00
parent 54473c74d6
commit 0b67a6c303
6 changed files with 102 additions and 10 deletions
@@ -74,8 +74,8 @@ function ResizeControl({
if (node && node.expandParent && node.parentId) {
const origin = node.origin ?? nodeOrigin;
const width = change.width ?? node.measured.width!;
const height = change.height ?? node.measured.height!;
const width = change.width ?? node.measured.width ?? 0;
const height = change.height ?? node.measured.height ?? 0;
const child: ParentExpandChild = {
id: node.id,
+6 -6
View File
@@ -51,7 +51,7 @@ const createStore = ({
* setNodes() is called exclusively in response to user actions:
* - 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.
*
*
* When this happens, we take the note objects passed by the user and extend them with fields
* relevant for internal React Flow operations.
*/
@@ -161,9 +161,9 @@ const createStore = ({
type: 'position',
position: expandParent
? {
x: Math.max(0, dragItem.position.x),
y: Math.max(0, dragItem.position.y),
}
x: Math.max(0, dragItem.position.x),
y: Math.max(0, dragItem.position.y),
}
: dragItem.position,
dragging,
};
@@ -174,8 +174,8 @@ const createStore = ({
parentId: dragItem.parentId!,
rect: {
...dragItem.internals.positionAbsolute,
width: dragItem.measured.width!,
height: dragItem.measured.height!,
width: dragItem.measured.width ?? 0,
height: dragItem.measured.height ?? 0,
},
});
}
+2
View File
@@ -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.`,
error014: () =>
'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 = [
+6 -2
View File
@@ -428,10 +428,14 @@ export function calculateNodePosition<NodeType extends NodeBase>({
? clampPosition(nextPosition, extent, node.measured)
: nextPosition;
if (node.measured.width === undefined || node.measured.height === undefined) {
onError?.('015', errorMessages['error015']());
}
return {
position: {
x: positionAbsolute.x - parentX + node.measured.width! * origin[0],
y: positionAbsolute.y - parentY + node.measured.height! * origin[1],
x: positionAbsolute.x - parentX + (node.measured.width ?? 0) * origin[0],
y: positionAbsolute.y - parentY + (node.measured.height ?? 0) * origin[1],
},
positionAbsolute,
};