Skip to content

fix(cdk/a11y): allow mixed types to be passed into setActiveItem #31462

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
Jul 1, 2025
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
3 changes: 3 additions & 0 deletions goldens/cdk/a11y/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class A11yModule {
export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable & T> {
setActiveItem(index: number): void;
setActiveItem(item: T): void;
setActiveItem(item: T | number): void;
}

// @public
Expand Down Expand Up @@ -169,6 +170,7 @@ export interface FocusableOption extends ListKeyManagerOption {
export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
setActiveItem(index: number): void;
setActiveItem(item: T): void;
setActiveItem(item: T | number): void;
setFocusOrigin(origin: FocusOrigin): this;
}

Expand Down Expand Up @@ -366,6 +368,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
onKeydown(event: KeyboardEvent): void;
setActiveItem(index: number): void;
setActiveItem(item: T): void;
setActiveItem(item: T | number): void;
setFirstItemActive(): void;
setLastItemActive(): void;
setNextItemActive(): void;
Expand Down
7 changes: 7 additions & 0 deletions src/cdk/a11y/key-manager/activedescendant-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export class ActiveDescendantKeyManager<T> extends ListKeyManager<Highlightable
* @param item Item to be set as active.
*/
override setActiveItem(item: T): void;
/**
* Sets the active item to the item to the specified one and adds the
* active styles to the it. Also removes active styles from the
* previously active item.
* @param item Item to be set as active.
*/
override setActiveItem(item: T | number): void;

override setActiveItem(index: any): void {
if (this.activeItem) {
Expand Down
6 changes: 6 additions & 0 deletions src/cdk/a11y/key-manager/focus-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
*/
override setActiveItem(item: T): void;

/**
* Sets the active item to the item that is specified and focuses it.
* @param item Item to be set as active.
*/
override setActiveItem(item: T | number): void;

override setActiveItem(item: any): void {
super.setActiveItem(item);

Expand Down
6 changes: 6 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
*/
setActiveItem(item: T): void;

/**
* Sets the active item to the specified item.
* @param item The item to be set as active.
*/
setActiveItem(item: T | number): void;

setActiveItem(item: any): void {
const previousActiveItem = this._activeItem();

Expand Down
8 changes: 1 addition & 7 deletions src/cdk/menu/menu-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,7 @@ export abstract class CdkMenuBase
* @param item The index of the item to be set as active, or the CdkMenuItem instance.
*/
setActiveMenuItem(item: number | CdkMenuItem) {
if (this.keyManager) {
if (typeof item === 'number') {
this.keyManager.setActiveItem(item);
} else {
this.keyManager.setActiveItem(item);
}
}
this.keyManager?.setActiveItem(item);
}

/** Gets the tabindex for this menu. */
Expand Down
Loading