Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not error when data has nodes removed that are expanded #174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,33 +295,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
Loading