Skip to content

fix(material/autocomplete): don't reset active option if list of options changes #16616

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,32 @@ describe('MDC-based MatAutocomplete', () => {
componentOptions.slice(1).forEach(option => expect(option.deselect).not.toHaveBeenCalled());
}));

it('should not reset the active item if the options list changes while open', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
fixture.detectChanges();

const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
tick();

const classList = overlayContainerElement.querySelector('mat-option')!.classList;
expect(classList)
.withContext('Expected first option to be highlighted.')
.toContain('mat-mdc-option-active');

fixture.componentInstance.states.push({code: 'PR', name: 'Puerto Rico'});
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(classList)
.withContext('Expected first option to stay highlighted.')
.toContain('mat-mdc-option-active');
}));

it('should be able to preselect the first option', fakeAsync(() => {
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
fixture.componentInstance.trigger.openPanel();
Expand All @@ -2276,6 +2302,7 @@ describe('MDC-based MatAutocomplete', () => {
testComponent.trigger.autocomplete.autoActiveFirstOption = true;
testComponent.states[0].disabled = true;
testComponent.states[1].disabled = true;
fixture.detectChanges();
testComponent.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
Expand Down
3 changes: 2 additions & 1 deletion src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ export abstract class _MatAutocompleteTriggerBase
// that were created, and flatten it so our stream only emits closing events...
switchMap(() => {
const wasOpen = this.panelOpen;
this._resetActiveItem();
this.autocomplete._setVisibility();
this._changeDetectorRef.detectChanges();

Expand All @@ -539,6 +538,7 @@ export abstract class _MatAutocompleteTriggerBase
// can happen if the users opens the panel and there are no options, but the
// options come in slightly later or as a result of the value changing.
if (wasOpen !== this.panelOpen) {
this._resetActiveItem();
this.autocomplete.opened.emit();
}
}
Expand Down Expand Up @@ -653,6 +653,7 @@ export abstract class _MatAutocompleteTriggerBase
// We need to do an extra `panelOpen` check in here, because the
// autocomplete won't be shown if there are no options.
if (this.panelOpen && wasOpen !== this.panelOpen) {
this._resetActiveItem();
this.autocomplete.opened.emit();
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,32 @@ describe('MatAutocomplete', () => {
componentOptions.slice(1).forEach(option => expect(option.deselect).not.toHaveBeenCalled());
}));

it('should not reset the active item if the options list changes while open', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
fixture.detectChanges();

const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
tick();

const classList = overlayContainerElement.querySelector('mat-option')!.classList;
expect(classList)
.withContext('Expected first option to be highlighted.')
.toContain('mat-active');

fixture.componentInstance.states.push({code: 'PR', name: 'Puerto Rico'});
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(classList)
.withContext('Expected first option to stay highlighted.')
.toContain('mat-active');
}));

it('should be able to preselect the first option', fakeAsync(() => {
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
fixture.componentInstance.trigger.openPanel();
Expand All @@ -2261,6 +2287,7 @@ describe('MatAutocomplete', () => {
testComponent.trigger.autocomplete.autoActiveFirstOption = true;
testComponent.states[0].disabled = true;
testComponent.states[1].disabled = true;
fixture.detectChanges();
testComponent.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
Expand Down
Loading