Skip to content

Commit 3f7ebde

Browse files
committed
feat(forms): changed all form directives to have basic control attributes
1 parent 4656c6f commit 3f7ebde

File tree

10 files changed

+139
-10
lines changed

10 files changed

+139
-10
lines changed

modules/angular2/angular2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export * from './directives';
7171

7272
export {
7373
AbstractControl,
74+
AbstractControlDirective,
7475
Control,
7576
ControlGroup,
7677
ControlArray,

modules/angular2/forms.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
export {AbstractControl, Control, ControlGroup, ControlArray} from './src/forms/model';
1717

18+
export {AbstractControlDirective} from './src/forms/directives/abstract_control_directive';
1819
export {NgControlName} from './src/forms/directives/ng_control_name';
1920
export {NgFormControl} from './src/forms/directives/ng_form_control';
2021
export {NgModel} from './src/forms/directives/ng_model';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {AbstractControl} from '../model';
2+
3+
export class AbstractControlDirective {
4+
get control(): AbstractControl { return null; }
5+
6+
get value(): any { return this.control.value; }
7+
8+
get valid(): boolean { return this.control.valid; }
9+
10+
get errors(): StringMap<string, any> { return this.control.errors; }
11+
12+
get pristine(): boolean { return this.control.pristine; }
13+
14+
get dirty(): boolean { return this.control.dirty; }
15+
16+
get touched(): boolean { return this.control.touched; }
17+
18+
get untouched(): boolean { return this.control.untouched; }
19+
}

modules/angular2/src/forms/directives/control_container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {Form} from './form_interface';
2+
import {AbstractControlDirective} from './abstract_control_directive';
23
import {List} from 'angular2/src/facade/collection';
34

45
/**
56
* A directive that contains a group of [NgControl].
67
*
78
* Only used by the forms module.
89
*/
9-
export class ControlContainer {
10+
export class ControlContainer extends AbstractControlDirective {
1011
name: string;
1112
get formDirective(): Form { return null; }
1213
get path(): List<string> { return null; }

modules/angular2/src/forms/directives/form_interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {NgControl} from './ng_control';
22
import {NgControlGroup} from './ng_control_group';
3-
import {Control} from '../model';
3+
import {Control, ControlGroup} from '../model';
44

55
/**
66
* An interface that {@link NgFormModel} and {@link NgForm} implement.
@@ -13,5 +13,6 @@ export interface Form {
1313
getControl(dir: NgControl): Control;
1414
addControlGroup(dir: NgControlGroup): void;
1515
removeControlGroup(dir: NgControlGroup): void;
16+
getControlGroup(dir: NgControlGroup): ControlGroup;
1617
updateModel(dir: NgControl, value: any): void;
1718
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ControlValueAccessor} from './control_value_accessor';
2-
import {Control} from '../model';
2+
import {AbstractControlDirective} from './abstract_control_directive';
33

44
/**
55
* An abstract class that all control directive extend.
@@ -8,13 +8,12 @@ import {Control} from '../model';
88
*
99
* @exportedAs angular2/forms
1010
*/
11-
export class NgControl {
11+
export class NgControl extends AbstractControlDirective {
1212
name: string = null;
1313
valueAccessor: ControlValueAccessor = null;
1414

1515
get validator(): Function { return null; }
1616
get path(): List<string> { return null; }
17-
get control(): Control { return null; }
1817

1918
viewToModelUpdate(newValue: any): void {}
2019
}

modules/angular2/src/forms/directives/ng_control_group.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {CONST_EXPR} from 'angular2/src/facade/lang';
55

66
import {ControlContainer} from './control_container';
77
import {controlPath} from './shared';
8+
import {ControlGroup} from '../model';
9+
import {Form} from './form_interface';
810

911
const controlGroupBinding =
1012
CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgControlGroup)}));
@@ -66,7 +68,9 @@ export class NgControlGroup extends ControlContainer {
6668

6769
onDestroy() { this.formDirective.removeControlGroup(this); }
6870

71+
get control(): ControlGroup { return this.formDirective.getControlGroup(this); }
72+
6973
get path(): List<string> { return controlPath(this.name, this._parent); }
7074

71-
get formDirective(): any { return this._parent.formDirective; }
75+
get formDirective(): Form { return this._parent.formDirective; }
7276
}

