Skip to content

Commit 16450d6

Browse files
committed
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.
1 parent b090f6d commit 16450d6

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/lib/core/option/option.spec.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ describe('MatOption component', () => {
99
beforeEach(async(() => {
1010
TestBed.configureTestingModule({
1111
imports: [MatOptionModule],
12-
declarations: [OptionWithDisable]
12+
declarations: [BasicOption]
1313
}).compileComponents();
1414
}));
1515

1616
it('should complete the `stateChanges` stream on destroy', () => {
17-
const fixture = TestBed.createComponent(OptionWithDisable);
17+
const fixture = TestBed.createComponent(BasicOption);
1818
fixture.detectChanges();
1919

2020
const optionInstance: MatOption =
@@ -27,14 +27,25 @@ describe('MatOption component', () => {
2727
subscription.unsubscribe();
2828
});
2929

30+
it('should be able to set a custom id', () => {
31+
const fixture = TestBed.createComponent(BasicOption);
32+
33+
fixture.componentInstance.id = 'custom-option';
34+
fixture.detectChanges();
35+
36+
const optionInstance = fixture.debugElement.query(By.directive(MatOption)).componentInstance;
37+
38+
expect(optionInstance.id).toBe('custom-option');
39+
});
40+
3041
describe('ripples', () => {
31-
let fixture: ComponentFixture<OptionWithDisable>;
42+
let fixture: ComponentFixture<BasicOption>;
3243
let optionDebugElement: DebugElement;
3344
let optionNativeElement: HTMLElement;
3445
let optionInstance: MatOption;
3546

3647
beforeEach(() => {
37-
fixture = TestBed.createComponent(OptionWithDisable);
48+
fixture = TestBed.createComponent(BasicOption);
3849
fixture.detectChanges();
3950

4051
optionDebugElement = fixture.debugElement.query(By.directive(MatOption));
@@ -73,8 +84,9 @@ describe('MatOption component', () => {
7384
});
7485

7586
@Component({
76-
template: `<mat-option [disabled]="disabled"></mat-option>`
87+
template: `<mat-option [id]="id" [disabled]="disabled"></mat-option>`
7788
})
78-
class OptionWithDisable {
89+
class BasicOption {
7990
disabled: boolean;
91+
id: string;
8092
}

src/lib/core/option/option.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,20 @@ export class MatOption implements AfterViewChecked, OnDestroy {
8888
private _selected = false;
8989
private _active = false;
9090
private _disabled = false;
91-
private _id = `mat-option-${_uniqueIdCounter++}`;
9291
private _mostRecentViewValue = '';
9392

9493
/** Whether the wrapping component is in multiple selection mode. */
9594
get multiple() { return this._parent && this._parent.multiple; }
9695

97-
/** The unique ID of the option. */
98-
get id(): string { return this._id; }
99-
10096
/** Whether or not the option is currently selected. */
10197
get selected(): boolean { return this._selected; }
10298

10399
/** The form value of the option. */
104100
@Input() value: any;
105101

102+
/** The unique ID of the option. */
103+
@Input() id: string = `mat-option-${_uniqueIdCounter++}`;
104+
106105
/** Whether the option is disabled. */
107106
@Input()
108107
get disabled() { return (this.group && this.group.disabled) || this._disabled; }

0 commit comments

Comments
 (0)