Skip to content

fix(radio-button-group): no longer focus first radio button on label click and adds setFocus method. #7050

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

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
67 changes: 66 additions & 1 deletion src/components/radio-button-group/radio-button-group.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { newE2EPage } from "@stencil/core/testing";
import { accessible, defaults, hidden, reflects, renders } from "../../tests/commonTests";
import { accessible, defaults, focusable, hidden, reflects, renders } from "../../tests/commonTests";
import { html } from "../../../support/formatting";
import { getFocusedElementProp } from "../../tests/utils";

describe("calcite-radio-button-group", () => {
describe("renders", () => {
Expand All @@ -20,6 +21,22 @@ describe("calcite-radio-button-group", () => {
]);
});

describe("is focusable", () => {
focusable(
html`<calcite-radio-button-group name="Options" layout="vertical">
<calcite-label layout="inline">
<calcite-radio-button value="flowers" disabled></calcite-radio-button>
Flowers
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="trees"></calcite-radio-button>
Trees
</calcite-label>
</calcite-radio-button-group>`,
{ focusTargetSelector: "calcite-radio-button" }
);
});

describe("honors hidden attribute", () => {
hidden("calcite-radio-button");

Expand Down Expand Up @@ -452,4 +469,52 @@ describe("calcite-radio-button-group", () => {
expect(changeEvent).toHaveReceivedEventTimes(3);
expect(await getSelectedItemValue()).toBe("three");
});

it("should focus the checked radio-button on setFocus()", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-radio-button-group name="Options" layout="vertical">
<calcite-label layout="inline">
<calcite-radio-button value="trees" disabled id="trees"></calcite-radio-button>
Trees
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="shrubs" id="shrubs"></calcite-radio-button>
Shrubs
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="flowers" id="flowers" checked></calcite-radio-button>
Flowers
</calcite-label>
</calcite-radio-button-group>
`);
const group = await page.find("calcite-radio-button-group");
await group.callMethod("setFocus");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("flowers");
});

it("should focus the first focusable radio-button on setFocus()", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-radio-button-group name="Options" layout="vertical">
<calcite-label layout="inline">
<calcite-radio-button value="trees" disabled id="trees"></calcite-radio-button>
Trees
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="shrubs" id="shrubs"></calcite-radio-button>
Shrubs
</calcite-label>
<calcite-label layout="inline">
<calcite-radio-button value="flowers" id="flowers"></calcite-radio-button>
Flowers
</calcite-label>
</calcite-radio-button-group>
`);
const group = await page.find("calcite-radio-button-group");
await group.callMethod("setFocus");
await page.waitForChanges();
expect(await getFocusedElementProp(page, "id")).toBe("shrubs");
});
});
67 changes: 59 additions & 8 deletions src/components/radio-button-group/radio-button-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@ import {
h,
Host,
Listen,
Method,
Prop,
State,
VNode,
Watch
} from "@stencil/core";
import { createObserver } from "../../utils/observers";
import { Layout, Scale } from "../interfaces";
import {
componentLoaded,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent
} from "../../utils/loadable";

/**
* @slot - A slot for adding `calcite-radio-button`s.
*/
@Component({
tag: "calcite-radio-button-group",
styleUrl: "radio-button-group.scss",
shadow: {
delegatesFocus: true
}
shadow: true
})
export class RadioButtonGroup {
export class RadioButtonGroup implements LoadableComponent {
//--------------------------------------------------------------------------
//
// Element
Expand Down Expand Up @@ -91,6 +97,8 @@ export class RadioButtonGroup {

mutationObserver = createObserver("mutation", () => this.passPropsToRadioButtons());

@State() radioButtons: HTMLCalciteRadioButtonElement[] = [];

//--------------------------------------------------------------------------
//
// Lifecycle
Expand All @@ -102,6 +110,14 @@ export class RadioButtonGroup {
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}

componentWillLoad(): void {
setUpLoadableComponent(this);
}

componentDidLoad(): void {
setComponentLoaded(this);
}

disconnectedCallback(): void {
this.mutationObserver?.disconnect();
}
Expand All @@ -113,10 +129,11 @@ export class RadioButtonGroup {
//--------------------------------------------------------------------------

private passPropsToRadioButtons = (): void => {
const radioButtons = this.el.querySelectorAll("calcite-radio-button");
this.selectedItem = Array.from(radioButtons).find((radioButton) => radioButton.checked) || null;
if (radioButtons.length > 0) {
radioButtons.forEach((radioButton) => {
this.radioButtons = Array.from(this.el.querySelectorAll("calcite-radio-button"));
this.selectedItem =
Array.from(this.radioButtons).find((radioButton) => radioButton.checked) || null;
if (this.radioButtons.length > 0) {
this.radioButtons.forEach((radioButton) => {
radioButton.disabled = this.disabled || radioButton.disabled;
radioButton.hidden = this.hidden;
radioButton.name = this.name;
Expand All @@ -126,6 +143,20 @@ export class RadioButtonGroup {
}
};

private getFocusableRadioButton(): HTMLCalciteRadioButtonElement | null {
Copy link
Member

Choose a reason for hiding this comment

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

Could you use the focusFirstTabbable helper in the dom utils?

This is what the modal does...

 @Method()
  async setFocus(): Promise<void> {
    await componentLoaded(this);
    focusFirstTabbable(this.el);
  }

Would we want only 1 radio button within a group tabbable at a time? Seems like that might make for a good enhancement in the future.

Copy link
Contributor Author

@anveshmekala anveshmekala Jun 1, 2023

Choose a reason for hiding this comment

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

tried implementing focusFirstTabbable helper here, it works well if none if the radio-button's in the group are disabled. The reason for this is the way we assign tabIndex at the radio-button level. The tabIndex=0 only if the radio-button is checked or if its the first element in the group. I think this is a bug. Wonder if the assignment of tabIndex should stay at the group level not the radio-button level.

Would we want only 1 radio button within a group tabbable at a time? Seems like that might make for a good enhancement in the future.

The above is following w3 radio group pattern keyboard specs i believe. One button is tabbable and the user can use Arrow Keys to navigate between the items.

let index = 0;
let focusableEle = this.radioButtons[index];
while (focusableEle.disabled) {
index++;
if (index < this.radioButtons.length) {
focusableEle = this.radioButtons[index];
} else {
return null;
}
}
return focusableEle;
}

//--------------------------------------------------------------------------
//
// Events
Expand All @@ -137,6 +168,26 @@ export class RadioButtonGroup {
*/
@Event({ cancelable: false }) calciteRadioButtonGroupChange: EventEmitter<void>;

//--------------------------------------------------------------------------
//
// Public Method
//
//--------------------------------------------------------------------------

/** Sets focus on the fist focusable `calcite-radio-button` element in the component. */
@Method()
async setFocus(): Promise<void> {
await componentLoaded(this);
if (this.selectedItem && !this.selectedItem.disabled) {
this.selectedItem.setFocus();
return;
}
if (this.radioButtons.length > 0) {
const focusableRadioButton = this.getFocusableRadioButton();
focusableRadioButton !== null && focusableRadioButton.setFocus();
}
}

//--------------------------------------------------------------------------
//
// Event Listeners
Expand Down