diff --git a/src/ListBox/ListBoxField.svelte b/src/ListBox/ListBoxField.svelte
index 8b9950d1..63348d4c 100644
--- a/src/ListBox/ListBoxField.svelte
+++ b/src/ListBox/ListBoxField.svelte
@@ -47,6 +47,7 @@
{
- if (disabled) return;
- if (filterable) {
- open = true;
- inputRef.focus();
- } else {
- open = !open;
- }
- }}
- on:keydown={(e) => {
- if (filterable) {
- return;
- }
- const key = e.key;
- if ([" ", "ArrowUp", "ArrowDown"].includes(key)) {
- e.preventDefault();
- }
- if (key === " ") {
- open = !open;
- } else if (key === "Tab") {
- if (selectionRef && checked.length > 0) {
- selectionRef.focus();
- } else {
- open = false;
- }
- } else if (key === "ArrowDown") {
- change(1);
- } else if (key === "ArrowUp") {
- change(-1);
- } else if (key === "Enter") {
- if (highlightedIndex > -1) {
- sortedItems = sortedItems.map((item, i) => {
- if (i !== highlightedIndex) return item;
- return { ...item, checked: !item.checked };
- });
- }
- } else if (key === "Escape") {
- open = false;
- }
- }}
- on:focus={() => {
- if (filterable) {
- open = true;
- if (inputRef) inputRef.focus();
- }
- }}
- on:blur={(e) => {
- if (!filterable) dispatch("blur", e);
- }}
- {id}
- {disabled}
- {translateWithId}
- >
+
{#if checked.length > 0}
{/if}
- {#if filterable}
- {
- if (key === "Enter") {
- if (highlightedId) {
- const filteredItemIndex = sortedItems.findIndex(
- (item) => item.id === highlightedId,
- );
- sortedItems = sortedItems.map((item, i) => {
- if (i !== filteredItemIndex) return item;
- return { ...item, checked: !item.checked };
- });
- }
- } else if (key === "Tab") {
+ {
+ if (disabled) return;
+ if (filterable) {
+ open = true;
+ inputRef.focus();
+ } else {
+ open = !open;
+ }
+ }}
+ on:keydown={(e) => {
+ if (filterable) {
+ return;
+ }
+ const key = e.key;
+ if ([" ", "ArrowUp", "ArrowDown"].includes(key)) {
+ e.preventDefault();
+ }
+ if (key === " ") {
+ open = !open;
+ } else if (key === "Tab") {
+ if (selectionRef && checked.length > 0) {
+ selectionRef.focus();
+ } else {
open = false;
- } else if (key === "ArrowDown") {
- change(1);
- } else if (key === "ArrowUp") {
- change(-1);
- } else if (key === "Escape") {
- open = false;
- } else if (key === " ") {
- if (!open) open = true;
}
- }}
- on:input
- on:keyup
- on:focus
- on:blur
- on:paste
- {disabled}
- {placeholder}
- {id}
- {name}
- />
- {#if invalid}
-
- {/if}
- {#if value}
- {
- value = "";
- open = false;
+ } else if (key === "ArrowDown") {
+ change(1);
+ } else if (key === "ArrowUp") {
+ change(-1);
+ } else if (key === "Enter") {
+ if (highlightedIndex > -1) {
+ sortedItems = sortedItems.map((item, i) => {
+ if (i !== highlightedIndex) return item;
+ return { ...item, checked: !item.checked };
+ });
+ }
+ } else if (key === "Escape") {
+ open = false;
+ }
+ }}
+ on:focus={() => {
+ if (filterable) {
+ open = true;
+ if (inputRef) inputRef.focus();
+ }
+ }}
+ on:blur={(e) => {
+ if (!filterable) dispatch("blur", e);
+ }}
+ {id}
+ {disabled}
+ {translateWithId}
+ >
+ {#if filterable}
+ {
+ if (key === "Enter") {
+ if (highlightedId) {
+ const filteredItemIndex = sortedItems.findIndex(
+ (item) => item.id === highlightedId,
+ );
+ sortedItems = sortedItems.map((item, i) => {
+ if (i !== filteredItemIndex) return item;
+ return { ...item, checked: !item.checked };
+ });
+ }
+ } else if (key === "Tab") {
+ open = false;
+ } else if (key === "ArrowDown") {
+ change(1);
+ } else if (key === "ArrowUp") {
+ change(-1);
+ } else if (key === "Escape") {
+ open = false;
+ } else if (key === " ") {
+ if (!open) open = true;
+ }
}}
- translateWithId={translateWithIdSelection}
+ on:input
+ on:keyup
+ on:focus
+ on:blur
+ on:paste
{disabled}
+ {placeholder}
+ {id}
+ {name}
+ />
+ {#if invalid}
+
+ {/if}
+ {#if value}
+ {
+ value = "";
+ open = false;
+ }}
+ translateWithId={translateWithIdSelection}
+ {disabled}
+ {open}
+ />
+ {/if}
+ {
+ e.stopPropagation();
+ open = !open;
+ }}
+ {translateWithId}
{open}
/>
{/if}
- {
- e.stopPropagation();
- open = !open;
- }}
- {translateWithId}
- {open}
- />
- {/if}
- {#if !filterable}
- {label}
-
- {/if}
-
+ {#if !filterable}
+ {label}
+
+ {/if}
+
+
{#each filterable ? filteredItems : sortedItems as item, i (item.id)}
diff --git a/tests/MultiSelect/MultiSelect.test.ts b/tests/MultiSelect/MultiSelect.test.ts
index 1dac770f..9717ab65 100644
--- a/tests/MultiSelect/MultiSelect.test.ts
+++ b/tests/MultiSelect/MultiSelect.test.ts
@@ -465,20 +465,24 @@ describe("MultiSelect", () => {
selectedIds: ["0", "1"],
},
});
- await user.click(screen.getAllByRole("button")[0]);
+ await openMenu();
const options = screen.getAllByRole("option");
expect(options[0]).toHaveAttribute("aria-selected", "true");
expect(options[1]).toHaveAttribute("aria-selected", "true");
expect(options[2]).toHaveAttribute("aria-selected", "false");
- const clearButton = screen.getByRole("button", { name: /clear/i });
+ await closeMenu();
+ const clearButton = screen.getByRole("button", {
+ name: /clear all selected items/i,
+ });
await user.click(clearButton);
- await user.click(screen.getByRole("button"));
- expect(options[0]).toHaveAttribute("aria-selected", "false");
- expect(options[1]).toHaveAttribute("aria-selected", "false");
- expect(options[2]).toHaveAttribute("aria-selected", "false");
+ await openMenu();
+ const updatedOptions = screen.getAllByRole("option");
+ expect(updatedOptions[0]).toHaveAttribute("aria-selected", "false");
+ expect(updatedOptions[1]).toHaveAttribute("aria-selected", "false");
+ expect(updatedOptions[2]).toHaveAttribute("aria-selected", "false");
});
it("skips disabled items during keyboard navigation", async () => {