Skip to content

Commit 32221c8

Browse files
authored
Merge pull request #138 from jogelin/issue-137
2 parents a6a0c22 + 849500f commit 32221c8

File tree

8 files changed

+78
-17
lines changed

8 files changed

+78
-17
lines changed
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
<form [formGroup]="group">
2-
{{ group.value | json }}
2+
<pre>{{ group.value | json }}</pre>
33

44
<input type="text" [formControl]="group.controls.foo" />
55
</form>
66

77

88
<form [formGroup]="build">
9-
{{ build.value | json }}
9+
<pre>{{ build.value | json }}</pre>
1010

11+
<label>name:</label>
1112
<input type="text" [formControl]="build.controls.name" />
1213

1314
<div formGroupName="address">
14-
<input type="text" formControlName="street">
15+
<label>Street:</label>
16+
<input type="text" formControlName="street" />
1517
</div>
16-
</form>
18+
19+
<ng-container *ngFor="let _ of build.controls.profiles.controls; index as i">
20+
<label>Profile name:</label>
21+
<pre>{{ build.controls.profiles.controls[i].value | json }}</pre>
22+
</ng-container>
23+
</form>

apps/playground/src/app/app.component.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Validators } from '@angular/forms';
3-
import { FormGroup, FormControl, ControlsOf, FormArray, FormBuilder } from '@ngenat/reactive-forms';
3+
import {
4+
FormGroup,
5+
FormControl,
6+
ControlsOf,
7+
FormArray,
8+
FormBuilder,
9+
ValuesOf,
10+
} from '@ngneat/reactive-forms';
11+
import { User, UserForm } from './user.interfaces';
412

513
interface Props {
614
foo: string;
@@ -36,16 +44,36 @@ export class AppComponent implements OnInit {
3644
arr: new FormArray([]),
3745
});
3846

39-
build = this.builder.group({
47+
build = this.builder.group<UserForm>({
4048
name: '',
4149
address: this.builder.group({
42-
street: ['', Validators.required]
43-
})
44-
})
50+
street: ['', Validators.required],
51+
}),
52+
profiles: this.builder.array([]),
53+
});
4554

46-
constructor(private builder: FormBuilder) { }
55+
constructor(private builder: FormBuilder) {}
4756

4857
ngOnInit() {
58+
const user: User = {
59+
name: 'My User',
60+
address: {
61+
street: 'street one',
62+
},
63+
profiles: [
64+
{ name: 'Profile One', permissions: ['Can', 'View'] },
65+
{ name: 'Profile Two', permissions: ['Cannot', 'Edit'] },
66+
],
67+
};
68+
69+
// This is to check that ValuesOf takes in account the Control type of a FormArray
70+
const valueOfUser: ValuesOf<UserForm> = user;
71+
72+
valueOfUser.profiles.forEach((profile) =>
73+
this.build.controls.profiles.push(new FormControl(profile))
74+
);
75+
this.build.setValue(valueOfUser);
76+
4977
console.log(1);
5078
}
5179
}

apps/playground/src/app/demo/demo.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22

33
import { Component, OnInit } from '@angular/core';
4-
import { FormControl, FormGroup } from '@ngenat/reactive-forms';
4+
import { FormControl, FormGroup } from '@ngneat/reactive-forms';
55

66
@Component({
77
selector: 'ngneat-demo',
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { FormArray, FormControl, FormGroup } from '@ngneat/reactive-forms';
2+
3+
export interface UserAddress {
4+
street: string;
5+
}
6+
7+
export interface UserProfiles {
8+
name: string;
9+
permissions: string[];
10+
}
11+
12+
export interface User {
13+
name: string;
14+
address: UserAddress;
15+
profiles: UserProfiles[];
16+
}
17+
18+
export interface UserAddressForm {
19+
street: FormControl<string>;
20+
}
21+
22+
export interface UserForm {
23+
name: string;
24+
address: FormGroup<UserAddressForm>;
25+
profiles: FormArray<UserProfiles, FormControl<UserProfiles>>;
26+
}

libs/reactive-forms/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export { FormGroup } from './lib/form-group';
22
export { FormControl } from './lib/form-control';
33
export { FormArray } from './lib/form-array';
44
export { FormBuilder } from './lib/form-builder';
5-
export { ControlsOf } from './lib/types';
5+
export { ControlsOf, ValuesOf } from './lib/types';
66
export { diff } from './lib/operators/diff';
77
export { LocalStorageManager, persistControl, restoreControl, PersistManager, SessionStorageManager } from './lib/persist/persist';
8-
export { ControlValueAccessor } from './lib/control-value.accessor';
8+
export { ControlValueAccessor } from './lib/control-value.accessor';

libs/reactive-forms/src/lib/form-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class FormBuilder extends NgFormBuilder {
2121
asyncValidator?: ConstructorParameters<typeof FormArray>[2]) {
2222
const controls = controlsConfig.map(c => (this as any)._createControl(c));
2323

24-
return new FormArray<T>(controls, validatorOrOpts, asyncValidator);
24+
return new FormArray<T,Control>(controls, validatorOrOpts, asyncValidator);
2525
}
2626

2727

libs/reactive-forms/src/lib/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AbstractControl } from '@angular/forms';
55

66
type NonUndefined<T> = T extends undefined ? never : T;
77

8-
// This type is **Experimental**
8+
// This type is **Experimental**
99
export type ControlsOf<T extends Record<string, any>> = {
1010
[K in keyof T]: NonUndefined<T[K]> extends AbstractControl ? T[K] : NonUndefined<T[K]> extends (infer R)[]
1111
? FormArray<R>
@@ -19,7 +19,7 @@ export type ValuesOf<T extends ControlsOf<any>> = {
1919
? R
2020
: NonUndefined<T[K]> extends FormGroup<infer R>
2121
? ValuesOf<R>
22-
: NonUndefined<T[K]> extends FormArray<infer R> ? R[] : NonUndefined<T[K]>;
22+
: NonUndefined<T[K]> extends FormArray<infer R, infer C> ? R[] : NonUndefined<T[K]>;
2323
};
2424

2525
export type DeepPartial<T> = {

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"skipDefaultLibCheck": true,
1717
"baseUrl": ".",
1818
"paths": {
19-
"@ngenat/reactive-forms": ["libs/reactive-forms/src/index.ts"]
19+
"@ngneat/reactive-forms": ["libs/reactive-forms/src/index.ts"]
2020
}
2121
},
2222
"exclude": ["node_modules", "tmp"]

0 commit comments

Comments
 (0)