Merge pull request #2894 from wbkd/fix/deselect-nodes-focus

fix(nodes): blur when node gets unselected
This commit is contained in:
Moritz Klack
2023-03-06 16:37:50 +01:00
committed by GitHub
4 changed files with 14 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@reactflow/core': patch
---
fix(nodes): blur when node gets unselected
+5 -1
View File
@@ -1,4 +1,4 @@
import { MouseEvent } from 'react'; import { MouseEvent, RefObject } from 'react';
import { StoreApi } from 'zustand'; import { StoreApi } from 'zustand';
import { getDimensions } from '../../utils'; import { getDimensions } from '../../utils';
@@ -58,6 +58,7 @@ export function handleNodeClick({
id, id,
store, store,
unselect = false, unselect = false,
nodeRef,
}: { }: {
id: string; id: string;
store: { store: {
@@ -65,6 +66,7 @@ export function handleNodeClick({
setState: StoreApi<ReactFlowState>['setState']; setState: StoreApi<ReactFlowState>['setState'];
}; };
unselect?: boolean; unselect?: boolean;
nodeRef?: RefObject<HTMLDivElement>;
}) { }) {
const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodeInternals } = store.getState(); const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodeInternals } = store.getState();
const node = nodeInternals.get(id)!; const node = nodeInternals.get(id)!;
@@ -75,5 +77,7 @@ export function handleNodeClick({
addSelectedNodes([id]); addSelectedNodes([id]);
} else if (unselect || (node.selected && multiSelectionActive)) { } else if (unselect || (node.selected && multiSelectionActive)) {
unselectNodesAndEdges({ nodes: [node] }); unselectNodesAndEdges({ nodes: [node] });
requestAnimationFrame(() => nodeRef?.current?.blur());
} }
} }
@@ -74,6 +74,7 @@ export default (NodeComponent: ComponentType<NodeProps>) => {
handleNodeClick({ handleNodeClick({
id, id,
store, store,
nodeRef,
}); });
} }
@@ -90,13 +91,12 @@ export default (NodeComponent: ComponentType<NodeProps>) => {
if (elementSelectionKeys.includes(event.key) && isSelectable) { if (elementSelectionKeys.includes(event.key) && isSelectable) {
const unselect = event.key === 'Escape'; const unselect = event.key === 'Escape';
if (unselect) {
nodeRef.current?.blur();
}
handleNodeClick({ handleNodeClick({
id, id,
store, store,
unselect, unselect,
nodeRef,
}); });
} else if ( } else if (
!disableKeyboardA11y && !disableKeyboardA11y &&
+1
View File
@@ -153,6 +153,7 @@ function useDrag({
handleNodeClick({ handleNodeClick({
id: nodeId, id: nodeId,
store, store,
nodeRef: nodeRef as RefObject<HTMLDivElement>,
}); });
} }