From 29d517361e0f46d77a1af4d4c95703649da98eea Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 24 Aug 2018 16:59:45 +0200 Subject: [PATCH] fix(select,autocomplete): unable to set custom id on mat-option (#11573) Fixes consumers not being allowed to set their own id on a `mat-option`. Fixes #11572. --- src/lib/core/option/option.spec.ts | 28 ++++++++++++++++++++-------- src/lib/core/option/option.ts | 7 +++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/lib/core/option/option.spec.ts b/src/lib/core/option/option.spec.ts index d1b4949d3b0b..dcab92f68961 100644 --- a/src/lib/core/option/option.spec.ts +++ b/src/lib/core/option/option.spec.ts @@ -9,12 +9,12 @@ describe('MatOption component', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [MatOptionModule], - declarations: [OptionWithDisable] + declarations: [BasicOption] }).compileComponents(); })); it('should complete the `stateChanges` stream on destroy', () => { - const fixture = TestBed.createComponent(OptionWithDisable); + const fixture = TestBed.createComponent(BasicOption); fixture.detectChanges(); const optionInstance: MatOption = @@ -28,7 +28,7 @@ describe('MatOption component', () => { }); it('should not emit to `onSelectionChange` if selecting an already-selected option', () => { - const fixture = TestBed.createComponent(OptionWithDisable); + const fixture = TestBed.createComponent(BasicOption); fixture.detectChanges(); const optionInstance: MatOption = @@ -50,7 +50,7 @@ describe('MatOption component', () => { }); it('should not emit to `onSelectionChange` if deselecting an unselected option', () => { - const fixture = TestBed.createComponent(OptionWithDisable); + const fixture = TestBed.createComponent(BasicOption); fixture.detectChanges(); const optionInstance: MatOption = @@ -71,14 +71,25 @@ describe('MatOption component', () => { subscription.unsubscribe(); }); + it('should be able to set a custom id', () => { + const fixture = TestBed.createComponent(BasicOption); + + fixture.componentInstance.id = 'custom-option'; + fixture.detectChanges(); + + const optionInstance = fixture.debugElement.query(By.directive(MatOption)).componentInstance; + + expect(optionInstance.id).toBe('custom-option'); + }); + describe('ripples', () => { - let fixture: ComponentFixture; + let fixture: ComponentFixture; let optionDebugElement: DebugElement; let optionNativeElement: HTMLElement; let optionInstance: MatOption; beforeEach(() => { - fixture = TestBed.createComponent(OptionWithDisable); + fixture = TestBed.createComponent(BasicOption); fixture.detectChanges(); optionDebugElement = fixture.debugElement.query(By.directive(MatOption)); @@ -117,8 +128,9 @@ describe('MatOption component', () => { }); @Component({ - template: `` + template: `` }) -class OptionWithDisable { +class BasicOption { disabled: boolean; + id: string; } diff --git a/src/lib/core/option/option.ts b/src/lib/core/option/option.ts index 3b0339c1804d..cd6412980058 100644 --- a/src/lib/core/option/option.ts +++ b/src/lib/core/option/option.ts @@ -88,21 +88,20 @@ export class MatOption implements AfterViewChecked, OnDestroy { private _selected = false; private _active = false; private _disabled = false; - private _id = `mat-option-${_uniqueIdCounter++}`; private _mostRecentViewValue = ''; /** Whether the wrapping component is in multiple selection mode. */ get multiple() { return this._parent && this._parent.multiple; } - /** The unique ID of the option. */ - get id(): string { return this._id; } - /** Whether or not the option is currently selected. */ get selected(): boolean { return this._selected; } /** The form value of the option. */ @Input() value: any; + /** The unique ID of the option. */ + @Input() id: string = `mat-option-${_uniqueIdCounter++}`; + /** Whether the option is disabled. */ @Input() get disabled() { return (this.group && this.group.disabled) || this._disabled; }