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

feat(radio, radio-group): Enhance accessibility with arrow key navigation and aria attributes #951

Merged
merged 7 commits into from
Mar 10, 2025
61 changes: 61 additions & 0 deletions src/components/radio-group/bl-radio-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,65 @@ describe("bl-radio-group", () => {
//then
expect(document.activeElement).to.equal(radioGroup?.options[0]);
});

it("should wrap to first option when navigating past last option with arrow right", async () => {
//when
const el = await fixture(
html`<div>
<input id="previnput" /><bl-radio-group label="Payment Type" name="pt" value="cc">
<bl-radio value="cc">Credit Card</bl-radio>
<bl-radio value="ch">Cash</bl-radio> </bl-radio-group
><input id="nextinput" />
</div>`
);

await elementUpdated(el);

el.querySelector<HTMLInputElement>("#previnput")?.focus();

const radioGroup = el.querySelector("bl-radio-group");

//given
await sendKeys({
press: "Tab",
});
await sendKeys({
press: "ArrowRight",
});
await sendKeys({
press: "ArrowRight",
});

//then
expect(document.activeElement).to.equal(radioGroup?.options[0]);
});

it("should wrap to last option when navigating before first option with arrow left", async () => {
//when
const el = await fixture(
html`<div>
<input id="previnput" /><bl-radio-group label="Payment Type" name="pt" value="cc">
<bl-radio value="cc">Credit Card</bl-radio>
<bl-radio value="ch">Cash</bl-radio> </bl-radio-group
><input id="nextinput" />
</div>`
);

await elementUpdated(el);

el.querySelector<HTMLInputElement>("#previnput")?.focus();

const radioGroup = el.querySelector("bl-radio-group");

//given
await sendKeys({
press: "Tab",
});
await sendKeys({
press: "ArrowLeft",
});

//then
expect(document.activeElement).to.equal(radioGroup?.options[1]);
});
});
15 changes: 8 additions & 7 deletions src/components/radio-group/bl-radio-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,18 @@ export default class BlRadioGroup extends FormControlMixin(LitElement) {
// Next option
if (["ArrowDown", "ArrowRight"].includes(event.key)) {
this.focusedOptionIndex++;
if (this.focusedOptionIndex >= this.availableOptions.length) {
this.focusedOptionIndex = 0; // Wrap around to the first option
}
this.availableOptions[this.focusedOptionIndex].select();

// Previous option
} else if (["ArrowUp", "ArrowLeft"].includes(event.key)) {
this.focusedOptionIndex--;
if (this.focusedOptionIndex < 0) {
this.focusedOptionIndex = this.availableOptions.length - 1; // Wrap around to the last option
}
this.availableOptions[this.focusedOptionIndex].select();

// Select option
} else if ([" "].includes(event.key)) {
Expand All @@ -90,17 +98,10 @@ export default class BlRadioGroup extends FormControlMixin(LitElement) {
return;
}

// Don't exceed array indexes
this.focusedOptionIndex = Math.max(
0,
Math.min(this.focusedOptionIndex, this.availableOptions.length - 1)
);

this.availableOptions[this.focusedOptionIndex].focus();

event.preventDefault();
}

connectedCallback(): void {
super.connectedCallback();

Expand Down
2 changes: 1 addition & 1 deletion src/components/radio-group/radio/bl-radio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("bl-radio", () => {
`<div
aria-disabled="false"
aria-labelledby="label"
aria-readonly="false"
aria-checked="false"
class="wrapper"
role="radio"
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/radio-group/radio/bl-radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class BlRadio extends LitElement {
role="radio"
aria-labelledby="label"
aria-disabled=${this.disabled}
aria-readonly=${this.disabled}
aria-checked=${this.selected}
@blur=${this.blur}
@click=${this.select}
>
Expand Down