Skip to content

Commit

Permalink
Disable Drop Update (#100)
Browse files Browse the repository at this point in the history
* Solidify DisableDrop

* Added Cypress

* Finalize tests

* Don't provide internal root id to create

* Tests Pass

* Update Change Log
  • Loading branch information
jameskerr authored Jan 27, 2023
1 parent 8e421b8 commit 00d5064
Show file tree
Hide file tree
Showing 27 changed files with 1,421 additions and 201 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Version 3.0.0

## Features

- Disable Edit
- Disable Drop Dynamically

### Disable Edit

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.

### Disable Drop Dynamically

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.

## Breaking Changes

### Tree Component `disableDrop` Prop

If you were passing a function to the `disableDrop` prop, you'll need to update it to use the following signature:

```ts
declare function disableDrop(args: {
dragNodes: NodeApi[]; // The nodes being dragged
parentNode: NodeApi; // The new parent of the dragNodes if dropped
index: number; // The new child index of the dragNodes if dropped
}): boolean;
```

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.

### NodeApi `isDroppable` property

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.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,48 @@ These are all the props you can pass to the Tree component.
```ts
interface TreeProps<T> {
/* Data Options */
data?: T[];
initialData?: T[];
data?: readonly T[];
initialData?: readonly T[];

/* Data Handlers */
onCreate?: handlers.CreateHandler;
onMove?: handlers.MoveHandler;
onRename?: handlers.RenameHandler;
onDelete?: handlers.DeleteHandler;
onCreate?: handlers.CreateHandler<T>;
onMove?: handlers.MoveHandler<T>;
onRename?: handlers.RenameHandler<T>;
onDelete?: handlers.DeleteHandler<T>;

/* Renderers*/
children?: ElementType<renderers.NodeRendererProps<T>>;
renderRow?: ElementType<renderers.RowRendererProps<T>>;
renderDragPreview?: ElementType<renderers.DragPreviewProps>;
renderCursor?: ElementType<renderers.CursorProps>;
renderContainer?: ElementType<{}>;

/* Sizes */
rowHeight?: number;
overscanCount?: number;
width?: number;
width?: number | string;
height?: number;
indent?: number;
paddingTop?: number;
paddingBottom?: number;
padding?: number;

/* Config */
childrenAccessor?: string | ((d: T) => T[] | null);
idAccessor?: string | ((d: T) => string);
openByDefault?: boolean;
selectionFollowsFocus?: boolean;
disableMultiSelection?: boolean;
disableEdit?: string | boolean | BoolFunc<T>;
disableDrag?: string | boolean | BoolFunc<T>;
disableDrop?: string | boolean | BoolFunc<T>;
childrenAccessor?: string | ((d: T) => T[]);
idAccessor?: string | ((d: T) => string);
disableDrop?:
| string
| boolean
| ((args: {
parentNode: NodeApi<T>;
dragNodes: NodeApi<T>[];
index: number;
}) => boolean);

/* Event Handlers */
onActivate?: (node: NodeApi<T>) => void;
Expand All @@ -319,6 +328,7 @@ interface TreeProps<T> {
/* Extra */
className?: string | undefined;
rowClassName?: string | undefined;

dndRootElement?: globalThis.Node | null;
onClick?: MouseEventHandler;
onContextMenu?: MouseEventHandler;
Expand Down
1 change: 1 addition & 0 deletions packages/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# e2e
9 changes: 9 additions & 0 deletions packages/e2e/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
129 changes: 129 additions & 0 deletions packages/e2e/cypress/e2e/spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import "@4tw/cypress-drag-drop";

describe("template spec", () => {
beforeEach(() => {
cy.visit("http://localhost:3000/gmail");
cy.get("[role=treeitem]").as("item");
});

it("Edits The Social Node", () => {
cy.get("[role=treeitem").contains("Social").click();
cy.focused().type("{enter}");
cy.focused().type("My Favorite Social Sites{enter}");
cy.get("[role=treeitem]").contains("My Favorite Social Sites");
});

it("Collapses and Expands the Categories", () => {
cy.get("@item").should("have.length", "16");
cy.get("@item").contains("Categories").click();
cy.get("@item").should("have.length", "12");
cy.get("@item").contains("Categories").click();
cy.get("@item").should("have.length", "16");
});

it("Up and Down Arrows", () => {
cy.get("@item").first().click();
cy.focused().type("{downArrow}");
cy.focused().should("contain.text", "Starred");
cy.focused().type("{downArrow}");
cy.focused().should("contain.text", "Snoozed");
cy.focused().type("{upArrow}{upArrow}{upArrow}{upArrow}");
cy.focused().should("contain.text", "Inbox");
});

it("Left and Right Arrows", () => {
cy.get("@item").should("have.length", 16);
cy.get("@item").contains("Categories").click();
cy.focused().type("{leftArrow}");
cy.get("@item").should("have.length", 12);
cy.focused().type("{rightArrow}");
cy.get("@item").should("have.length", 16);
cy.focused().should("contain.text", "Categories");
cy.focused().type("{rightArrow}");
cy.focused().should("contain.text", "Social");
cy.focused().type("{downArrow}");
cy.focused().type("{downArrow}");
cy.focused().should("contain.text", "Forums");
});

it("Creates Leaf Nodes", () => {
// At the root level
cy.get("@item").first().click();
cy.focused().type("a");
cy.focused().type("Turn A New Leaf{enter}");
cy.get("@item").should("have.length", 17);

// In a Folder
cy.get("@item").contains("Social").click();
cy.focused().type("a");
cy.focused().type("Turn More Leaves{enter}");
cy.get("@item").should("have.length", 18);

// On a folder that is closed
cy.get("@item").contains("Categories").click(); // closed it
cy.focused().type("a");
cy.focused().type("Root{enter}");
cy.get("@item").contains("Root").click();
cy.focused().should("have.attr", "aria-level", "0");

// On a folder that is open
cy.get("@item").contains("Categories").click(); // opened it
cy.focused().type("a");
cy.focused().type("Child{enter}");
cy.get("@item").contains("Child").click();
cy.focused().should("have.attr", "aria-level", "1");
});

it("Creates Internal Nodes", () => {
// At the root level
cy.get("@item").first().click();
cy.focused().type("A");
cy.focused().type("Turn A New Internal{enter}");
cy.get("@item").should("have.length", 17);
cy.focused().children().should("have.class", "isInternal");

// In a Folder
cy.get("@item").contains("Social").click();
cy.focused().type("A");
cy.focused().type("Turn More Inernals{enter}");
cy.get("@item").should("have.length", 18);
cy.focused().children().should("have.class", "isInternal");

// On a folder that is closed
cy.get("@item").contains("Categories").click(); // closed it
cy.focused().type("A");
cy.focused().type("Root{enter}");
cy.get("@item").contains("Root").click();
cy.focused().children().should("have.class", "isInternal");
cy.focused().should("have.attr", "aria-level", "0");

// On a folder that is open
cy.get("@item").contains("Categories").click(); // opened it
cy.focused().type("A");
cy.focused().type("Child{enter}");
cy.get("@item").contains("Child").click();
cy.focused().should("have.attr", "aria-level", "1");
});

it("drags and drops in its list", () => {
cy.get("@item")
.contains("Inbox")
.drag("[role=treeitem]:nth-child(5)", "bottom");

cy.get("@item").contains("Inbox").click();
cy.focused().invoke("index").should("eq", 4);
});

it("drags and drops into folder", () => {
cy.get("@item").contains("Starred").drag("[role=treeitem]:nth-child(12)");

cy.get("@item").contains("Starred").click();
cy.focused().invoke("index").should("eq", 11);
});

it("prevents Inbox from Dragging into Categories", () => {
cy.get("@item").contains("Inbox").drag("[role=treeitem]:nth-child(12)");
cy.get("@item").contains("Inbox").click();
cy.focused().invoke("index").should("eq", 0);
});
});
5 changes: 5 additions & 0 deletions packages/e2e/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
25 changes: 25 additions & 0 deletions packages/e2e/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions packages/e2e/cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
14 changes: 14 additions & 0 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "e2e",
"packageManager": "[email protected]",
"dependencies": {
"@4tw/cypress-drag-drop": "^2.2.3",
"cypress": "^12.4.1",
"cypress-drag-drop": "^1.1.1",
"typescript": "^4.9.4"
},
"scripts": {
"test": "yarn cypress run",
"open": "yarn cypress open"
}
}
9 changes: 9 additions & 0 deletions packages/e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
2 changes: 0 additions & 2 deletions packages/react-arborist/src/data/create-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function createRoot<T>(tree: TreeApi<T>): NodeApi<T> {
id,
children: null,
isDraggable: tree.isDraggable(data),
isDroppable: tree.isDroppable(data),
rowIndex: null,
});
const children = tree.accessChildren(data);
Expand All @@ -40,7 +39,6 @@ export function createRoot<T>(tree: TreeApi<T>): NodeApi<T> {
parent: null,
children: null,
isDraggable: true,
isDroppable: true,
rowIndex: null,
});

Expand Down
19 changes: 0 additions & 19 deletions packages/react-arborist/src/dnd/compute-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ function getDropLevel(
return bound(hoverLevel, min, max);
}

function canDrop(above: NodeApi | null, below: NodeApi | null) {
if (!above) {
return true;
}

let n: NodeApi | null = above;
if (isClosed(above) && above !== below) n = above.parent;

while (n) {
if (!n.isDroppable) return false;
n = n.parent;
}
return true;
}

export type ComputedDrop = {
drop: DropResult | null;
cursor: Cursor | null;
Expand Down Expand Up @@ -147,10 +132,6 @@ export function computeDrop(args: Args): ComputedDrop {
const { node, nextNode, prevNode } = args;
const [above, below] = getNodesAroundCursor(node, prevNode, nextNode, hover);

if (!canDrop(above, below)) {
return { drop: null, cursor: noCursor() };
}

/* Hovering over the middle of a folder */
if (node && node.isInternal && hover.inMiddle) {
return {
Expand Down
Loading

0 comments on commit 00d5064

Please sign in to comment.