react-arborist 3.10.2 → 3.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main/dnd/drag-hook.d.ts +1 -0
- package/dist/main/dnd/drag-hook.js +8 -1
- package/dist/main/dnd/drag-hook.test.js +14 -0
- package/dist/module/dnd/drag-hook.d.ts +1 -0
- package/dist/module/dnd/drag-hook.js +7 -1
- package/dist/module/dnd/drag-hook.test.js +15 -1
- package/package.json +1 -1
- package/src/dnd/drag-hook.test.ts +19 -1
- package/src/dnd/drag-hook.ts +8 -1
|
@@ -2,4 +2,5 @@ import { ConnectDragSource } from "react-dnd";
|
|
|
2
2
|
import { NodeApi } from "../interfaces/node-api";
|
|
3
3
|
import { TreeProps } from "../types/tree-props";
|
|
4
4
|
export declare function dragTypeForNode<T>(dragType: TreeProps<T>["dragType"], node: NodeApi<T>): string;
|
|
5
|
+
export declare function canDragNode<T>(node: NodeApi<T>): boolean;
|
|
5
6
|
export declare function useDragHook<T>(node: NodeApi<T>): ConnectDragSource;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dragTypeForNode = dragTypeForNode;
|
|
4
|
+
exports.canDragNode = canDragNode;
|
|
4
5
|
exports.useDragHook = useDragHook;
|
|
5
6
|
const react_1 = require("react");
|
|
6
7
|
const react_dnd_1 = require("react-dnd");
|
|
@@ -14,11 +15,17 @@ function dragTypeForNode(dragType, node) {
|
|
|
14
15
|
return dragType(node);
|
|
15
16
|
return dragType !== null && dragType !== void 0 ? dragType : "NODE";
|
|
16
17
|
}
|
|
18
|
+
/* A node can start a drag only when it's draggable and not currently being
|
|
19
|
+
renamed. Without the editing guard, dragging inside the rename input would
|
|
20
|
+
pick the row up and move it (issue #195). */
|
|
21
|
+
function canDragNode(node) {
|
|
22
|
+
return node.isDraggable && !node.isEditing;
|
|
23
|
+
}
|
|
17
24
|
function useDragHook(node) {
|
|
18
25
|
const tree = (0, context_1.useTreeApi)();
|
|
19
26
|
const ids = tree.selectedIds;
|
|
20
27
|
const [_, ref, preview] = (0, react_dnd_1.useDrag)(() => ({
|
|
21
|
-
canDrag: () => node
|
|
28
|
+
canDrag: () => canDragNode(node),
|
|
22
29
|
type: dragTypeForNode(tree.props.dragType, node),
|
|
23
30
|
item: () => {
|
|
24
31
|
// This is fired once at the beginning of a drag operation
|
|
@@ -17,3 +17,17 @@ test("resolves a per-node dragType function against the node", () => {
|
|
|
17
17
|
expect((0, drag_hook_1.dragTypeForNode)(dragType, nodeWith({ kind: "folder" }))).toBe("FOLDER");
|
|
18
18
|
expect((0, drag_hook_1.dragTypeForNode)(dragType, nodeWith({ kind: "file" }))).toBe("FILE");
|
|
19
19
|
});
|
|
20
|
+
/* canDragNode only reads the isDraggable/isEditing flags, so a minimal stub
|
|
21
|
+
stands in for a real NodeApi. */
|
|
22
|
+
function draggableNode(flags) {
|
|
23
|
+
return flags;
|
|
24
|
+
}
|
|
25
|
+
test("a draggable node that isn't being edited can drag", () => {
|
|
26
|
+
expect((0, drag_hook_1.canDragNode)(draggableNode({ isDraggable: true, isEditing: false }))).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
test("a non-draggable node can't drag", () => {
|
|
29
|
+
expect((0, drag_hook_1.canDragNode)(draggableNode({ isDraggable: false, isEditing: false }))).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
test("a node being renamed can't drag, even when draggable (#195)", () => {
|
|
32
|
+
expect((0, drag_hook_1.canDragNode)(draggableNode({ isDraggable: true, isEditing: true }))).toBe(false);
|
|
33
|
+
});
|
|
@@ -2,4 +2,5 @@ import { ConnectDragSource } from "react-dnd";
|
|
|
2
2
|
import { NodeApi } from "../interfaces/node-api";
|
|
3
3
|
import { TreeProps } from "../types/tree-props";
|
|
4
4
|
export declare function dragTypeForNode<T>(dragType: TreeProps<T>["dragType"], node: NodeApi<T>): string;
|
|
5
|
+
export declare function canDragNode<T>(node: NodeApi<T>): boolean;
|
|
5
6
|
export declare function useDragHook<T>(node: NodeApi<T>): ConnectDragSource;
|
|
@@ -10,11 +10,17 @@ export function dragTypeForNode(dragType, node) {
|
|
|
10
10
|
return dragType(node);
|
|
11
11
|
return dragType !== null && dragType !== void 0 ? dragType : "NODE";
|
|
12
12
|
}
|
|
13
|
+
/* A node can start a drag only when it's draggable and not currently being
|
|
14
|
+
renamed. Without the editing guard, dragging inside the rename input would
|
|
15
|
+
pick the row up and move it (issue #195). */
|
|
16
|
+
export function canDragNode(node) {
|
|
17
|
+
return node.isDraggable && !node.isEditing;
|
|
18
|
+
}
|
|
13
19
|
export function useDragHook(node) {
|
|
14
20
|
const tree = useTreeApi();
|
|
15
21
|
const ids = tree.selectedIds;
|
|
16
22
|
const [_, ref, preview] = useDrag(() => ({
|
|
17
|
-
canDrag: () => node
|
|
23
|
+
canDrag: () => canDragNode(node),
|
|
18
24
|
type: dragTypeForNode(tree.props.dragType, node),
|
|
19
25
|
item: () => {
|
|
20
26
|
// This is fired once at the beginning of a drag operation
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { dragTypeForNode } from "./drag-hook";
|
|
1
|
+
import { canDragNode, dragTypeForNode } from "./drag-hook";
|
|
2
2
|
/* dragTypeForNode only reads node.data when dragType is a function, so a
|
|
3
3
|
minimal stub stands in for a real NodeApi. */
|
|
4
4
|
function nodeWith(data) {
|
|
@@ -15,3 +15,17 @@ test("resolves a per-node dragType function against the node", () => {
|
|
|
15
15
|
expect(dragTypeForNode(dragType, nodeWith({ kind: "folder" }))).toBe("FOLDER");
|
|
16
16
|
expect(dragTypeForNode(dragType, nodeWith({ kind: "file" }))).toBe("FILE");
|
|
17
17
|
});
|
|
18
|
+
/* canDragNode only reads the isDraggable/isEditing flags, so a minimal stub
|
|
19
|
+
stands in for a real NodeApi. */
|
|
20
|
+
function draggableNode(flags) {
|
|
21
|
+
return flags;
|
|
22
|
+
}
|
|
23
|
+
test("a draggable node that isn't being edited can drag", () => {
|
|
24
|
+
expect(canDragNode(draggableNode({ isDraggable: true, isEditing: false }))).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
test("a non-draggable node can't drag", () => {
|
|
27
|
+
expect(canDragNode(draggableNode({ isDraggable: false, isEditing: false }))).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
test("a node being renamed can't drag, even when draggable (#195)", () => {
|
|
30
|
+
expect(canDragNode(draggableNode({ isDraggable: true, isEditing: true }))).toBe(false);
|
|
31
|
+
});
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NodeApi } from "../interfaces/node-api";
|
|
2
|
-
import { dragTypeForNode } from "./drag-hook";
|
|
2
|
+
import { canDragNode, dragTypeForNode } from "./drag-hook";
|
|
3
3
|
|
|
4
4
|
/* dragTypeForNode only reads node.data when dragType is a function, so a
|
|
5
5
|
minimal stub stands in for a real NodeApi. */
|
|
@@ -20,3 +20,21 @@ test("resolves a per-node dragType function against the node", () => {
|
|
|
20
20
|
expect(dragTypeForNode(dragType, nodeWith({ kind: "folder" }))).toBe("FOLDER");
|
|
21
21
|
expect(dragTypeForNode(dragType, nodeWith({ kind: "file" }))).toBe("FILE");
|
|
22
22
|
});
|
|
23
|
+
|
|
24
|
+
/* canDragNode only reads the isDraggable/isEditing flags, so a minimal stub
|
|
25
|
+
stands in for a real NodeApi. */
|
|
26
|
+
function draggableNode(flags: { isDraggable: boolean; isEditing: boolean }): NodeApi {
|
|
27
|
+
return flags as NodeApi;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test("a draggable node that isn't being edited can drag", () => {
|
|
31
|
+
expect(canDragNode(draggableNode({ isDraggable: true, isEditing: false }))).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("a non-draggable node can't drag", () => {
|
|
35
|
+
expect(canDragNode(draggableNode({ isDraggable: false, isEditing: false }))).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a node being renamed can't drag, even when draggable (#195)", () => {
|
|
39
|
+
expect(canDragNode(draggableNode({ isDraggable: true, isEditing: true }))).toBe(false);
|
|
40
|
+
});
|
package/src/dnd/drag-hook.ts
CHANGED
|
@@ -15,12 +15,19 @@ export function dragTypeForNode<T>(dragType: TreeProps<T>["dragType"], node: Nod
|
|
|
15
15
|
return dragType ?? "NODE";
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/* A node can start a drag only when it's draggable and not currently being
|
|
19
|
+
renamed. Without the editing guard, dragging inside the rename input would
|
|
20
|
+
pick the row up and move it (issue #195). */
|
|
21
|
+
export function canDragNode<T>(node: NodeApi<T>): boolean {
|
|
22
|
+
return node.isDraggable && !node.isEditing;
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
|
|
19
26
|
const tree = useTreeApi<T>();
|
|
20
27
|
const ids = tree.selectedIds;
|
|
21
28
|
const [_, ref, preview] = useDrag<DragItem<T>, DropResult, void>(
|
|
22
29
|
() => ({
|
|
23
|
-
canDrag: () => node
|
|
30
|
+
canDrag: () => canDragNode(node),
|
|
24
31
|
type: dragTypeForNode(tree.props.dragType, node),
|
|
25
32
|
item: () => {
|
|
26
33
|
// This is fired once at the beginning of a drag operation
|