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 @@ -11,6 +11,7 @@ import {
renders
} from "../../tests/commonTests";
import { html } from "../../../support/formatting";
import { getFocusedElementProp } from "../../tests/utils";

describe("calcite-radio-button", () => {
describe("renders", () => {
Expand Down Expand Up @@ -66,6 +67,102 @@ describe("calcite-radio-button", () => {
focusable("calcite-radio-button", {
shadowFocusTargetSelector: ".container"
});

it("focuses first focusable item on Tab when new radio-button is added", async () => {
const page = await newE2EPage();
await page.setContent(html`
<div>
<calcite-label layout="inline" id="1">
<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>
`);

await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("shrubs");
await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("submit");

await page.evaluate(() => {
const firstRadioButton = document.querySelector('calcite-label[id="1"]');
const newRadioButton = `<calcite-label layout="inline">
<calcite-radio-button value="plants" name="Options" id="plants"></calcite-radio-button>
Plants
</calcite-label>`;
firstRadioButton.insertAdjacentHTML("beforebegin", newRadioButton);
});

await page.keyboard.press("Tab");
await page.waitForChanges();
await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("plants");
await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("submit");

const radioButtonElement = await page.find('calcite-radio-button[id="plants"]');
radioButtonElement.setProperty("disabled", true);
await page.keyboard.press("Tab");
await page.waitForChanges();
await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("shrubs");
});

it("focuses checked item on Tab when new radio-button is added", async () => {
const page = await newE2EPage();
await page.setContent(html`
<div>
<calcite-label layout="inline" id="1">
<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" checked></calcite-radio-button>
Flowers
</calcite-label>
<calcite-button id="submit">submit</calcite-button>
</div>
`);

await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("flowers");
await page.keyboard.press("Tab");
expect(await getFocusedElementProp(page, "id")).toBe("submit");

await page.evaluate(() => {
const firstRadioButton = document.querySelector('calcite-label[id="1"]');
const newRadioButton = `<calcite-label layout="inline">
<calcite-radio-button value="plants" name="Options" id="plants"></calcite-radio-button>
Plants
</calcite-label>`;
firstRadioButton.insertAdjacentHTML("beforebegin", newRadioButton);
});

await page.keyboard.press("Tab");
await page.waitForChanges();
await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("flowers");
});
});

describe("reflects", () => {
Expand Down
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 @@ -73,6 +74,11 @@ export class RadioButton
/** When `true`, interaction is prevented and the component is displayed with lower opacity. */
@Prop({ reflect: true }) disabled = false;

@Watch("disabled")
disabledChanged(): void {
this.updateTabIndexOfOtherRadioButtonsInGroup();
}

/**
* The focused state of the component.
*
Expand All @@ -94,6 +100,11 @@ export class RadioButton
/** When `true`, the component is not displayed and is not focusable or checkable. */
@Prop({ reflect: true }) hidden = false;
Copy link
Member

Choose a reason for hiding this comment

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

Sidebar: we should revisit custom hidden props since it's a reflected prop on HTMLElement.


@Watch("hidden")
hiddenChanged(): void {
this.updateTabIndexOfOtherRadioButtonsInGroup();
}

/**
* The hovered state of the component.
*
Expand Down Expand Up @@ -184,9 +195,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 @@ -291,11 +304,25 @@ 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.

return 0;
} else {
return -1;
}
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -446,6 +473,7 @@ export class RadioButton
connectInteractive(this);
connectLabel(this);
connectForm(this);
this.updateTabIndexOfOtherRadioButtonsInGroup();
}

componentWillLoad(): void {
Expand All @@ -464,6 +492,7 @@ export class RadioButton
disconnectInteractive(this);
disconnectLabel(this);
disconnectForm(this);
this.updateTabIndexOfOtherRadioButtonsInGroup();
}

componentDidRender(): void {
Expand Down