Skip to content

Commit 396963c

Browse files
authored
fix(Select): allow toggling items with the space key (#2073)
1 parent 2d03bdb commit 396963c

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎.changeset/cool-lands-battle.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bits-ui": patch
3+
---
4+
5+
fix(Select): space to toggle items

‎packages/bits-ui/src/lib/bits/select/select.svelte.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ export class SelectTriggerState {
937937
) {
938938
e.preventDefault();
939939
const shouldReturn = this.#handleKeyboardSelection();
940-
if (shouldReturn) return;
940+
if (shouldReturn || e.key === kbd.SPACE) return;
941941
}
942942

943943
if (e.key === kbd.ARROW_UP && e.altKey) {

‎tests/src/tests/select/select.browser.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,23 @@ describe("select - multiple", () => {
12201220
await expectNotSelected([item0, item1, item2]);
12211221
});
12221222

1223+
it("should toggle a highlighted item with a single space keypress", async () => {
1224+
const t = await openMultiple({}, kbd.SPACE);
1225+
const [item0, item1] = getItems(page.getByTestId);
1226+
1227+
await expectHighlighted(item0);
1228+
await userEvent.keyboard(kbd.ARROW_DOWN);
1229+
await expectHighlighted(item1);
1230+
1231+
await userEvent.keyboard(kbd.SPACE);
1232+
await expect.element(t.trigger).toHaveTextContent("A");
1233+
await expectSelected(item1);
1234+
1235+
await userEvent.keyboard(kbd.SPACE);
1236+
await expect.element(t.trigger).toHaveTextContent("Open combobox");
1237+
await expectNotSelected(item1);
1238+
});
1239+
12231240
it("should apply the `data-highlighted` attribute on mouseover", async () => {
12241241
await openMultiple({});
12251242
const [item1, item2] = getItems(page.getByTestId);

0 commit comments

Comments
 (0)