fix(react): drag nodes after toggle hidden closes #3931

This commit is contained in:
moklick
2024-02-26 15:01:17 +01:00
parent d88f72a6cc
commit 14516ab061
4 changed files with 7 additions and 11 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
- better cursor defaults for the pane, nodes and edges - better cursor defaults for the pane, nodes and edges
- `disableKeyboardA11y` now also disables Enter and Escape for selecting/deselecting nodes and edges - `disableKeyboardA11y` now also disables Enter and Escape for selecting/deselecting nodes and edges
- fix bug where users couldn't drag a node after toggle nodes `hidden` attribute
## 12.0.0-next.9 ## 12.0.0-next.9
+2 -3
View File
@@ -33,7 +33,6 @@ export function useDrag({
useEffect(() => { useEffect(() => {
if (nodeRef?.current) { if (nodeRef?.current) {
xyDrag.current = XYDrag({ xyDrag.current = XYDrag({
domNode: nodeRef.current,
getStoreItems: () => store.getState(), getStoreItems: () => store.getState(),
onNodeMouseDown: (id: string) => { onNodeMouseDown: (id: string) => {
handleNodeClick({ handleNodeClick({
@@ -55,11 +54,11 @@ export function useDrag({
useEffect(() => { useEffect(() => {
if (disabled) { if (disabled) {
xyDrag.current?.destroy(); xyDrag.current?.destroy();
} else { } else if (nodeRef.current) {
xyDrag.current?.update({ xyDrag.current?.update({
noDragClassName, noDragClassName,
handleSelector, handleSelector,
domNode: nodeRef.current as Element, domNode: nodeRef.current,
isSelectable, isSelectable,
nodeId, nodeId,
}); });
@@ -19,7 +19,6 @@ export type UseDragParams = {
export default function drag(domNode: Element, params: UseDragParams) { export default function drag(domNode: Element, params: UseDragParams) {
const { store, onDrag, onDragStart, onDragStop, onNodeMouseDown } = params; const { store, onDrag, onDragStart, onDragStop, onNodeMouseDown } = params;
const dragInstance = XYDrag({ const dragInstance = XYDrag({
domNode,
onDrag, onDrag,
onDragStart, onDragStart,
onDragStop, onDragStop,
+4 -6
View File
@@ -1,5 +1,5 @@
import { drag } from 'd3-drag'; import { drag } from 'd3-drag';
import { select } from 'd3-selection'; import { select, type Selection } from 'd3-selection';
import { import {
calcAutoPan, calcAutoPan,
@@ -58,7 +58,6 @@ type StoreItems<OnNodeDrag> = {
}; };
export type XYDragParams<OnNodeDrag> = { export type XYDragParams<OnNodeDrag> = {
domNode: Element;
getStoreItems: () => StoreItems<OnNodeDrag>; getStoreItems: () => StoreItems<OnNodeDrag>;
onDragStart?: OnDrag; onDragStart?: OnDrag;
onDrag?: OnDrag; onDrag?: OnDrag;
@@ -81,7 +80,6 @@ export type DragUpdateParams = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => void | undefined>({ export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => void | undefined>({
domNode,
onNodeMouseDown, onNodeMouseDown,
getStoreItems, getStoreItems,
onDragStart, onDragStart,
@@ -95,11 +93,11 @@ export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => voi
let mousePosition: XYPosition = { x: 0, y: 0 }; let mousePosition: XYPosition = { x: 0, y: 0 };
let containerBounds: DOMRect | null = null; let containerBounds: DOMRect | null = null;
let dragStarted = false; let dragStarted = false;
let d3Selection: Selection<Element, unknown, null, undefined> | null = null;
const d3Selection = select(domNode);
// public functions // public functions
function update({ noDragClassName, handleSelector, domNode, isSelectable, nodeId }: DragUpdateParams) { function update({ noDragClassName, handleSelector, domNode, isSelectable, nodeId }: DragUpdateParams) {
d3Selection = select(domNode);
function updateNodes({ x, y }: XYPosition, dragEvent: MouseEvent | null) { function updateNodes({ x, y }: XYPosition, dragEvent: MouseEvent | null) {
const { const {
nodeLookup, nodeLookup,
@@ -342,7 +340,7 @@ export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => voi
} }
function destroy() { function destroy() {
d3Selection.on('.drag', null); d3Selection?.on('.drag', null);
} }
return { return {