From 16450d6e010c5fe3313400e0fe3a6059824ba00e Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 30 May 2018 11:26:53 +0300 Subject: [PATCH] fix(select,autocomplete): unable to set custom id on mat-option Fixes consumers not being allowed to set their own id on a `mat-option`. Fixes #11572. --- src/lib/core/option/option.spec.ts | 24 ++++++++++++++++++------ src/lib/core/option/option.ts | 7 +++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib/core/option/option.spec.ts b/src/lib/core/option/option.spec.ts index 9eceeb530e2f..c97c9ed87c41 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 = @@ -27,14 +27,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)); @@ -73,8 +84,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 75c468d49b25..5d084f3340df 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; }