Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select,autocomplete): unable to set custom id on mat-option #11573

Merged
merged 1 commit into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 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 @@ -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 =
Expand All @@ -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 =
Expand All @@ -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<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 @@ -117,8 +128,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