Skip to content

Commit

Permalink
Do not error when data has nodes removed that are expanded. Resolves #…
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Feb 1, 2024
1 parent 4e82bb3 commit d76e90c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,33 +291,37 @@ const useTree = ({
//controlled collapsing
if (diffCollapseIds.size) {
for (const id of diffCollapseIds) {
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const ids = [id, ...getDescendants(data, id, new Set<number>())];
dispatch({
type: treeTypes.collapseMany,
ids: ids,
lastInteractedWith: id,
});
if (data.find((n) => n.id === id)) {
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const ids = [id, ...getDescendants(data, id, new Set<number>())];
dispatch({
type: treeTypes.collapseMany,
ids: ids,
lastInteractedWith: id,
});
}
}
}
}
//controlled expanding
if (diffExpandedIds.size) {
for (const id of diffExpandedIds) {
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const parentId = getParent(data, id);
if (parentId) {
dispatch({
type: treeTypes.expandMany,
ids: [id, parentId],
lastInteractedWith: id,
});
} else {
dispatch({
type: treeTypes.expand,
id: id,
lastInteractedWith: id,
});
if (data.find((n) => n.id === id)) {
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const parentId = getParent(data, id);
if (parentId) {
dispatch({
type: treeTypes.expandMany,
ids: [id, parentId],
lastInteractedWith: id,
});
} else {
dispatch({
type: treeTypes.expand,
id: id,
lastInteractedWith: id,
});
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/ControlledExpandedNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,37 @@ describe("Without node ids", () => {
expect(nodesAfterExpandedAll[6]).toHaveAttribute("aria-expanded", "true");
expect(nodesAfterExpandedAll[11]).toHaveAttribute("aria-expanded", "true");
});
test("Should rerender when the data and expanded nodes change", () => {
const expandedIds = [1, 7, 11];

const { queryAllByRole, rerender } = render(
<ControlledExpanded expandedIds={expandedIds} data={dataWithoutIds} />
);

let nodes = queryAllByRole("treeitem");
expect(nodes.length).toBe(16);

// remove the fruits directory from both data and expandedIds
rerender(
<ControlledExpanded
expandedIds={[7, 11]}
data={dataWithoutIds
.filter((node) => node.id !== 1)
.map((node) => ({
...node,
children: node.children.filter((child) => child !== 1),
}))}
/>
);

nodes = queryAllByRole("treeitem");
// six nodes were removed (everything in fruits)
expect(nodes.length).toBe(10);

expect(nodes[0]).toHaveAttribute("aria-expanded", "true");
expect(nodes[1]).not.toHaveAttribute("aria-expanded");
expect(nodes[4]).toHaveAttribute("aria-expanded", "true");
});
});

describe("With node ids", () => {
Expand Down

0 comments on commit d76e90c

Please sign in to comment.