Skip to content

Commit 29d5173

Browse files
crisbetojelbourn
authored andcommitted
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.
1 parent 625f792 commit 29d5173

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

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

Lines changed: 20 additions & 8 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 =
@@ -28,7 +28,7 @@ describe('MatOption component', () => {
2828
});
2929

3030
it('should not emit to `onSelectionChange` if selecting an already-selected option', () => {
31-
const fixture = TestBed.createComponent(OptionWithDisable);
31+
const fixture = TestBed.createComponent(BasicOption);
3232
fixture.detectChanges();
3333

3434
const optionInstance: MatOption =
@@ -50,7 +50,7 @@ describe('MatOption component', () => {
5050
});
5151

5252
it('should not emit to `onSelectionChange` if deselecting an unselected option', () => {
53-
const fixture = TestBed.createComponent(OptionWithDisable);
53+
const fixture = TestBed.createComponent(BasicOption);
5454
fixture.detectChanges();
5555

5656
const optionInstance: MatOption =
@@ -71,14 +71,25 @@ describe('MatOption component', () => {
7171
subscription.unsubscribe();
7272
});
7373

74+
it('should be able to set a custom id', () => {
75+
const fixture = TestBed.createComponent(BasicOption);
76+
77+
fixture.componentInstance.id = 'custom-option';
78+
fixture.detectChanges();
79+
80+
const optionInstance = fixture.debugElement.query(By.directive(MatOption)).componentInstance;
81+
82+
expect(optionInstance.id).toBe('custom-option');
83+
});
84+
7485
describe('ripples', () => {
75-
let fixture: ComponentFixture<OptionWithDisable>;
86+
let fixture: ComponentFixture<BasicOption>;
7687
let optionDebugElement: DebugElement;
7788
let optionNativeElement: HTMLElement;
7889
let optionInstance: MatOption;
7990

8091
beforeEach(() => {
81-
fixture = TestBed.createComponent(OptionWithDisable);
92+
fixture = TestBed.createComponent(BasicOption);
8293
fixture.detectChanges();
8394

8495
optionDebugElement = fixture.debugElement.query(By.directive(MatOption));
@@ -117,8 +128,9 @@ describe('MatOption component', () => {
117128
});
118129

119130
@Component({
120-
template: `<mat-option [disabled]="disabled"></mat-option>`
131+
template: `<mat-option [id]="id" [disabled]="disabled"></mat-option>`
121132
})
122-
class OptionWithDisable {
133+
class BasicOption {
123134
disabled: boolean;
135+
id: string;
124136
}

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)