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): support use inside a custom value accessor #2704

Merged
merged 1 commit into from
Jan 19, 2017
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
59 changes: 57 additions & 2 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
import {MdSelect} from './select';
import {MdOption} from '../core/option/option';
import {Dir} from '../core/rtl/dir';
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {
ControlValueAccessor, FormControl, FormsModule, NG_VALUE_ACCESSOR, ReactiveFormsModule
} from '@angular/forms';
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';

describe('MdSelect', () => {
Expand All @@ -22,7 +24,9 @@ describe('MdSelect', () => {
ManySelects,
NgIfSelect,
SelectInitWithoutOptions,
SelectWithChangeEvent
SelectWithChangeEvent,
CustomSelectAccessor,
CompWithCustomSelect
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -517,6 +521,20 @@ describe('MdSelect', () => {

});

describe('misc forms', () => {
it('should support use inside a custom value accessor', () => {
const fixture = TestBed.createComponent(CompWithCustomSelect);
spyOn(fixture.componentInstance.customAccessor, 'writeValue');
fixture.detectChanges();

expect(fixture.componentInstance.customAccessor.select._control)
.toBe(null, 'Expected md-select NOT to inherit control from parent value accessor.');
expect(fixture.componentInstance.customAccessor.writeValue).toHaveBeenCalled();
});

});


describe('animations', () => {
let fixture: ComponentFixture<BasicSelect>;
let trigger: HTMLElement;
Expand Down Expand Up @@ -1357,6 +1375,43 @@ class SelectInitWithoutOptions {
}
}

@Component({
selector: 'custom-select-accessor',
template: `
<md-select></md-select>
`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: CustomSelectAccessor,
multi: true
}]
})
class CustomSelectAccessor implements ControlValueAccessor {
@ViewChild(MdSelect) select: MdSelect;

writeValue(val: any): void {}
registerOnChange(fn: (val: any) => void): void {}
registerOnTouched(fn: Function): void {}
}

@Component({
selector: 'comp-with-custom-select',
template: `
<custom-select-accessor [formControl]="ctrl">
</custom-select-accessor>
`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: CustomSelectAccessor,
multi: true
}]
})
class CompWithCustomSelect {
ctrl = new FormControl('initial value');
@ViewChild(CustomSelectAccessor) customAccessor: CustomSelectAccessor;
}


/**
* TODO: Move this to core testing utility until Angular has event faking
* support.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Output,
QueryList,
Renderer,
Self,
ViewEncapsulation,
ViewChild,
} from '@angular/core';
Expand Down Expand Up @@ -232,7 +233,7 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr

constructor(private _element: ElementRef, private _renderer: Renderer,
private _viewportRuler: ViewportRuler, @Optional() private _dir: Dir,
@Optional() public _control: NgControl) {
@Self() @Optional() public _control: NgControl) {
if (this._control) {
this._control.valueAccessor = this;
}
Expand Down