Skip to content

bug(autocomplete): deselect the other options when the user select an option #3690

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 2 commits into from
Mar 28, 2017
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
12 changes: 12 additions & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,25 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
*/
private _setValueAndClose(event: MdOptionSelectionChange | null): void {
if (event) {
this._clearPreviousSelectedOption(event.source);
this._setTriggerValue(event.source.value);
this._onChange(event.source.value);
}

this.closePanel();
}

/**
* Clear any previous selected option and emit a selection change event for this option
*/
private _clearPreviousSelectedOption(skip: MdOption) {
this.autocomplete.options.forEach((option) => {
if (option != skip && option.selected) {
option.deselect();
}
});
}

private _createOverlay(): void {
this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef);
this._overlayRef = this._overlay.create(this._getOverlayConfig());
Expand Down
63 changes: 63 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,69 @@ describe('MdAutocomplete', () => {

});

describe('Option selection', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;

beforeEach(() => {
fixture = TestBed.createComponent(SimpleAutocomplete);
fixture.detectChanges();

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
});

it('should deselect any other selected option', async(() => {
let options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
options[0].click();
fixture.detectChanges();

fixture.whenStable().then(() => {
fixture.detectChanges();

let componentOptions = fixture.componentInstance.options.toArray();
expect(componentOptions[0].selected)
.toBe(true, `Clicked option should be selected.`);

options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
options[1].click();
fixture.detectChanges();

expect(componentOptions[0].selected)
.toBe(false, `Previous option should not be selected.`);
expect(componentOptions[1].selected)
.toBe(true, `New Clicked option should be selected.`);

});
}));

it('should call deselect only on the previous selected option', async(() => {
let options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
options[0].click();
fixture.detectChanges();

fixture.whenStable().then(() => {
fixture.detectChanges();

let componentOptions = fixture.componentInstance.options.toArray();
componentOptions.forEach(option => spyOn(option, 'deselect'));

expect(componentOptions[0].selected)
.toBe(true, `Clicked option should be selected.`);

options =
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
options[1].click();
fixture.detectChanges();

expect(componentOptions[0].deselect).toHaveBeenCalled();
componentOptions.slice(1).forEach(option => expect(option.deselect).not.toHaveBeenCalled());
});
}));
});

describe('misc', () => {

it('should allow basic use without any forms directives', () => {
Expand Down