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(cdk/listbox): unable to tab in if active option is removed #28583

Merged
merged 1 commit into from
Feb 13, 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
35 changes: 29 additions & 6 deletions src/cdk/listbox/listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ describe('CdkOption and CdkListbox', () => {

expect(optionEls[0].getAttribute('tabindex')).toBe('10');
});

it('should reset the tabindex if the active option is destroyed', () => {
const {fixture, listbox, listboxEl} = setupComponent(ListboxWithOptions);
let options = fixture.nativeElement.querySelectorAll('.cdk-option');
expect(listboxEl.getAttribute('tabindex')).toBe('0');
expect(options[0].getAttribute('tabindex')).toBe('-1');

listbox.focus();
fixture.detectChanges();

expect(listboxEl.getAttribute('tabindex')).toBe('-1');
expect(options[0].getAttribute('tabindex')).toBe('0');

fixture.componentInstance.appleRendered = false;
fixture.detectChanges();
options = fixture.nativeElement.querySelectorAll('.cdk-option');

expect(listboxEl.getAttribute('tabindex')).toBe('0');
expect(options[0].getAttribute('tabindex')).toBe('-1');
});
});

describe('selection', () => {
Expand Down Expand Up @@ -943,12 +963,14 @@ describe('CdkOption and CdkListbox', () => {
[cdkListboxNavigatesDisabledOptions]="!navigationSkipsDisabled"
[cdkListboxValue]="selectedValue"
(cdkListboxValueChange)="onSelectionChange($event)">
<div cdkOption="apple"
[cdkOptionDisabled]="isAppleDisabled"
[id]="appleId"
[tabindex]="appleTabindex">
Apple
</div>
@if (appleRendered) {
<div cdkOption="apple"
[cdkOptionDisabled]="isAppleDisabled"
[id]="appleId"
[tabindex]="appleTabindex">
Apple
</div>
}
<div cdkOption="orange" [cdkOptionDisabled]="isOrangeDisabled">Orange
</div>
<div cdkOption="banana">Banana</div>
Expand All @@ -965,6 +987,7 @@ class ListboxWithOptions {
isActiveDescendant = false;
navigationWraps = true;
navigationSkipsDisabled = true;
appleRendered = true;
listboxId: string;
listboxTabindex: number;
appleId: string;
Expand Down
11 changes: 11 additions & 0 deletions src/cdk/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,17 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
}

this.listKeyManager.change.subscribe(() => this._focusActiveOption());

this.options.changes.pipe(takeUntil(this.destroyed)).subscribe(() => {
const activeOption = this.listKeyManager.activeItem;

// If the active option was deleted, we need to reset
// the key manager so it can allow focus back in.
if (activeOption && !this.options.find(option => option === activeOption)) {
this.listKeyManager.setActiveItem(-1);
this.changeDetectorRef.markForCheck();
}
});
}

/** Focus the active option. */
Expand Down
Loading