4
4
<img width =" 50% " height =" 50% " src =" ./logo.png " >
5
5
</p >
6
6
7
+ > The Foundation for Proper Form Management
8
+
9
+ ## 🔮 Features
10
+
11
+ ✅ Auto persists the form's state upon user navigation.<br >
12
+ ✅ Provides an API to reactively querying any form, from anywhere. <br >
13
+ ✅ Persist the form's state to local storage.
14
+
15
+ <br >
16
+
7
17
[ ![ Build Status] ( https://img.shields.io/travis/datorama/akita.svg?style=flat-square )] ( https://travis-ci.org/ngneat/transloco )
8
18
[ ![ commitizen] ( https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square )] ( )
9
19
[ ![ PRs] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square )] ( )
@@ -63,52 +73,138 @@ export class OnboardingComponent {
63
73
}
64
74
65
75
ngOnDestroy() {
66
- this .formsManager .unsubscribe ();
76
+ this .formsManager .unsubscribe (' onboarding ' );
67
77
}
68
78
}
69
79
```
70
80
71
81
As you can see, we’re still working with the existing API in order to create a form in Angular. We’re injecting the ` NgFormsManager ` and calling the ` upsert ` method, giving it the form name and an ` AbstractForm ` .
72
- From that point on, ` NgFormsManager ` will track the form value changes and update the store accordingly.
82
+ From that point on, ` NgFormsManager ` will track the ` form ` value changes, and update the store accordingly.
73
83
74
84
With this setup, you’ll have an extensive API to query the store and update the form from anywhere in your application:
75
85
86
+ ## API
87
+
88
+ - ` selectValid() ` - Whether the control is valid
89
+
76
90
``` ts
91
+ const isFormValid$ = formsManager .selectValid (' onboarding' );
92
+ const isNameValid$ = formsManager .selectValid (' onboarding' , ' name' );
93
+ ```
77
94
78
- @Component ({
79
- selector: ' my-comp' ,
80
- templateUrl: ' ./app.component.html' ,
81
- })
82
- export class MyCompComponent {
83
- constructor (private formsManager : NgFormsManager ) {}
95
+ - ` selectDirty() ` - Whether the control is dirty
84
96
85
- ngOnInit() {
86
- this . formsManager .selectForm ( formName ). subscribe ( form => {} );
87
- this . formsManager .getForm ( formName );
88
- this . formsManager . hasForm ( formName );
97
+ ``` ts
98
+ const isFormDirty$ = formsManager .selectDirty ( ' onboarding ' );
99
+ const isNameDirty$ = formsManager .selectDirty ( ' onboarding ' , ' name ' );
100
+ ```
89
101
90
- this .formsManager .selectControl (formName , ' email' ).subscribe (emailControl => {});
91
- this .formsManager .getControl (formName , path ? );
102
+ - ` selectDisabled() ` - Whether the control is disabled
92
103
93
- this .formsManager .selectErrors (formName , path ? ).subscribe (errors => {});
94
- this .formsManager .selectValue (formName , path ? ).subscribe (value => {});
95
- this .formsManager .selectDisabled (formName , path ? ).subscribe (disabled => {});
96
- this .formsManager .selectDirty (formName , path ? ).subscribe (dirty => {});
97
- this .formsManager .selectValid (formName , path ? ).subscribe (valid => {});
104
+ ``` ts
105
+ const isFormDisabled$ = formsManager .selectDisabled (' onboarding' );
106
+ const isNameDisabled$ = formsManager .selectDisabled (' onboarding' , ' name' );
107
+ ```
98
108
99
- this .formsManager .patchValue (formName , value , options );
100
- this .formsManager .setValue (formName , value , options );
101
- }
109
+ - ` selectValue() ` - Observe the control's value
102
110
103
- ngOnDestroy () {
104
- formsManager.unsubscribe(formName?);
105
- }
111
+ ``` ts
112
+ const value$ = formsManager .selectValue (' onboarding' );
113
+ const nameValue$ = formsManager .selectValue (' onboarding' , ' name' );
114
+ ```
115
+
116
+ - ` selectErrors() ` - Observe the control's errors
117
+
118
+ ``` ts
119
+ const errors$ = formsManager .selectErrors (' onboarding' );
120
+ const nameErros$ = formsManager .selectErrors (' onboarding' , ' name' );
121
+ ```
122
+
123
+ - ` selectControl() ` - Observe the control state
124
+
125
+ ``` ts
126
+ const control$ = formsManager .selectControl (' onboarding' );
127
+ const nameControl$ = formsManager .selectControl (' onboarding' , ' name' );
128
+ ```
129
+
130
+ - ` getControl() ` - Get the control state
131
+
132
+ ``` ts
133
+ const control = formsManager .getControl (' onboarding' );
134
+ const nameControl = formsManager .getControl (' onboarding' , ' name' );
135
+ ```
136
+
137
+ It returns the following state:
138
+
139
+ ``` ts
140
+ {
141
+ value : any ,
142
+ rawValue : object ,
143
+ errors : object ,
144
+ valid : boolean ,
145
+ dirty : boolean ,
146
+ invalid : boolean ,
147
+ disabled : boolean ,
148
+ touched : boolean ,
149
+ pristine : boolean ,
150
+ pending : boolean ,
106
151
}
107
152
```
108
153
154
+ - ` selectForm() ` - Observe the form state
155
+
156
+ ``` ts
157
+ const form$ = formsManager .selectForm (' onboarding' );
158
+ ```
159
+
160
+ - ` getForm() ` - Get the form state
161
+
162
+ ``` ts
163
+ const form = formsManager .getForm (' onboarding' );
164
+ ```
165
+
166
+ - ` hasForm() ` - Whether the form exists
167
+
168
+ ``` ts
169
+ const hasForm = formsManager .hasForm (' onboarding' );
170
+ ```
171
+
172
+ - ` patchValue() ` - A facade to the original ` patchValue ` method
173
+
174
+ ``` ts
175
+ formsManager .patchValue (' onboarding' , value , options );
176
+ ```
177
+
178
+ - ` setValue() ` - A facade to the original ` setValue ` method
179
+
180
+ ``` ts
181
+ formsManager .setValue (' onboarding' , value , options );
182
+ ```
183
+
184
+ - ` unsubscribe() ` - Unsubscribe from the form's ` valueChanges ` observable (always call it on ` ngOnDestroy ` )
185
+
186
+ ``` ts
187
+ formsManager .unsubscribe (' onboarding' );
188
+ formsManager .unsubscribe ();
189
+ ```
190
+
191
+ - ` clear() ` - Deletes the form from the store
192
+
193
+ ``` ts
194
+ formsManager .clear (' onboarding' );
195
+ formsManager .clear ();
196
+ ```
197
+
198
+ - ` destroy() ` - Destroy the form (Internally calls ` clear ` and ` unsubscribe ` )
199
+
200
+ ``` ts
201
+ formsManager .destroy (' onboarding' );
202
+ formsManager .destroy ();
203
+ ```
204
+
109
205
## Persist to Local Storage
110
206
111
- When passing the form to the ` upsert ` method, pass the ` persistState ` flag:
207
+ In the ` upsert ` method, pass the ` persistState ` flag:
112
208
113
209
``` ts
114
210
formsManager .upsert (formName , abstractContorl , {
@@ -142,8 +238,8 @@ export class HomeComponent{
142
238
});
143
239
144
240
/*
145
- * Check the `minPrice` value in the settings form
146
- * and update the price's control validators
241
+ * Observe the `minPrice` value in the ` settings` form
242
+ * and update the price ` control` validators
147
243
*/
148
244
this .formsManager .selectValue <number >(' settings' , ' minPrice' )
149
245
.subscribe (minPrice => setValidators (this .form .get (' price' ), Validators .min (minPrice ));
@@ -153,7 +249,7 @@ export class HomeComponent{
153
249
154
250
## Using FormArray Controls
155
251
156
- When working with a ` FormArray ` , it's required to pass a factory function that tells how to create the controls inside the array . For example:
252
+ When working with a ` FormArray ` , it's required to pass a ` factory ` function that instruct how to create the ` controls ` inside the ` FormArray ` . For example:
157
253
158
254
` ` ` ts
159
255
import { NgFormsManager } from ' @ngneat/forms-manager' ;
@@ -165,19 +261,17 @@ export class HomeComponent {
165
261
constructor (private formsManager : NgFormsManager <FormsState >) {}
166
262
167
263
ngOnInit() {
168
- const createControl = value => new FormControl (value );
169
-
170
- this .skills = new FormArray ([createControl (' JS' )]);
264
+ this .skills = new FormArray ([]);
171
265
172
- /** Or inside form group */
266
+ /** Or inside a FormGroup */
173
267
this .config = new FormGroup ({
174
- skills: new FormArray ([createControl ( ' JS ' ) ]),
268
+ skills: new FormArray ([]),
175
269
});
176
270
177
271
this .formsManager
178
- .upsert (' skills' , this .skills , { arrControlFactory: createControl })
272
+ .upsert (' skills' , this .skills , { arrControlFactory : value => new FormControl ( value ) })
179
273
.upsert (' config' , this .config , {
180
- arrControlFactory: { skills: createControl },
274
+ arrControlFactory: { skills : value => new FormControl ( value ) },
181
275
});
182
276
}
183
277
0 commit comments