Skip to content

fix(angular/autocomplete): optionSelections not emitting when the list of options changes #1033

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
Jan 17, 2022
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
14 changes: 9 additions & 5 deletions src/angular/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,16 @@ export class SbbAutocompleteTrigger
);
}

/** Stream of autocomplete option selections. */
/** Stream of changes to the selection state of the autocomplete options. */
readonly optionSelections: Observable<SbbOptionSelectionChange> = defer(() => {
if (this.autocomplete && this.autocomplete.options) {
return merge(...this.autocomplete.options.map((option) => option.onSelectionChange));
}
const options = this.autocomplete ? this.autocomplete.options : null;

if (options) {
return options.changes.pipe(
startWith(options),
switchMap(() => merge(...options.map((option) => option.onSelectionChange)))
);
}
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
// Return a stream that we'll replace with the real one once everything is in place.
return this._zone.onStable.pipe(
Expand Down Expand Up @@ -429,7 +433,7 @@ export class SbbAutocompleteTrigger

// Implemented as part of ControlValueAccessor.
writeValue(value: any): void {
Promise.resolve(null).then(() => this._setTriggerValue(value));
Promise.resolve().then(() => this._setTriggerValue(value));
}

// Implemented as part of ControlValueAccessor.
Expand Down
28 changes: 28 additions & 0 deletions src/angular/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2770,6 +2770,34 @@ describe('SbbAutocomplete', () => {
subscription!.unsubscribe();
}));

it('should emit to `optionSelections` if the list of options changes', fakeAsync(() => {
const spy = jasmine.createSpy('option selection spy');
const subscription = fixture.componentInstance.trigger.optionSelections.subscribe(spy);
const openAndSelectFirstOption = () => {
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
(overlayContainerElement.querySelector('sbb-option') as HTMLElement).click();
fixture.detectChanges();
zone.simulateZoneExit();
};

fixture.componentInstance.numbers = [{ code: 'OR', name: 'Oregon' }];
fixture.detectChanges();

openAndSelectFirstOption();
expect(spy).toHaveBeenCalledTimes(1);

fixture.componentInstance.numbers = [{ code: 'WV', name: 'West Virginia' }];
fixture.detectChanges();

openAndSelectFirstOption();
expect(spy).toHaveBeenCalledTimes(2);

subscription!.unsubscribe();
}));

it('should reposition the panel when the amount of options changes', fakeAsync(() => {
const formField = fixture.debugElement.query(By.css('.sbb-form-field'))!.nativeElement;
const input = formField.querySelector('input');
Expand Down