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-button): focuses first focusable radio-button element in group. #7152

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
reflects,
renders
} from "../../tests/commonTests";
import { html } from "../../../support/formatting";
import { getFocusedElementProp } from "../../tests/utils";

describe("calcite-radio-button", () => {
describe("renders", () => {
Expand Down Expand Up @@ -452,4 +454,58 @@ describe("calcite-radio-button", () => {
describe("is form-associated", () => {
formAssociated("calcite-radio-button", { testValue: true, inputType: "radio" });
});

it("focuses first focusable item on Tab", async () => {
const page = await newE2EPage();
await page.setContent(html`
<div>
<calcite-label layout="inline">
<calcite-radio-button value="trees" disabled id="trees" name="Options"></calcite-radio-button>
Trees
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="shrubs" id="shrubs" name="Options"></calcite-radio-button>
Shrubs
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="flowers" id="flowers" name="Options"></calcite-radio-button>
Flowers
</calcite-label>
<calcite-button id="submit">submit</calcite-button>
</div>
`);
const container = await page.find("div");
await container.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("shrubs");
await container.press("Tab");
expect(await getFocusedElementProp(page, "id")).toBe("submit");
});

it("focuses checked item on Tab", async () => {
const page = await newE2EPage();
await page.setContent(html`
<div>
<calcite-label layout="inline">
<calcite-radio-button value="trees" disabled id="trees" name="Options"></calcite-radio-button>
Trees
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="shrubs" id="shrubs" name="Options"></calcite-radio-button>
Shrubs
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="flowers" id="flowers" checked name="Options"></calcite-radio-button>
Flowers
</calcite-label>
<calcite-button id="submit">submit</calcite-button>
</div>
`);
const container = await page.find("div");
await container.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("flowers");
await container.press("Tab");
expect(await getFocusedElementProp(page, "id")).toBe("submit");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Element,
Event,
EventEmitter,
forceUpdate,
h,
Host,
Listen,
Expand Down Expand Up @@ -179,9 +180,11 @@ export class RadioButton
) as HTMLCalciteRadioButtonElement[];
};

isDefaultSelectable = (): boolean => {
isFocusable = (): boolean => {
const radioButtons = this.queryButtons();
return !radioButtons.some((radioButton) => radioButton.checked) && radioButtons[0] === this.el;
const firstFocusable = radioButtons.find((radiobutton) => !radiobutton.disabled);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

radiobutton ➡️ radioButton

const checked = radioButtons.find((radiobutton) => radiobutton.checked);
return firstFocusable === this.el && !checked;
};

check = (): void => {
Expand Down Expand Up @@ -271,11 +274,26 @@ export class RadioButton
});
}

private updateTabIndexOfOtherRadioButtonsInGroup(): void {
const radioButtons = this.queryButtons();
const otherFocusableRadioButtons = radioButtons.filter(
(radioButton) => radioButton.guid !== this.guid && !radioButton.disabled
);
otherFocusableRadioButtons.forEach((radiobutton) => {
forceUpdate(radiobutton);
eriklharper marked this conversation as resolved.
Show resolved Hide resolved
});
}
eriklharper marked this conversation as resolved.
Show resolved Hide resolved

private getTabIndex(): number | undefined {
if (this.disabled) {
return undefined;
}
return this.checked || this.isDefaultSelectable() ? 0 : -1;
if (this.checked || this.isFocusable()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No love for the previous ternary? 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, there was some extra code in if block earlier. Will revert to ternary.

this.updateTabIndexOfOtherRadioButtonsInGroup();
eriklharper marked this conversation as resolved.
Show resolved Hide resolved
return 0;
} else {
return -1;
}
}

//--------------------------------------------------------------------------
Expand Down