Allow using Tab to select the active option

This commit is contained in:
Koen Vlaswinkel
2024-01-29 14:53:36 +01:00
parent dc8062b784
commit 8c696e10af
2 changed files with 47 additions and 3 deletions

View File

@@ -207,9 +207,9 @@ export const SuggestBox = <
"aria-autocomplete": "list",
"aria-label": ariaLabel,
onKeyDown: (event) => {
// When the user presses the enter key, select the active item
// When the user presses the enter or tab key, select the active item
if (
event.key === "Enter" &&
(event.key === "Enter" || event.key === "Tab") &&
activeIndex !== null &&
suggestionItems[activeIndex]
) {

View File

@@ -161,7 +161,7 @@ describe("SuggestBox", () => {
expect(screen.getByRole("option")).toHaveTextContent("Parameter[1]");
});
it("closes the options when selecting an option", async () => {
it("selects an option using Enter", async () => {
render({
value: "Argument[block].1",
});
@@ -169,6 +169,50 @@ describe("SuggestBox", () => {
await userEvent.click(screen.getByRole("combobox"));
await userEvent.keyboard("{Enter}");
expect(onChange).toHaveBeenCalledWith("Argument[block].Parameter[1]");
});
it("selects an option using Tab", async () => {
render({
value: "Argument[block].1",
});
await userEvent.click(screen.getByRole("combobox"));
await userEvent.keyboard("{Enter}");
expect(onChange).toHaveBeenCalledWith("Argument[block].Parameter[1]");
});
it("does not select an option using Home", async () => {
render({
value: "Argument[block].1",
});
await userEvent.click(screen.getByRole("combobox"));
await userEvent.keyboard("{Home}");
expect(onChange).not.toHaveBeenCalled();
});
it("closes the options when selecting an option using Enter", async () => {
render({
value: "Argument[block].1",
});
await userEvent.click(screen.getByRole("combobox"));
await userEvent.keyboard("{Enter}");
expect(screen.queryByRole("option")).not.toBeInTheDocument();
});
it("closes the options when selecting an option using Tab", async () => {
render({
value: "Argument[block].1",
});
await userEvent.click(screen.getByRole("combobox"));
await userEvent.keyboard("{Tab}");
expect(screen.queryByRole("option")).not.toBeInTheDocument();
});