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

fix: radio item label not selecting #943

Merged
merged 2 commits into from
Nov 18, 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/hot-dolphins-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: issue causing labels associated with radio group items not to select the item
19 changes: 5 additions & 14 deletions packages/bits-ui/src/lib/bits/radio-group/radio-group.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { srOnlyStyles, styleToString, useRefById } from "svelte-toolbelt";
import type { FocusEventHandler, KeyboardEventHandler, MouseEventHandler } from "svelte/elements";
import type { ReadableBoxedValues, WritableBoxedValues } from "$lib/internal/box.svelte.js";
import type { WithRefProps } from "$lib/internal/types.js";
import { getAriaChecked, getAriaRequired, getDataDisabled } from "$lib/internal/attrs.js";
Expand Down Expand Up @@ -117,26 +118,17 @@ class RadioGroupItemState {
});
}

#onpointerdown = (e: PointerEvent) => {
#onclick: MouseEventHandler<HTMLButtonElement> = (e) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") return e.preventDefault();
this.#root.setValue(this.#value.current);
};

#onpointerup = (e: PointerEvent) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") {
e.preventDefault();
this.#root.setValue(this.#value.current);
}
};

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

#onkeydown = (e: KeyboardEvent) => {
#onkeydown: KeyboardEventHandler<HTMLButtonElement> = (e) => {
if (this.#isDisabled) return;
if (e.key === kbd.SPACE) {
e.preventDefault();
Expand Down Expand Up @@ -165,10 +157,9 @@ class RadioGroupItemState {
role: "radio",
tabindex: this.#tabIndex,
//
onpointerdown: this.#onpointerdown,
onpointerup: this.#onpointerup,
onkeydown: this.#onkeydown,
onfocus: this.#onfocus,
onclick: this.#onclick,
}) as const
);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/tests/src/tests/radio-group/radio-group-test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
<main>
<RadioGroup.Root data-testid="root" bind:value {...restProps}>
{#each items as { value, disabled }}
<RadioGroup.Item {value} {disabled} data-testid="{value}-item">
<RadioGroup.Item id={value} {value} {disabled} data-testid="{value}-item">
{#snippet children({ checked })}
<span data-testid="{value}-indicator"> {checked} </span>
{value}
{/snippet}
</RadioGroup.Item>
<label for={value} data-testid="{value}-label">
Label for {value}
</label>
{/each}
</RadioGroup.Root>
<button
Expand Down
15 changes: 15 additions & 0 deletions packages/tests/src/tests/radio-group/radio-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const testItems: Item[] = [
];

const itemIds = testItems.map((item) => `${item.value}-item`);
const labelIds = testItems.map((item) => `${item.value}-label`);
const indicatorIds = testItems.map((item) => `${item.value}-indicator`);

function setup(props: Partial<RadioGroupTestProps> = {}, items: Item[] = testItems) {
Expand Down Expand Up @@ -237,4 +238,18 @@ describe("radio group", () => {
expect(aItem).toHaveFocus();
expect(input).toHaveValue("a");
});

it("should change the value when a label associated with an item is clicked", async () => {
const { getByTestId, user } = setup();

for (const indicator of indicatorIds) {
expect(getByTestId(indicator)).toHaveTextContent("false");
}
const itemIdx = randItem();

const label = getByTestId(labelIds[itemIdx] as string);

await user.click(label);
expect(getByTestId(indicatorIds[itemIdx] as string)).toHaveTextContent("true");
});
});
2 changes: 1 addition & 1 deletion sites/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"dev": "concurrently \"pnpm:dev:content\" \"pnpm:dev:svelte\" \"pnpm:replace:velite\"",
"dev:content": "velite dev --watch",
"dev:svelte": "vite dev --host",
"dev:svelte": "vite dev",
"build": "velite && node ./other/update-velite-output.js && pnpm build:search && vite build",
"replace:velite": "node ./other/watch-velite-output.js",
"build:content": "velite",
Expand Down