Skip to content

feat(accordion-item): add setFocus #9364

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 1 commit into from
May 20, 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
4 changes: 4 additions & 0 deletions packages/calcite-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export namespace Components {
* Specifies the size of the component inherited from the `calcite-accordion`.
*/
"scale": Scale;
/**
* Sets focus on the component.
*/
"setFocus": () => Promise<void>;
}
interface CalciteAction {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { newE2EPage } from "@stencil/core/testing";
import { accessible, renders, slots, hidden } from "../../tests/commonTests";
import { accessible, renders, slots, hidden, focusable } from "../../tests/commonTests";
import { html } from "../../../support/formatting";
import { CSS, IDS, SLOTS } from "./resources";

Expand All @@ -20,6 +20,10 @@ describe("calcite-accordion-item", () => {
slots("calcite-accordion-item", SLOTS);
});

describe("is focusable", () => {
focusable("calcite-accordion-item");
});

it("properly uses ARIA and roles", async () => {
// this test covers a11y relationships not reported by axe-core/accessible test helper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
h,
Host,
Listen,
Method,
Prop,
VNode,
} from "@stencil/core";
Expand All @@ -23,6 +24,7 @@ import {
import { CSS_UTILITY } from "../../utils/resources";
import { getIconScale } from "../../utils/component";
import { FlipContext, Position, Scale, SelectionMode } from "../interfaces";
import { componentFocusable } from "../../utils/component";
import { SLOTS, CSS, IDS } from "./resources";
import { RequestedItem } from "./interfaces";

Expand Down Expand Up @@ -180,6 +182,7 @@ export class AccordionItem implements ConditionalSlotComponent {
class={CSS.headerContent}
id={IDS.sectionToggle}
onClick={this.itemHeaderClickHandler}
ref={this.storeHeaderEl}
role="button"
tabindex="0"
>
Expand Down Expand Up @@ -286,12 +289,31 @@ export class AccordionItem implements ConditionalSlotComponent {

@Element() el: HTMLCalciteAccordionItemElement;

private headerEl: HTMLDivElement;

//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------

/** Sets focus on the component. */
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this.el);
this.headerEl.focus();
}

//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------

private storeHeaderEl = (el: HTMLDivElement): void => {
this.headerEl = el;
};

/** handle clicks on item header */
private itemHeaderClickHandler = (): void => this.emitRequestedItem();

Expand Down
Loading