modules/angular2/src/forms/directives/ng_form.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ export class NgForm extends ControlContainer implements Form {
6767

6868
get formDirective(): Form { return this; }
6969

70+
get control(): ControlGroup { return this.form; }
71+
7072
get path(): List<string> { return []; }
7173

7274
get controls(): StringMap<string, AbstractControl> { return this.form.controls; }
7375

74-
get value(): any { return this.form.value; }
75-
76-
get errors(): any { return this.form.errors; }
77-
7876
addControl(dir: NgControl): void {
7977
this._later(_ => {
8078
var container = this._findContainer(dir.path);
@@ -116,6 +114,10 @@ export class NgForm extends ControlContainer implements Form {
116114
});
117115
}
118116

117+
getControlGroup(dir: NgControlGroup): ControlGroup {
118+
return <ControlGroup>this.form.find(dir.path);
119+
}
120+
119121
updateModel(dir: NgControl, value: any): void {
120122
this._later(_ => {
121123
var c = <Control>this.form.find(dir.path);

modules/angular2/src/forms/directives/ng_form_model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export class NgFormModel extends ControlContainer implements Form {
102102

103103
get formDirective(): Form { return this; }
104104

105+
get control(): ControlGroup { return this.form; }
106+
105107
get path(): List<string> { return []; }
106108

107109
addControl(dir: NgControl): void {
@@ -119,6 +121,10 @@ export class NgFormModel extends ControlContainer implements Form {
119121

120122
removeControlGroup(dir: NgControlGroup) {}
121123

124+
getControlGroup(dir: NgControlGroup): ControlGroup {
125+
return <ControlGroup>this.form.find(dir.path);
126+
}
127+
122128
updateModel(dir: NgControl, value: any): void {
123129
var c  = <Control>this.form.find(dir.path);
124130
c.updateValue(value);

modules/angular2/test/forms/directives_spec.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ export function main() {
5656
loginControlDir.valueAccessor = new DummyControlValueAccessor();
5757
});
5858

59+
it("should reexport control properties", () => {
60+
expect(form.control).toBe(formModel);
61+
expect(form.value).toBe(formModel.value);
62+
expect(form.valid).toBe(formModel.valid);
63+
expect(form.errors).toBe(formModel.errors);
64+
expect(form.pristine).toBe(formModel.pristine);
65+
expect(form.dirty).toBe(formModel.dirty);
66+
expect(form.touched).toBe(formModel.touched);
67+
expect(form.untouched).toBe(formModel.untouched);
68+
});
69+
5970
describe("addControl", () => {
6071
it("should throw when no control found", () => {
6172
var dir = new NgControlName(form, null);
@@ -137,6 +148,17 @@ export function main() {
137148
loginControlDir.valueAccessor = new DummyControlValueAccessor();
138149
});
139150

151+
it("should reexport control properties", () => {
152+
expect(form.control).toBe(formModel);
153+
expect(form.value).toBe(formModel.value);
154+
expect(form.valid).toBe(formModel.valid);
155+
expect(form.errors).toBe(formModel.errors);
156+
expect(form.pristine).toBe(formModel.pristine);
157+
expect(form.dirty).toBe(formModel.dirty);
158+
expect(form.touched).toBe(formModel.touched);
159+
expect(form.untouched).toBe(formModel.untouched);
160+
});
161+
140162
describe("addControl & addControlGroup", () => {
141163
it("should create a control with the given name", fakeAsync(() => {
142164
form.addControlGroup(personControlGroupDir);
@@ -168,6 +190,31 @@ export function main() {
168190
});
169191
});
170192

193+
describe("NgControlGroup", () => {
194+
var formModel;
195+
var controlGroupDir;
196+
197+
beforeEach(() => {
198+
formModel = new ControlGroup({"login": new Control(null)});
199+
200+
var parent = new NgFormModel();
201+
parent.form = new ControlGroup({"group": formModel});
202+
controlGroupDir = new NgControlGroup(parent);
203+
controlGroupDir.name = "group";
204+
});
205+
206+
it("should reexport control properties", () => {
207+
expect(controlGroupDir.control).toBe(formModel);
208+
expect(controlGroupDir.value).toBe(formModel.value);
209+
expect(controlGroupDir.valid).toBe(formModel.valid);
210+
expect(controlGroupDir.errors).toBe(formModel.errors);
211+
expect(controlGroupDir.pristine).toBe(formModel.pristine);
212+
expect(controlGroupDir.dirty).toBe(formModel.dirty);
213+
expect(controlGroupDir.touched).toBe(formModel.touched);
214+
expect(controlGroupDir.untouched).toBe(formModel.untouched);
215+
});
216+
});
217+
171218
describe("NgFormControl", () => {
172219
var controlDir;
173220
var control;
@@ -180,6 +227,17 @@ export function main() {
180227
controlDir.form = control;
181228
});
182229

230+
it("should reexport control properties", () => {
231+
expect(controlDir.control).toBe(control);
232+
expect(controlDir.value).toBe(control.value);
233+
expect(controlDir.valid).toBe(control.valid);
234+
expect(controlDir.errors).toBe(control.errors);
235+
expect(controlDir.pristine).toBe(control.pristine);
236+
expect(controlDir.dirty).toBe(control.dirty);
237+
expect(controlDir.touched).toBe(control.touched);
238+
expect(controlDir.untouched).toBe(control.untouched);
239+
});
240+
183241
it("should set up validator", () => {
184242
controlDir.ngValidators.reset([new NgRequiredValidator()]);
185243

@@ -200,6 +258,18 @@ export function main() {
200258
ngModel.valueAccessor = new DummyControlValueAccessor();
201259
});
202260

261+
it("should reexport control properties", () => {
262+
var control = ngModel.control;
263+
expect(ngModel.control).toBe(control);
264+
expect(ngModel.value).toBe(control.value);
265+
expect(ngModel.valid).toBe(control.valid);
266+
expect(ngModel.errors).toBe(control.errors);
267+
expect(ngModel.pristine).toBe(control.pristine);
268+
expect(ngModel.dirty).toBe(control.dirty);
269+
expect(ngModel.touched).toBe(control.touched);
270+
expect(ngModel.untouched).toBe(control.untouched);
271+
});
272+
203273
it("should set up validator", () => {
204274
ngModel.ngValidators.reset([new NgRequiredValidator()]);
205275

@@ -211,5 +281,30 @@ export function main() {
211281
expect(ngModel.control.valid).toBe(false);
212282
});
213283
});
284+
285+
describe("NgControlName", () => {
286+
var formModel;
287+
var controlNameDir;
288+
289+
beforeEach(() => {
290+
formModel = new Control("name");
291+
292+
var parent = new NgFormModel();
293+
parent.form = new ControlGroup({"name": formModel});
294+
controlNameDir = new NgControlName(parent, new QueryList<any>());
295+
controlNameDir.name = "name";
296+
});
297+
298+
it("should reexport control properties", () => {
299+
expect(controlNameDir.control).toBe(formModel);
300+
expect(controlNameDir.value).toBe(formModel.value);
301+
expect(controlNameDir.valid).toBe(formModel.valid);
302+
expect(controlNameDir.errors).toBe(formModel.errors);
303+
expect(controlNameDir.pristine).toBe(formModel.pristine);
304+
expect(controlNameDir.dirty).toBe(formModel.dirty);
305+
expect(controlNameDir.touched).toBe(formModel.touched);
306+
expect(controlNameDir.untouched).toBe(formModel.untouched);
307+
});
308+
});
214309
});
215310
}

0 commit comments

Comments
 (0)