Skip to content

Commit 00d5064

Browse files
authored
Disable Drop Update (#100)
* Solidify DisableDrop * Added Cypress * Finalize tests * Don't provide internal root id to create * Tests Pass * Update Change Log
1 parent 8e421b8 commit 00d5064

27 files changed

+1421
-201
lines changed

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Version 3.0.0
2+
3+
## Features
4+
5+
- Disable Edit
6+
- Disable Drop Dynamically
7+
8+
### Disable Edit
9+
10+
The `disableEdit` prop was added to the tree to specify nodes that cannot be edited. This also fixed a bug when pressing the keyboard shortcut "Enter" on a node that did not render a form. The tree would get stuck in the "editing" mode and could not return to the normal mode.
11+
12+
### Disable Drop Dynamically
13+
14+
The `disableDrop` prop now accepts a function with the arguments described below. Previously you could only provide a static list of nodes that were not droppable, but now you can determine it dynamically.
15+
16+
## Breaking Changes
17+
18+
### Tree Component `disableDrop` Prop
19+
20+
If you were passing a function to the `disableDrop` prop, you'll need to update it to use the following signature:
21+
22+
```ts
23+
declare function disableDrop(args: {
24+
dragNodes: NodeApi[]; // The nodes being dragged
25+
parentNode: NodeApi; // The new parent of the dragNodes if dropped
26+
index: number; // The new child index of the dragNodes if dropped
27+
}): boolean;
28+
```
29+
30+
This lets you disallow a drop based on the items being dragged and which node you are hovering over. You might notice it matches the function signature of the onMove handler. It is still possible to pass a string or a boolean to the `disableDrop` prop to prevent drops statically.
31+
32+
### NodeApi `isDroppable` property
33+
34+
The `.isDroppable` property has been removed from the NodeApi class. This is now determined dynamically from the tree's state. It doesn't make sense to ask an single node if it is droppable anymore.

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,39 +265,48 @@ These are all the props you can pass to the Tree component.
265265
```ts
266266
interface TreeProps<T> {
267267
/* Data Options */
268-
data?: T[];
269-
initialData?: T[];
268+
data?: readonly T[];
269+
initialData?: readonly T[];
270270

271271
/* Data Handlers */
272-
onCreate?: handlers.CreateHandler;
273-
onMove?: handlers.MoveHandler;
274-
onRename?: handlers.RenameHandler;
275-
onDelete?: handlers.DeleteHandler;
272+
onCreate?: handlers.CreateHandler<T>;
273+
onMove?: handlers.MoveHandler<T>;
274+
onRename?: handlers.RenameHandler<T>;
275+
onDelete?: handlers.DeleteHandler<T>;
276276

277277
/* Renderers*/
278278
children?: ElementType<renderers.NodeRendererProps<T>>;
279279
renderRow?: ElementType<renderers.RowRendererProps<T>>;
280280
renderDragPreview?: ElementType<renderers.DragPreviewProps>;
281281
renderCursor?: ElementType<renderers.CursorProps>;
282+
renderContainer?: ElementType<{}>;
282283

283284
/* Sizes */
284285
rowHeight?: number;
285286
overscanCount?: number;
286-
width?: number;
287+
width?: number | string;
287288
height?: number;
288289
indent?: number;
289290
paddingTop?: number;
290291
paddingBottom?: number;
291292
padding?: number;
292293

293294
/* Config */
295+
childrenAccessor?: string | ((d: T) => T[] | null);
296+
idAccessor?: string | ((d: T) => string);
294297
openByDefault?: boolean;
295298
selectionFollowsFocus?: boolean;
296299
disableMultiSelection?: boolean;
300+
disableEdit?: string | boolean | BoolFunc<T>;
297301
disableDrag?: string | boolean | BoolFunc<T>;
298-
disableDrop?: string | boolean | BoolFunc<T>;
299-
childrenAccessor?: string | ((d: T) => T[]);
300-
idAccessor?: string | ((d: T) => string);
302+
disableDrop?:
303+
| string
304+
| boolean
305+
| ((args: {
306+
parentNode: NodeApi<T>;
307+
dragNodes: NodeApi<T>[];
308+
index: number;
309+
}) => boolean);
301310

302311
/* Event Handlers */
303312
onActivate?: (node: NodeApi<T>) => void;
@@ -319,6 +328,7 @@ interface TreeProps<T> {
319328
/* Extra */
320329
className?: string | undefined;
321330
rowClassName?: string | undefined;
331+
322332
dndRootElement?: globalThis.Node | null;
323333
onClick?: MouseEventHandler;
324334
onContextMenu?: MouseEventHandler;

packages/e2e/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# e2e

packages/e2e/cypress.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { defineConfig } = require("cypress");
2+
3+
module.exports = defineConfig({
4+
e2e: {
5+
setupNodeEvents(on, config) {
6+
// implement node event listeners here
7+
},
8+
},
9+
});
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import "@4tw/cypress-drag-drop";
2+
3+
describe("template spec", () => {
4+
beforeEach(() => {
5+
cy.visit("http://localhost:3000/gmail");
6+
cy.get("[role=treeitem]").as("item");
7+
});
8+
9+
it("Edits The Social Node", () => {
10+
cy.get("[role=treeitem").contains("Social").click();
11+
cy.focused().type("{enter}");
12+
cy.focused().type("My Favorite Social Sites{enter}");
13+
cy.get("[role=treeitem]").contains("My Favorite Social Sites");
14+
});
15+
16+
it("Collapses and Expands the Categories", () => {
17+
cy.get("@item").should("have.length", "16");
18+
cy.get("@item").contains("Categories").click();
19+
cy.get("@item").should("have.length", "12");
20+
cy.get("@item").contains("Categories").click();
21+
cy.get("@item").should("have.length", "16");
22+
});
23+
24+
it("Up and Down Arrows", () => {
25+
cy.get("@item").first().click();
26+
cy.focused().type("{downArrow}");
27+
cy.focused().should("contain.text", "Starred");
28+
cy.focused().type("{downArrow}");
29+
cy.focused().should("contain.text", "Snoozed");
30+
cy.focused().type("{upArrow}{upArrow}{upArrow}{upArrow}");
31+
cy.focused().should("contain.text", "Inbox");
32+
});
33+
34+
it("Left and Right Arrows", () => {
35+
cy.get("@item").should("have.length", 16);
36+
cy.get("@item").contains("Categories").click();
37+
cy.focused().type("{leftArrow}");
38+
cy.get("@item").should("have.length", 12);
39+
cy.focused().type("{rightArrow}");
40+
cy.get("@item").should("have.length", 16);
41+
cy.focused().should("contain.text", "Categories");
42+
cy.focused().type("{rightArrow}");
43+
cy.focused().should("contain.text", "Social");
44+
cy.focused().type("{downArrow}");
45+
cy.focused().type("{downArrow}");
46+
cy.focused().should("contain.text", "Forums");
47+
});
48+
49+
it("Creates Leaf Nodes", () => {
50+
// At the root level
51+
cy.get("@item").first().click();
52+
cy.focused().type("a");
53+
cy.focused().type("Turn A New Leaf{enter}");
54+
cy.get("@item").should("have.length", 17);
55+
56+
// In a Folder
57+
cy.get("@item").contains("Social").click();
58+
cy.focused().type("a");
59+
cy.focused().type("Turn More Leaves{enter}");
60+
cy.get("@item").should("have.length", 18);
61+
62+
// On a folder that is closed
63+
cy.get("@item").contains("Categories").click(); // closed it
64+
cy.focused().type("a");
65+
cy.focused().type("Root{enter}");
66+
cy.get("@item").contains("Root").click();
67+
cy.focused().should("have.attr", "aria-level", "0");
68+
69+
// On a folder that is open
70+
cy.get("@item").contains("Categories").click(); // opened it
71+
cy.focused().type("a");
72+
cy.focused().type("Child{enter}");
73+
cy.get("@item").contains("Child").click();
74+
cy.focused().should("have.attr", "aria-level", "1");
75+
});
76+
77+
it("Creates Internal Nodes", () => {
78+
// At the root level
79+
cy.get("@item").first().click();
80+
cy.focused().type("A");
81+
cy.focused().type("Turn A New Internal{enter}");
82+
cy.get("@item").should("have.length", 17);
83+
cy.focused().children().should("have.class", "isInternal");
84+
85+
// In a Folder
86+
cy.get("@item").contains("Social").click();
87+
cy.focused().type("A");
88+
cy.focused().type("Turn More Inernals{enter}");
89+
cy.get("@item").should("have.length", 18);
90+
cy.focused().children().should("have.class", "isInternal");
91+
92+
// On a folder that is closed
93+
cy.get("@item").contains("Categories").click(); // closed it
94+
cy.focused().type("A");
95+
cy.focused().type("Root{enter}");
96+
cy.get("@item").contains("Root").click();
97+
cy.focused().children().should("have.class", "isInternal");
98+
cy.focused().should("have.attr", "aria-level", "0");
99+
100+
// On a folder that is open
101+
cy.get("@item").contains("Categories").click(); // opened it
102+
cy.focused().type("A");
103+
cy.focused().type("Child{enter}");
104+
cy.get("@item").contains("Child").click();
105+
cy.focused().should("have.attr", "aria-level", "1");
106+
});
107+
108+
it("drags and drops in its list", () => {
109+
cy.get("@item")
110+
.contains("Inbox")
111+
.drag("[role=treeitem]:nth-child(5)", "bottom");
112+
113+
cy.get("@item").contains("Inbox").click();
114+
cy.focused().invoke("index").should("eq", 4);
115+
});
116+
117+
it("drags and drops into folder", () => {
118+
cy.get("@item").contains("Starred").drag("[role=treeitem]:nth-child(12)");
119+
120+
cy.get("@item").contains("Starred").click();
121+
cy.focused().invoke("index").should("eq", 11);
122+
});
123+
124+
it("prevents Inbox from Dragging into Categories", () => {
125+
cy.get("@item").contains("Inbox").drag("[role=treeitem]:nth-child(12)");
126+
cy.get("@item").contains("Inbox").click();
127+
cy.focused().invoke("index").should("eq", 0);
128+
});
129+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add('login', (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This will overwrite an existing command --
25+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/e2e.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

packages/e2e/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "e2e",
3+
"packageManager": "[email protected]",
4+
"dependencies": {
5+
"@4tw/cypress-drag-drop": "^2.2.3",
6+
"cypress": "^12.4.1",
7+
"cypress-drag-drop": "^1.1.1",
8+
"typescript": "^4.9.4"
9+
},
10+
"scripts": {
11+
"test": "yarn cypress run",
12+
"open": "yarn cypress open"
13+
}
14+
}

packages/e2e/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"target": "es5",
5+
"lib": ["es5", "dom"],
6+
"types": ["cypress", "node"]
7+
},
8+
"include": ["**/*.ts"]
9+
}

0 commit comments

Comments
 (0)