Skip to content

Commit abfc6d6

Browse files
authored
Bug with closing nodes, when they are match with search string (#116)
* Bug with closing nodes, when they are match with search string Fixes #115 * Remove deps we no longer need
1 parent 739769b commit abfc6d6

File tree

8 files changed

+69
-49
lines changed

8 files changed

+69
-49
lines changed
File renamed without changes.

packages/e2e/cypress.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = defineConfig({
55
setupNodeEvents(on, config) {
66
// implement node event listeners here
77
},
8+
viewportHeight: 900,
89
},
910
});

packages/e2e/cypress/e2e/gmail-spec.cy.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "@4tw/cypress-drag-drop";
1+
const TOTAL_ITEMS = 17;
22

33
describe("Testing the Gmail Demo", () => {
44
beforeEach(() => {
@@ -14,11 +14,11 @@ describe("Testing the Gmail Demo", () => {
1414
});
1515

1616
it("Collapses and Expands the Categories", () => {
17-
cy.get("@item").should("have.length", "16");
17+
cy.get("@item").should("have.length", TOTAL_ITEMS);
1818
cy.get("@item").contains("Categories").click();
1919
cy.get("@item").should("have.length", "12");
2020
cy.get("@item").contains("Categories").click();
21-
cy.get("@item").should("have.length", "16");
21+
cy.get("@item").should("have.length", TOTAL_ITEMS);
2222
});
2323

2424
it("Up and Down Arrows", () => {
@@ -32,12 +32,12 @@ describe("Testing the Gmail Demo", () => {
3232
});
3333

3434
it("Left and Right Arrows", () => {
35-
cy.get("@item").should("have.length", 16);
35+
cy.get("@item").should("have.length", TOTAL_ITEMS);
3636
cy.get("@item").contains("Categories").click();
3737
cy.focused().type("{leftArrow}");
3838
cy.get("@item").should("have.length", 12);
3939
cy.focused().type("{rightArrow}");
40-
cy.get("@item").should("have.length", 16);
40+
cy.get("@item").should("have.length", TOTAL_ITEMS);
4141
cy.focused().should("contain.text", "Categories");
4242
cy.focused().type("{rightArrow}");
4343
cy.focused().should("contain.text", "Social");
@@ -51,13 +51,13 @@ describe("Testing the Gmail Demo", () => {
5151
cy.get("@item").first().click();
5252
cy.focused().type("a");
5353
cy.focused().type("Turn A New Leaf{enter}");
54-
cy.get("@item").should("have.length", 17);
54+
cy.get("@item").should("have.length", TOTAL_ITEMS + 1);
5555

5656
// In a Folder
5757
cy.get("@item").contains("Social").click();
5858
cy.focused().type("a");
5959
cy.focused().type("Turn More Leaves{enter}");
60-
cy.get("@item").should("have.length", 18);
60+
cy.get("@item").should("have.length", TOTAL_ITEMS + 2);
6161

6262
// On a folder that is closed
6363
cy.get("@item").contains("Categories").click(); // closed it
@@ -79,14 +79,14 @@ describe("Testing the Gmail Demo", () => {
7979
cy.get("@item").first().click();
8080
cy.focused().type("A");
8181
cy.focused().type("Turn A New Internal{enter}");
82-
cy.get("@item").should("have.length", 17);
82+
cy.get("@item").should("have.length", TOTAL_ITEMS + 1);
8383
cy.focused().children().should("have.class", "isInternal");
8484

8585
// In a Folder
8686
cy.get("@item").contains("Social").click();
8787
cy.focused().type("A");
8888
cy.focused().type("Turn More Inernals{enter}");
89-
cy.get("@item").should("have.length", 18);
89+
cy.get("@item").should("have.length", TOTAL_ITEMS + 2);
9090
cy.focused().children().should("have.class", "isInternal");
9191

9292
// On a folder that is closed
@@ -106,24 +106,44 @@ describe("Testing the Gmail Demo", () => {
106106
});
107107

108108
it("drags and drops in its list", () => {
109-
cy.get("@item")
110-
.contains("Inbox")
111-
.drag("[role=treeitem]:nth-child(5)", "bottom");
109+
dragAndDrop(
110+
cy.get("@item").contains("Inbox").first(),
111+
cy.get("@item").contains("Sent").first()
112+
);
112113

113114
cy.get("@item").contains("Inbox").click();
114-
cy.focused().invoke("index").should("eq", 4);
115+
cy.focused().invoke("index").should("eq", 2);
115116
});
116117

117118
it("drags and drops into folder", () => {
118-
cy.get("@item").contains("Starred").drag("[role=treeitem]:nth-child(12)");
119-
119+
dragAndDrop(
120+
cy.get("@item").contains("Starred").first(),
121+
cy.get("@item").contains("Social").first()
122+
);
120123
cy.get("@item").contains("Starred").click();
121124
cy.focused().invoke("index").should("eq", 11);
122125
});
123126

124127
it("prevents Inbox from Dragging into Categories", () => {
125-
cy.get("@item").contains("Inbox").drag("[role=treeitem]:nth-child(12)");
128+
dragAndDrop(
129+
cy.get("@item").contains("Inbox").first(),
130+
cy.get("@item").contains("Social").first()
131+
);
126132
cy.get("@item").contains("Inbox").click();
127133
cy.focused().invoke("index").should("eq", 0);
128134
});
135+
136+
it("filters to github, then checks expanding and collapsing", () => {
137+
cy.get("input").type("Git");
138+
cy.get("@item").should("have.length", 3);
139+
cy.get("@item").contains("Categories").click(); // collapses
140+
cy.get("@item").should("have.length", 1);
141+
});
129142
});
143+
144+
function dragAndDrop(src: any, dst: any) {
145+
const dataTransfer = new DataTransfer();
146+
src.trigger("dragstart", { dataTransfer });
147+
dst.trigger("drop", { dataTransfer });
148+
dst.trigger("dragend", { dataTransfer });
149+
}

packages/e2e/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"test": "start-server-and-test serve http://localhost:3000 cy:run"
99
},
1010
"devDependencies": {
11-
"@4tw/cypress-drag-drop": "^2.2.3",
1211
"cypress": "^12.4.1",
13-
"cypress-drag-drop": "^1.1.1",
1412
"serve": "^14.2.0",
1513
"start-server-and-test": "^1.15.3",
1614
"typescript": "^4.9.4"

packages/react-arborist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"module": "dist/module.js",
88
"types": "dist/index.d.ts",
99
"scripts": {
10-
"start": "run-p 'watch:**'",
10+
"start": "run-p 'start:**'",
1111
"build": "npm-run-all clean -p 'build:**'",
1212
"test": "jest",
1313
"build:js": "parcel build --target main --target module",

packages/react-arborist/src/data/create-list.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,35 @@ function flattenAndFilterTree<T>(
2929
root: NodeApi<T>,
3030
isMatch: (n: NodeApi<T>) => boolean
3131
): NodeApi<T>[] {
32-
function collect(node: NodeApi<T>) {
33-
let result: NodeApi<T>[] = [];
34-
const yes = !node.isRoot && isMatch(node);
32+
const matches: Record<string, boolean> = {};
33+
const list: NodeApi<T>[] = [];
3534

36-
if (node.children) {
37-
for (let child of node.children) {
38-
result = result.concat(collect(child));
35+
function markMatch(node: NodeApi<T>) {
36+
const yes = !node.isRoot && isMatch(node);
37+
if (yes) {
38+
matches[node.id] = true;
39+
let parent = node.parent;
40+
while (parent) {
41+
matches[parent.id] = true;
42+
parent = parent.parent;
3943
}
4044
}
41-
if (result.length) {
42-
if (!node.isRoot) result.unshift(node);
43-
return result;
45+
if (node.children) {
46+
for (let child of node.children) markMatch(child);
4447
}
45-
if (yes) return [node];
46-
else return [];
4748
}
4849

49-
const list = collect(root).filter((n) => n.parent?.isOpen);
50+
function collect(node: NodeApi<T>) {
51+
if (node.level >= 0 && matches[node.id]) {
52+
list.push(node);
53+
}
54+
if (node.isOpen) {
55+
node.children?.forEach(collect);
56+
}
57+
}
58+
59+
markMatch(root);
60+
collect(root);
5061
list.forEach(assignRowIndex);
5162
return list;
5263
}

packages/showcase/data/gmail.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ export const gmailData: GmailItem[] = [
114114
unread: 312,
115115
readOnly: false,
116116
icon: icons.MdChatBubbleOutline,
117+
children: [
118+
{
119+
id: "15-1",
120+
name: "Github",
121+
readOnly: false,
122+
icon: icons.MdSocialDistance,
123+
},
124+
],
117125
},
118126
{
119127
id: "16",

yarn.lock

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ __metadata:
55
version: 6
66
cacheKey: 8
77

8-
"@4tw/cypress-drag-drop@npm:^2.2.3":
9-
version: 2.2.3
10-
resolution: "@4tw/cypress-drag-drop@npm:2.2.3"
11-
peerDependencies:
12-
cypress: ^2.1.0 || ^3.1.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0
13-
checksum: 575a0f981093b44cc7c60de2cbe527468495ef2c020883ae54d5e2c977008d7466cf607b75de208bb954a3a0728fe744cf08b10aab0fdb7de866f2ccd04c89fe
14-
languageName: node
15-
linkType: hard
16-
178
"@ampproject/remapping@npm:^2.1.0":
189
version: 2.2.0
1910
resolution: "@ampproject/remapping@npm:2.2.0"
@@ -3472,13 +3463,6 @@ __metadata:
34723463
languageName: node
34733464
linkType: hard
34743465

3475-
"cypress-drag-drop@npm:^1.1.1":
3476-
version: 1.1.1
3477-
resolution: "cypress-drag-drop@npm:1.1.1"
3478-
checksum: 8e449c5db25c64afd5c403a19e91879f8d28750c0f7bb1ca5e0e95abb9731639e64b251c279673fb6de57c17454b6066b1e097536639e662ad1c1e308ecfa283
3479-
languageName: node
3480-
linkType: hard
3481-
34823466
"cypress@npm:^12.4.1":
34833467
version: 12.4.1
34843468
resolution: "cypress@npm:12.4.1"
@@ -3774,9 +3758,7 @@ __metadata:
37743758
version: 0.0.0-use.local
37753759
resolution: "e2e@workspace:packages/e2e"
37763760
dependencies:
3777-
"@4tw/cypress-drag-drop": ^2.2.3
37783761
cypress: ^12.4.1
3779-
cypress-drag-drop: ^1.1.1
37803762
serve: ^14.2.0
37813763
start-server-and-test: ^1.15.3
37823764
typescript: ^4.9.4

0 commit comments

Comments
 (0)