Skip to content

Commit 08f10f1

Browse files
committed
fix: Fixed an issue where isFolder returns incorrect values for leafs if they are not visibly rendered (#166)
1 parent 44883e4 commit 08f10f1

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

.changeset/dirty-beans-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@headless-tree/core": patch
3+
---
4+
5+
Fixed an issue where `isFolder` returns incorrect values for leafs if they are not visibly rendered (#166)

packages/core/src/features/checkboxes/checkboxes.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ describe("core-feature/checkboxes", () => {
102102
it("should turn folder checked if all children are checked", async () => {
103103
const testTree = await tree
104104
.with({
105-
isItemFolder: (item: any) => item.getItemData().length < 4,
106105
propagateCheckedState: true,
107106
canCheckFolders: false,
108107
})

packages/core/src/features/tree/feature.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ export const treeFeature: FeatureImplementation<any> = {
203203
isFocused: ({ tree, item, itemId }) =>
204204
tree.getState().focusedItem === itemId ||
205205
(tree.getState().focusedItem === null && item.getItemMeta().index === 0),
206-
isFolder: ({ tree, item }) =>
207-
item.getItemMeta().level === -1 ||
206+
isFolder: ({ tree, item, itemId }) =>
207+
itemId === tree.getConfig().rootItemId ||
208208
tree.getConfig().isItemFolder(item as ItemInstance<any>),
209209
getItemName: ({ tree, item }) => {
210210
const config = tree.getConfig();

packages/core/src/features/tree/tree.spec.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { propMemoizationFeature } from "../prop-memoization/feature";
44

55
const factory = TestTree.default({}).withFeatures(propMemoizationFeature);
66

7-
describe("core-feature/selections", () => {
8-
factory.forSuits((tree) => {
7+
describe("core-feature/tree", () => {
8+
factory.forSuits((tree, title) => {
99
describe("expanded items", () => {
1010
it("can expand via tree instance", () => {
1111
const setExpandedItems = tree.mockedHandler("setExpandedItems");
@@ -156,6 +156,17 @@ describe("core-feature/selections", () => {
156156
});
157157
});
158158

159+
it("unloaded item", () => {
160+
expect(tree.instance.getItemInstance("x444").getItemMeta()).toEqual({
161+
index: -1,
162+
itemId: "x444",
163+
level: -1,
164+
parentId: null,
165+
posInSet: 0,
166+
setSize: 1,
167+
});
168+
});
169+
159170
it("expanded container", () => {
160171
expect(tree.instance.getItemInstance("x11").getItemMeta()).toEqual({
161172
index: 1,
@@ -312,8 +323,23 @@ describe("core-feature/selections", () => {
312323
expect(tree.instance.getItemInstance("x1").isFolder()).toBe(true);
313324
});
314325

315-
it("returns correctly for false cases of isFolder()", () => {
316-
expect(tree.instance.getItemInstance("x111").isFolder()).toBe(false);
326+
it("returns correctly for true cases of isFolder() ", () => {
327+
expect(tree.instance.getItemInstance("x1").isFolder()).toBe(true);
328+
});
329+
330+
it("returns correct isFolder for hidden items", () => {
331+
if (title.toLocaleLowerCase().includes("async")) {
332+
// async test tree defaults to "loading" item names
333+
return;
334+
}
335+
336+
// Reference: https://github.com/lukasbach/headless-tree/issues/166
337+
expect(tree.instance.getItemInstance("x44").isFolder()).toBe(true);
338+
expect(tree.instance.getItemInstance("x444").isFolder()).toBe(false);
339+
});
340+
341+
it("returns isFolder=true for root item", () => {
342+
expect(tree.instance.getItemInstance("x").isFolder()).toBe(true);
317343
});
318344

319345
it("returns correctly for getParent()", () => {

packages/core/src/test-utils/test-tree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ export class TestTree<T = string> {
6363
}),
6464
};
6565

66-
forSuits(runSuite: (tree: TestTree<T>) => void) {
66+
forSuits(runSuite: (tree: TestTree<T>, title: string) => void) {
6767
describe.for([
6868
this.suits.sync(),
6969
this.suits.async(),
7070
this.suits.proxifiedSync(),
7171
this.suits.proxifiedAsync(),
72-
])("$title", ({ tree }) => {
72+
])("$title", ({ tree, title }) => {
7373
tree.resetBeforeEach();
74-
runSuite(tree);
74+
runSuite(tree, title);
7575
});
7676
}
7777

@@ -122,7 +122,7 @@ export class TestTree<T = string> {
122122
},
123123
getItemName: (item) => item.getItemData(),
124124
indent: 20,
125-
isItemFolder: (item) => item.getItemMeta().level < 2,
125+
isItemFolder: (item) => item.getItemData().length < 4,
126126
initialState: {
127127
expandedItems: ["x1", "x11"],
128128
},

0 commit comments

Comments
 (0)