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

Bug fix: Keyboard shortcut should not select node when clickAction is set to focus. #166

Merged
merged 1 commit into from
Dec 5, 2023
Merged
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
49 changes: 27 additions & 22 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ const handleKeyDown = ({
const element = getTreeNode(data, tabbableId);
const id = element.id;
if (event.ctrlKey) {
if (event.key === "a") {
if (event.key === "a" && clickAction !== clickActions.focus) {
event.preventDefault();
const dataWithoutRoot = data.filter((x) => x.parent !== null);
const ids = dataWithoutRoot
Expand All @@ -678,7 +678,8 @@ const handleKeyDown = ({
});
} else if (
event.shiftKey &&
(event.key === "Home" || event.key === "End")
(event.key === "Home" || event.key === "End") &&
clickAction !== clickActions.focus
) {
const newId =
event.key === "Home"
Expand Down Expand Up @@ -711,16 +712,18 @@ const handleKeyDown = ({
event.preventDefault();
const previous = getPreviousAccessible(data, id, expandedIds);
if (previous != null && !disabledIds.has(previous)) {
dispatch({
type: treeTypes.changeSelectMany,
ids: propagateSelect
? propagatedIds(data, [previous], disabledIds)
: [previous],
select: true,
multiSelect,
lastInteractedWith: previous,
lastManuallyToggled: previous,
});
if (clickAction !== clickActions.focus) {
dispatch({
type: treeTypes.changeSelectMany,
ids: propagateSelect
? propagatedIds(data, [previous], disabledIds)
: [previous],
select: true,
multiSelect,
lastInteractedWith: previous,
lastManuallyToggled: previous,
});
}
dispatch({
type: treeTypes.focus,
id: previous,
Expand All @@ -733,16 +736,18 @@ const handleKeyDown = ({
event.preventDefault();
const next = getNextAccessible(data, id, expandedIds);
if (next != null && !disabledIds.has(next)) {
dispatch({
type: treeTypes.changeSelectMany,
ids: propagateSelect
? propagatedIds(data, [next], disabledIds)
: [next],
multiSelect,
select: true,
lastInteractedWith: next,
lastManuallyToggled: next,
});
if (clickAction !== clickActions.focus) {
dispatch({
type: treeTypes.changeSelectMany,
ids: propagateSelect
? propagatedIds(data, [next], disabledIds)
: [next],
multiSelect,
select: true,
lastInteractedWith: next,
lastManuallyToggled: next,
});
}
dispatch({
type: treeTypes.focus,
id: next,
Expand Down
55 changes: 54 additions & 1 deletion src/__tests__/ClickActionFocus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function FocusClickActionTreeView() {
<TreeView
data={data}
aria-label="directory tree"
togglableSelect
multiSelect
clickAction="FOCUS"
nodeRenderer={({ element, getNodeProps }) => (
<div {...getNodeProps()}>{element.name}</div>
Expand All @@ -64,5 +66,56 @@ test("should not select on enter/space keydown when click action is focus", () =
);
}
fireEvent.keyDown(node, { key: " " });
expect(nodes[0]).not.toHaveAttribute("aria-selected");
expect(nodes[0]).toHaveAttribute("aria-selected", "false");
});

test("should not select on control keydown when click action is focus", () => {
const { queryAllByRole } = render(<FocusClickActionTreeView />);

const nodes = queryAllByRole("treeitem");
nodes[0].focus();

if (document.activeElement == null) {
throw new Error(
`Expected to find an active element on the document (after focusing the first element with role["treeitem"]), but did not.`
);
}

fireEvent.keyDown(document.activeElement, { key: "a", ctrlKey: true });
nodes.forEach((x) => expect(x).toHaveAttribute("aria-selected", "false"));

fireEvent.keyDown(document.activeElement, {
key: "Home",
ctrlKey: true,
shiftKey: true,
});
nodes.forEach((x) => expect(x).toHaveAttribute("aria-selected", "false"));

fireEvent.keyDown(document.activeElement, {
key: "End",
ctrlKey: true,
shiftKey: true,
});
nodes.forEach((x) => expect(x).toHaveAttribute("aria-selected", "false"));
});

test("should not select on shift + arrow up/down keydown when click action is focus", () => {
const { queryAllByRole } = render(<FocusClickActionTreeView />);

const nodes = queryAllByRole("treeitem");
nodes[0].focus();

if (document.activeElement == null) {
throw new Error(
`Expected to find an active element on the document (after focusing the first element with role["treeitem"]), but did not.`
);
}
fireEvent.keyDown(document.activeElement, {
key: "ArrowDown",
shiftKey: true,
});
nodes.forEach((x) => expect(x).toHaveAttribute("aria-selected", "false"));
nodes[4].focus();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe add some comment why we use exactly the node with index 4

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to ensure that the focus node has an item to shift up to.

fireEvent.keyDown(document.activeElement, { key: "ArrowUp", shiftKey: true });
nodes.forEach((x) => expect(x).toHaveAttribute("aria-selected", "false"));
});