Skip to content

Commit 651440f

Browse files
karammalerba
authored andcommitted
fix(select): support use inside a custom value accessor (#2704)
Fixes #2609
1 parent 10bd6da commit 651440f

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/lib/select/select.spec.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
66
import {MdSelect} from './select';
77
import {MdOption} from '../core/option/option';
88
import {Dir} from '../core/rtl/dir';
9-
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
9+
import {
10+
ControlValueAccessor, FormControl, FormsModule, NG_VALUE_ACCESSOR, ReactiveFormsModule
11+
} from '@angular/forms';
1012
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1113

1214
describe('MdSelect', () => {
@@ -22,7 +24,9 @@ describe('MdSelect', () => {
2224
ManySelects,
2325
NgIfSelect,
2426
SelectInitWithoutOptions,
25-
SelectWithChangeEvent
27+
SelectWithChangeEvent,
28+
CustomSelectAccessor,
29+
CompWithCustomSelect
2630
],
2731
providers: [
2832
{provide: OverlayContainer, useFactory: () => {
@@ -539,6 +543,20 @@ describe('MdSelect', () => {
539543

540544
});
541545

546+
describe('misc forms', () => {
547+
it('should support use inside a custom value accessor', () => {
548+
const fixture = TestBed.createComponent(CompWithCustomSelect);
549+
spyOn(fixture.componentInstance.customAccessor, 'writeValue');
550+
fixture.detectChanges();
551+
552+
expect(fixture.componentInstance.customAccessor.select._control)
553+
.toBe(null, 'Expected md-select NOT to inherit control from parent value accessor.');
554+
expect(fixture.componentInstance.customAccessor.writeValue).toHaveBeenCalled();
555+
});
556+
557+
});
558+
559+
542560
describe('animations', () => {
543561
let fixture: ComponentFixture<BasicSelect>;
544562
let trigger: HTMLElement;
@@ -1379,6 +1397,43 @@ class SelectInitWithoutOptions {
13791397
}
13801398
}
13811399

1400+
@Component({
1401+
selector: 'custom-select-accessor',
1402+
template: `
1403+
<md-select></md-select>
1404+
`,
1405+
providers: [{
1406+
provide: NG_VALUE_ACCESSOR,
1407+
useExisting: CustomSelectAccessor,
1408+
multi: true
1409+
}]
1410+
})
1411+
class CustomSelectAccessor implements ControlValueAccessor {
1412+
@ViewChild(MdSelect) select: MdSelect;
1413+
1414+
writeValue(val: any): void {}
1415+
registerOnChange(fn: (val: any) => void): void {}
1416+
registerOnTouched(fn: Function): void {}
1417+
}
1418+
1419+
@Component({
1420+
selector: 'comp-with-custom-select',
1421+
template: `
1422+
<custom-select-accessor [formControl]="ctrl">
1423+
</custom-select-accessor>
1424+
`,
1425+
providers: [{
1426+
provide: NG_VALUE_ACCESSOR,
1427+
useExisting: CustomSelectAccessor,
1428+
multi: true
1429+
}]
1430+
})
1431+
class CompWithCustomSelect {
1432+
ctrl = new FormControl('initial value');
1433+
@ViewChild(CustomSelectAccessor) customAccessor: CustomSelectAccessor;
1434+
}
1435+
1436+
13821437
/**
13831438
* TODO: Move this to core testing utility until Angular has event faking
13841439
* support.

src/lib/select/select.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Output,
1111
QueryList,
1212
Renderer,
13+
Self,
1314
ViewEncapsulation,
1415
ViewChild,
1516
} from '@angular/core';
@@ -232,7 +233,7 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
232233

233234
constructor(private _element: ElementRef, private _renderer: Renderer,
234235
private _viewportRuler: ViewportRuler, @Optional() private _dir: Dir,
235-
@Optional() public _control: NgControl) {
236+
@Self() @Optional() public _control: NgControl) {
236237
if (this._control) {
237238
this._control.valueAccessor = this;
238239
}

0 commit comments

Comments
 (0)