Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: improve radio item selection #927

Merged
merged 1 commit into from
Nov 14, 2024
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/wild-dogs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: don't auto select first radio item unless there is already a selected value
10 changes: 10 additions & 0 deletions packages/bits-ui/src/lib/bits/radio-group/radio-group.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useRovingFocus,
} from "$lib/internal/use-roving-focus.svelte.js";
import { createContext } from "$lib/internal/create-context.js";
import { kbd } from "$lib/internal/kbd.js";

const RADIO_GROUP_ROOT_ATTR = "data-radio-group-root";
const RADIO_GROUP_ITEM_ATTR = "data-radio-group-item";
Expand All @@ -32,6 +33,7 @@ class RadioGroupRootState {
name: RadioGroupRootStateProps["name"];
value: RadioGroupRootStateProps["value"];
rovingFocusGroup: UseRovingFocusReturn;
hasValue = $derived.by(() => this.value.current !== "");

constructor(props: RadioGroupRootStateProps) {
this.#id = props.id;
Expand All @@ -42,6 +44,7 @@ class RadioGroupRootState {
this.name = props.name;
this.value = props.value;
this.#ref = props.ref;

this.rovingFocusGroup = useRovingFocus({
rootNodeId: this.#id,
candidateAttr: RADIO_GROUP_ITEM_ATTR,
Expand Down Expand Up @@ -129,10 +132,17 @@ class RadioGroupItemState {
};

#onfocus = () => {
if (!this.#root.hasValue) return;
this.#root.setValue(this.#value.current);
};

#onkeydown = (e: KeyboardEvent) => {
if (this.#isDisabled) return;
if (e.key === kbd.SPACE) {
e.preventDefault();
this.#root.setValue(this.#value.current);
return;
}
this.#root.rovingFocusGroup.handleKeydown(this.#ref.current, e, true);
};

Expand Down
61 changes: 39 additions & 22 deletions packages/tests/src/tests/radio-group/radio-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ describe("radio group", () => {
const item2 = getByTestId(itemIds[2] as string);
const item3 = getByTestId(itemIds[3] as string);
item0.focus();
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
await user.keyboard(kbd.ARROW_DOWN);
await waitFor(() => expect(item1).toHaveFocus());
expect(item1).toHaveFocus();
await user.keyboard(kbd.ARROW_DOWN);
await waitFor(() => expect(item2).toHaveFocus());
expect(item2).toHaveFocus();
await user.keyboard(kbd.ARROW_DOWN);
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
await user.keyboard(kbd.ARROW_UP);
await waitFor(() => expect(item2).toHaveFocus());
expect(item2).toHaveFocus();
await user.keyboard(kbd.ARROW_UP);
await waitFor(() => expect(item1).toHaveFocus());
expect(item1).toHaveFocus();
await user.keyboard(kbd.ARROW_UP);
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
});

it("should navigate through the items using the keyboard (left and right)", async () => {
Expand All @@ -114,19 +114,19 @@ describe("radio group", () => {
const item2 = getByTestId(itemIds[2] as string);
const item3 = getByTestId(itemIds[3] as string);
item0.focus();
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
await waitFor(() => expect(item1).toHaveFocus());
expect(item1).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
await waitFor(() => expect(item2).toHaveFocus());
expect(item2).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
await user.keyboard(kbd.ARROW_LEFT);
await waitFor(() => expect(item2).toHaveFocus());
expect(item2).toHaveFocus();
await user.keyboard(kbd.ARROW_LEFT);
await waitFor(() => expect(item1).toHaveFocus());
expect(item1).toHaveFocus();
await user.keyboard(kbd.ARROW_LEFT);
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
});

it("should respect the loop prop", async () => {
Expand All @@ -137,14 +137,14 @@ describe("radio group", () => {
const item0 = getByTestId(itemIds[0] as string);
const item3 = getByTestId(itemIds[3] as string);
item0.focus();
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
await user.keyboard(kbd.ARROW_UP);
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();

item3.focus();
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
await user.keyboard(kbd.ARROW_DOWN);
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
});

it("should respect the value prop & binding", async () => {
Expand All @@ -170,14 +170,14 @@ describe("radio group", () => {
const item0 = getByTestId(itemIds[0] as string);
const item3 = getByTestId(itemIds[3] as string);
item0.focus();
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();
await user.keyboard(kbd.ARROW_LEFT);
await waitFor(() => expect(item0).toHaveFocus());
expect(item0).toHaveFocus();

item3.focus();
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
await waitFor(() => expect(item3).toHaveFocus());
expect(item3).toHaveFocus();
});

it("should not render an input if the `name` prop isn't passed", async () => {
Expand Down Expand Up @@ -220,4 +220,21 @@ describe("radio group", () => {

expect(input).toHaveAttribute("disabled");
});

it("should not automatically select the first item focused when the radio group does not have a value", async () => {
const { getByTestId, user, input } = setup({ name: "radio-group" });

const aItem = getByTestId("a-item");
aItem.focus();
expect(input).toHaveValue("");
await user.keyboard(kbd.ARROW_DOWN);
expect(input).toHaveValue("");
const bItem = getByTestId("b-item");
expect(bItem).toHaveFocus();
await user.keyboard(kbd.SPACE);
expect(input).toHaveValue("b");
await user.keyboard(kbd.ARROW_UP);
expect(aItem).toHaveFocus();
expect(input).toHaveValue("a");
});
});