feat(forms): made forms works with single controls
This commit is contained in:
18
modules/angular2/src/forms/directives.js
vendored
18
modules/angular2/src/forms/directives.js
vendored
@ -59,20 +59,20 @@ export class CheckboxControlValueAccessor {
|
|||||||
lifecycle: [onChange],
|
lifecycle: [onChange],
|
||||||
selector: '[control]',
|
selector: '[control]',
|
||||||
bind: {
|
bind: {
|
||||||
'controlName' : 'control'
|
'controlOrName' : 'control'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
export class ControlDirective {
|
export class ControlDirective {
|
||||||
_groupDirective:ControlGroupDirective;
|
_groupDirective:ControlGroupDirective;
|
||||||
|
|
||||||
controlName:string;
|
controlOrName:any;
|
||||||
valueAccessor:any; //ControlValueAccessor
|
valueAccessor:any; //ControlValueAccessor
|
||||||
|
|
||||||
validator:Function;
|
validator:Function;
|
||||||
|
|
||||||
constructor(@Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
|
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
|
||||||
this._groupDirective = groupDirective;
|
this._groupDirective = groupDirective;
|
||||||
this.controlName = null;
|
this.controlOrName = null;
|
||||||
this.valueAccessor = valueAccessor;
|
this.valueAccessor = valueAccessor;
|
||||||
this.validator = Validators.nullValidator;
|
this.validator = Validators.nullValidator;
|
||||||
}
|
}
|
||||||
@ -84,7 +84,9 @@ export class ControlDirective {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_initialize() {
|
_initialize() {
|
||||||
this._groupDirective.addDirective(this);
|
if(isPresent(this._groupDirective)) {
|
||||||
|
this._groupDirective.addDirective(this);
|
||||||
|
}
|
||||||
|
|
||||||
var c = this._control();
|
var c = this._control();
|
||||||
c.validator = Validators.compose([c.validator, this.validator]);
|
c.validator = Validators.compose([c.validator, this.validator]);
|
||||||
@ -102,7 +104,11 @@ export class ControlDirective {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_control() {
|
_control() {
|
||||||
return this._groupDirective.findControl(this.controlName);
|
if (isString(this.controlOrName)) {
|
||||||
|
return this._groupDirective.findControl(this.controlOrName);
|
||||||
|
} else {
|
||||||
|
return this.controlOrName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
modules/angular2/test/forms/integration_spec.js
vendored
20
modules/angular2/test/forms/integration_spec.js
vendored
@ -111,6 +111,24 @@ export function main() {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it("should work with single controls", inject([AsyncTestCompleter], (async) => {
|
||||||
|
var control = new Control("loginValue");
|
||||||
|
var ctx = new MyComp(control);
|
||||||
|
|
||||||
|
var t = `<div><input type="text" [control]="form"></div>`;
|
||||||
|
|
||||||
|
compile(MyComp, t, ctx, (view) => {
|
||||||
|
var input = queryView(view, "input")
|
||||||
|
expect(input.value).toEqual("loginValue");
|
||||||
|
|
||||||
|
input.value = "updatedValue";
|
||||||
|
dispatchEvent(input, "change");
|
||||||
|
|
||||||
|
expect(control.value).toEqual("updatedValue");
|
||||||
|
async.done();
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
it("should update DOM elements when rebinding the control group", inject([AsyncTestCompleter], (async) => {
|
it("should update DOM elements when rebinding the control group", inject([AsyncTestCompleter], (async) => {
|
||||||
var form = new ControlGroup({
|
var form = new ControlGroup({
|
||||||
"login": new Control("oldValue")
|
"login": new Control("oldValue")
|
||||||
@ -376,7 +394,7 @@ export function main() {
|
|||||||
selector: "my-comp"
|
selector: "my-comp"
|
||||||
})
|
})
|
||||||
class MyComp {
|
class MyComp {
|
||||||
form:ControlGroup;
|
form:any;
|
||||||
name:string;
|
name:string;
|
||||||
|
|
||||||
constructor(form = null, name = null) {
|
constructor(form = null, name = null) {
|
||||||
|
Reference in New Issue
Block a user