Skip to content

Commit

Permalink
fix(select,autocomplete): unable to set custom id on mat-option
Browse files Browse the repository at this point in the history
Fixes consumers not being allowed to set their own id on a `mat-option`.

Fixes angular#11572.
  • Loading branch information
crisbeto committed May 30, 2018
1 parent b090f6d commit 16450d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
24 changes: 18 additions & 6 deletions src/lib/core/option/option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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<OptionWithDisable>;
let fixture: ComponentFixture<BasicOption>;
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));
Expand Down Expand Up @@ -73,8 +84,9 @@ describe('MatOption component', () => {
});

@Component({
template: `<mat-option [disabled]="disabled"></mat-option>`
template: `<mat-option [id]="id" [disabled]="disabled"></mat-option>`
})
class OptionWithDisable {
class BasicOption {
disabled: boolean;
id: string;
}
7 changes: 3 additions & 4 deletions src/lib/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit 16450d6

Please sign in to comment.