fix(combo-box): clear button supports "Space" key (#2168)

Fixes #2166
This commit is contained in:
Eric Liu 2025-06-07 12:33:59 -07:00 committed by GitHub
commit 95c06a83b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 14 deletions

View file

@ -75,7 +75,7 @@
}
}}
on:keydown|stopPropagation={(e) => {
if (!disabled && e.key === "Enter") {
if (!disabled && (e.key === "Enter" || e.key === " ")) {
dispatch("clear", e);
}
}}
@ -103,7 +103,7 @@
}
}}
on:keydown|stopPropagation={(e) => {
if (!disabled && e.key === "Enter") {
if (!disabled && (e.key === "Enter" || e.key === " ")) {
dispatch("clear", e);
}
}}

View file

@ -52,5 +52,7 @@
}}
on:clear={(e) => {
console.log("clear", e.type);
value = "";
selectedId = undefined;
}}
/>

View file

@ -4,6 +4,9 @@ import ComboBox from "./ComboBox.test.svelte";
import ComboBoxCustom from "./ComboBoxCustom.test.svelte";
describe("ComboBox", () => {
const getClearButton = () =>
screen.getByRole("button", { name: "Clear selected item" });
beforeEach(() => {
vi.clearAllMocks();
});
@ -60,16 +63,13 @@ describe("ComboBox", () => {
},
});
const clearButton = screen.getByRole("button", {
name: "Clear selected item",
});
await user.click(clearButton);
await user.click(getClearButton());
expect(consoleLog).toHaveBeenCalledWith("clear", expect.any(String));
expect(screen.getByRole("textbox")).toHaveValue("");
});
it("should handle clear selection via keyboard navigation", async () => {
it("should handle clear selection via keyboard navigation (Enter)", async () => {
const consoleLog = vi.spyOn(console, "log");
render(ComboBox, {
props: {
@ -78,9 +78,10 @@ describe("ComboBox", () => {
},
});
const clearButton = screen.getByRole("button", {
name: /clear/i,
});
expect(consoleLog).not.toHaveBeenCalled();
expect(screen.getByRole("textbox")).toHaveValue("Email");
const clearButton = getClearButton();
clearButton.focus();
expect(clearButton).toHaveFocus();
await user.keyboard("{Enter}");
@ -89,6 +90,27 @@ describe("ComboBox", () => {
expect(screen.getByRole("textbox")).toHaveValue("");
});
it("should handle clear selection via keyboard navigation (Space)", async () => {
const consoleLog = vi.spyOn(console, "log");
render(ComboBox, {
props: {
selectedId: "1",
value: "Email",
},
});
expect(consoleLog).not.toHaveBeenCalled();
expect(screen.getByRole("textbox")).toHaveValue("Email");
const clearButton = getClearButton();
clearButton.focus();
expect(clearButton).toHaveFocus();
await user.keyboard(" ");
expect(consoleLog).toHaveBeenCalledWith("clear", expect.any(String));
expect(screen.getByRole("textbox")).toHaveValue("");
});
it("should use custom translations when translateWithId is provided", () => {
const customTranslations = {
clearSelection: "Remove selected item",
@ -205,8 +227,7 @@ describe("ComboBox", () => {
render(ComboBox, { props: { selectedId: "1" } });
expect(consoleLog).not.toBeCalled();
const clearButton = screen.getByRole("button", { name: /clear/i });
await user.click(clearButton);
await user.click(getClearButton());
expect(screen.getByRole("textbox")).toHaveValue("");
expect(consoleLog).toHaveBeenCalledWith("clear", "clear");
@ -250,8 +271,7 @@ describe("ComboBox", () => {
it("should clear filter on selection clear", async () => {
render(ComboBoxCustom, { props: { selectedId: "1" } });
const clearButton = screen.getByLabelText("Clear selected item");
await user.click(clearButton);
await user.click(getClearButton());
const input = screen.getByRole("textbox");
expect(input).toHaveValue("");