fix(node-click): handle deleted node
This commit is contained in:
@@ -2,6 +2,7 @@ import type { MouseEvent, RefObject } from 'react';
|
|||||||
import type { StoreApi } from 'zustand';
|
import type { StoreApi } from 'zustand';
|
||||||
|
|
||||||
import type { Node, ReactFlowState } from '../../types';
|
import type { Node, ReactFlowState } from '../../types';
|
||||||
|
import { errorMessages } from '@xyflow/system';
|
||||||
|
|
||||||
export function getMouseHandler(
|
export function getMouseHandler(
|
||||||
id: string,
|
id: string,
|
||||||
@@ -34,9 +35,14 @@ export function handleNodeClick({
|
|||||||
unselect?: boolean;
|
unselect?: boolean;
|
||||||
nodeRef?: RefObject<HTMLDivElement>;
|
nodeRef?: RefObject<HTMLDivElement>;
|
||||||
}) {
|
}) {
|
||||||
const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodes } = store.getState();
|
const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodes, onError } = store.getState();
|
||||||
const node = nodes.find((n) => n.id === id)!;
|
const node = nodes.find((n) => n.id === id)!;
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
|
onError?.('012', errorMessages['error012'](id));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
store.setState({ nodesSelectionActive: false });
|
store.setState({ nodesSelectionActive: false });
|
||||||
|
|
||||||
if (!node.selected) {
|
if (!node.selected) {
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export const errorMessages = {
|
|||||||
}", edge id: ${id}.`,
|
}", edge id: ${id}.`,
|
||||||
error010: () => 'Handle: No node id found. Make sure to only use a Handle inside a custom Node.',
|
error010: () => 'Handle: No node id found. Make sure to only use a Handle inside a custom Node.',
|
||||||
error011: (edgeType: string) => `Edge type "${edgeType}" not found. Using fallback type "default".`,
|
error011: (edgeType: string) => `Edge type "${edgeType}" not found. Using fallback type "default".`,
|
||||||
|
error012: (id: string) =>
|
||||||
|
`Node with id "${id}" does not exist, it may have been removed. This can happen when a node is deleted before the "onNodeClick" handler is called.`,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const internalsSymbol = Symbol.for('internals');
|
export const internalsSymbol = Symbol.for('internals');
|
||||||
|
|||||||
Reference in New Issue
Block a user