Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/quiet-owls-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix(Select/Combobox): highlight the selected item on open when its value is an empty string, and allow selecting it with Enter in `Combobox`
17 changes: 8 additions & 9 deletions packages/bits-ui/src/lib/bits/select/select.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,13 @@ export class SelectSingleRootState extends SelectBaseRootState {
this.domContext.getDocument().contains(this.highlightedNode)
)
return;
if (this.opts.value.current !== "") {
const node = this.getNodeByValue(this.opts.value.current);
if (node) {
this.setHighlightedNode(node, true);
return;
}
// "" is a valid value for a select item so we need to look it up too
const node = this.getNodeByValue(this.opts.value.current);
if (node) {
this.setHighlightedNode(node, true);
return;
}
// if no value is set, we want to highlight the first item
// if no item matches the value, we want to highlight the first item
this.setHighlightedToFirstCandidate(true);
});
}
Expand Down Expand Up @@ -408,7 +407,7 @@ class SelectMultipleRootState extends SelectBaseRootState {
this.domContext.getDocument().contains(this.highlightedNode)
)
return;
if (this.opts.value.current.length && this.opts.value.current[0] !== "") {
if (this.opts.value.current.length) {
const node = this.getNodeByValue(this.opts.value.current[0]!);
if (node) {
this.setHighlightedNode(node, true);
Expand Down Expand Up @@ -620,7 +619,7 @@ export class SelectInputState {
}

if (
this.root.highlightedValue &&
this.root.highlightedValue !== null &&
this.root.highlightedNode &&
this.root.highlightedNode.isConnected
) {
Expand Down
20 changes: 20 additions & 0 deletions tests/src/tests/combobox/combobox.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,26 @@ describe("combobox - single", () => {
await setter.click();
await expect.element(t.input).toHaveValue("C");
});

it("should select a highlighted item with an empty string value on `Enter`", async () => {
const t = setupSingle({ value: "1" }, [
{ value: "1", label: "A" },
{ value: "", label: "All" },
]);
const allItem = page.getByRole("option", { name: "All" });
(t.input.element() as HTMLElement).focus();

await t.user.keyboard(kbd.ARROW_DOWN);
await expect.element(page.getByTestId("1")).toHaveAttribute("data-highlighted");
await t.user.keyboard(kbd.ARROW_DOWN);
await expect.element(allItem).toHaveAttribute("data-highlighted");
await t.user.keyboard(kbd.ENTER);
await expectNotExists(t.getContent());

await t.user.keyboard(kbd.ARROW_DOWN);
await expect.element(allItem).toHaveAttribute("data-selected");
await expect.element(allItem).toHaveAttribute("data-highlighted");
});
});

// ////////////////////////////////////
Expand Down
37 changes: 37 additions & 0 deletions tests/src/tests/select/select.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,30 @@ describe("select - single", () => {
expect(selectedRect.top).toBeGreaterThanOrEqual(viewportRect.top);
expect(selectedRect.bottom).toBeLessThanOrEqual(viewportRect.bottom);
});

it("should highlight the selected item on reopen when its value is an empty string", async () => {
const items: Item[] = [
{ value: "1", label: "A" },
{ value: "2", label: "B" },
{ value: "", label: "All" },
];
const t = setupSingle({ value: "1" }, items);
(t.trigger.element() as HTMLElement).focus();

await userEvent.keyboard(kbd.ENTER);
await expectHighlighted(page.getByTestId("1"));
await userEvent.keyboard(kbd.ARROW_DOWN);
await userEvent.keyboard(kbd.ARROW_DOWN);
await expectHighlighted(page.getByTestId("empty"));
await userEvent.keyboard(kbd.ENTER);
await expectNotExists(t.getContent());

await userEvent.keyboard(kbd.ENTER);
await expectExists(t.getContent());
await expectSelected(page.getByTestId("empty"));
await expectHighlighted(page.getByTestId("empty"));
await expectNotHighlighted(page.getByTestId("1"));
});
});

////////////////////////////////////
Expand Down Expand Up @@ -1071,6 +1095,19 @@ describe("select - multiple", () => {
await t.submit.click();
expect(submittedValues).toHaveLength(0);
});

it("should highlight the selected item on open when its value is an empty string", async () => {
const t = setupMultiple({ value: [""] }, [
{ value: "1", label: "A" },
{ value: "", label: "All" },
]);
(t.trigger.element() as HTMLElement).focus();

await userEvent.keyboard(kbd.ENTER);
await expectExists(t.getContent());
await expectHighlighted(page.getByTestId("empty"));
await expectNotHighlighted(page.getByTestId("1"));
});
});

describe("select - value", () => {
Expand Down
Loading