@rolster/forms 2.7.2 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/cjs/arguments.js +46 -46
  2. package/dist/cjs/arguments.js.map +1 -1
  3. package/dist/cjs/helpers.js +53 -53
  4. package/dist/cjs/helpers.js.map +1 -1
  5. package/dist/cjs/index.js +518 -510
  6. package/dist/cjs/index.js.map +1 -1
  7. package/dist/es/arguments.js +46 -46
  8. package/dist/es/arguments.js.map +1 -1
  9. package/dist/es/helpers.js +53 -53
  10. package/dist/es/helpers.js.map +1 -1
  11. package/dist/es/index.js +518 -510
  12. package/dist/es/index.js.map +1 -1
  13. package/dist/esm/arguments.d.ts +18 -18
  14. package/dist/esm/arguments.js +47 -47
  15. package/dist/esm/arguments.js.map +1 -1
  16. package/dist/esm/form-array/arguments.d.ts +6 -6
  17. package/dist/esm/form-array/arguments.js +1 -1
  18. package/dist/esm/form-array/form-array-control.d.ts +20 -20
  19. package/dist/esm/form-array/form-array-control.js +13 -13
  20. package/dist/esm/form-array/form-array-group.d.ts +13 -13
  21. package/dist/esm/form-array/form-array-group.js +14 -14
  22. package/dist/esm/form-array/form-array-list.d.ts +20 -20
  23. package/dist/esm/form-array/form-array-list.js +51 -51
  24. package/dist/esm/form-array/form-array.d.ts +55 -54
  25. package/dist/esm/form-array/form-array.js +149 -145
  26. package/dist/esm/form-array/form-array.js.map +1 -1
  27. package/dist/esm/form-array/index.d.ts +5 -5
  28. package/dist/esm/form-array/index.js +5 -5
  29. package/dist/esm/form-array/types.d.ts +2 -2
  30. package/dist/esm/form-array/types.js +1 -1
  31. package/dist/esm/form-control.d.ts +53 -52
  32. package/dist/esm/form-control.js +123 -119
  33. package/dist/esm/form-control.js.map +1 -1
  34. package/dist/esm/form-group.d.ts +34 -34
  35. package/dist/esm/form-group.js +93 -93
  36. package/dist/esm/form-group.js.map +1 -1
  37. package/dist/esm/helpers.d.ts +25 -25
  38. package/dist/esm/helpers.js +55 -55
  39. package/dist/esm/helpers.js.map +1 -1
  40. package/dist/esm/index.d.ts +4 -4
  41. package/dist/esm/index.js +4 -4
  42. package/dist/esm/types.d.ts +141 -139
  43. package/dist/esm/types.js +1 -1
  44. package/package.json +5 -5
  45. package/readme.md +2 -2
package/dist/es/index.js CHANGED
@@ -1,530 +1,538 @@
1
1
  import { v4 } from 'uuid';
2
2
  import { parseBoolean, observable } from '@rolster/commons';
3
3
 
4
- function itIsControlOptions(options) {
5
- return (typeof options === 'object' &&
6
- ('value' in options || 'validators' in options));
7
- }
8
- function itIsGroupOptions(options) {
9
- return typeof options === 'object' && 'controls' in options;
10
- }
11
- function itIsArrayOptions(options) {
12
- return (typeof options === 'object' &&
13
- ('groups' in options || 'validators' in options));
14
- }
15
- function createFormControlOptions(...controlOptions) {
16
- const [options, validators] = controlOptions;
17
- if (!options) {
18
- return { value: options, validators };
19
- }
20
- if (!validators && itIsControlOptions(options)) {
21
- return options;
22
- }
23
- return {
24
- value: options,
25
- validators
26
- };
27
- }
28
- function createFormGroupOptions(...groupOptions) {
29
- const [options, validators] = groupOptions;
30
- if (!validators && itIsGroupOptions(options)) {
31
- return options;
32
- }
33
- return {
34
- controls: options,
35
- validators
36
- };
37
- }
38
- function createFormArrayOptions(...arrayOptions) {
39
- const [options, validators] = arrayOptions;
40
- if (!options) {
41
- return { groups: options, validators };
42
- }
43
- if (!validators && itIsArrayOptions(options)) {
44
- return options;
45
- }
46
- return {
47
- groups: options,
48
- validators
49
- };
4
+ function itIsControlOptions(options) {
5
+ return (typeof options === 'object' &&
6
+ ('value' in options || 'validators' in options));
7
+ }
8
+ function itIsGroupOptions(options) {
9
+ return typeof options === 'object' && 'controls' in options;
10
+ }
11
+ function itIsArrayOptions(options) {
12
+ return (typeof options === 'object' &&
13
+ ('groups' in options || 'validators' in options));
14
+ }
15
+ function createFormControlOptions(...controlOptions) {
16
+ const [options, validators] = controlOptions;
17
+ if (!options) {
18
+ return { value: options, validators };
19
+ }
20
+ if (!validators && itIsControlOptions(options)) {
21
+ return options;
22
+ }
23
+ return {
24
+ value: options,
25
+ validators
26
+ };
27
+ }
28
+ function createFormGroupOptions(...groupOptions) {
29
+ const [options, validators] = groupOptions;
30
+ if (!validators && itIsGroupOptions(options)) {
31
+ return options;
32
+ }
33
+ return {
34
+ controls: options,
35
+ validators
36
+ };
37
+ }
38
+ function createFormArrayOptions(...arrayOptions) {
39
+ const [options, validators] = arrayOptions;
40
+ if (!options) {
41
+ return { groups: options, validators };
42
+ }
43
+ if (!validators && itIsArrayOptions(options)) {
44
+ return options;
45
+ }
46
+ return {
47
+ groups: options,
48
+ validators
49
+ };
50
50
  }
51
51
 
52
- function reduceErrors(errors) {
53
- return errors.reduce((keys, { id }) => [...keys, id], []);
54
- }
55
- const controlIsValid = ({ value, validators }) => {
56
- return validators.reduce((errors, validator) => {
57
- const error = validator(value);
58
- if (error) {
59
- errors.push(error);
60
- }
61
- return errors;
62
- }, []);
63
- };
64
- const controlsAllChecked = (controls, key) => {
65
- return Object.values(controls).reduce((value, control) => control.disabled ? value : value && parseBoolean(control[key]), true);
66
- };
67
- const controlsPartialChecked = (controls, key) => {
68
- return Object.values(controls).reduce((value, control) => control.disabled ? value : value || parseBoolean(control[key]), false);
69
- };
70
- const controlsToValue = (controls) => {
71
- return Object.entries(controls).reduce((result, [key, { value }]) => {
72
- result[key] = value;
73
- return result;
74
- }, {});
75
- };
76
- const groupIsValid = ({ controls, validators }) => {
77
- return validators.reduce((errors, validator) => {
78
- const error = validator(controls);
79
- if (error) {
80
- errors.push(error);
81
- }
82
- return errors;
83
- }, []);
84
- };
85
- function groupAllChecked(groups, key) {
86
- return groups.reduce((value, group) => value && parseBoolean(group[key]), true);
87
- }
88
- function groupPartialChecked(groups, key) {
89
- return groups.reduce((value, group) => value || parseBoolean(group[key]), false);
90
- }
91
- const arrayIsValid = ({ groups, validators }) => {
92
- return validators.reduce((errors, validator) => {
93
- const error = validator(groups);
94
- if (error) {
95
- errors.push(error);
96
- }
97
- return errors;
98
- }, []);
99
- };
100
- function hasError(errors, key) {
101
- return reduceErrors(errors).includes(key);
102
- }
103
- function someErrors(errors, keys) {
104
- return reduceErrors(errors).some((key) => keys.includes(key));
52
+ function reduceErrors(errors) {
53
+ return errors.reduce((keys, { id }) => [...keys, id], []);
54
+ }
55
+ const controlIsValid = ({ value, validators }) => {
56
+ return validators.reduce((errors, validator) => {
57
+ const error = validator(value);
58
+ if (error) {
59
+ errors.push(error);
60
+ }
61
+ return errors;
62
+ }, []);
63
+ };
64
+ const controlsAllChecked = (controls, key) => {
65
+ return Object.values(controls).reduce((value, control) => control.disabled ? value : value && parseBoolean(control[key]), true);
66
+ };
67
+ const controlsPartialChecked = (controls, key) => {
68
+ return Object.values(controls).reduce((value, control) => control.disabled ? value : value || parseBoolean(control[key]), false);
69
+ };
70
+ const controlsToValue = (controls) => {
71
+ return Object.entries(controls).reduce((result, [key, { value }]) => {
72
+ result[key] = value;
73
+ return result;
74
+ }, {});
75
+ };
76
+ const groupIsValid = ({ controls, validators }) => {
77
+ return validators.reduce((errors, validator) => {
78
+ const error = validator(controls);
79
+ if (error) {
80
+ errors.push(error);
81
+ }
82
+ return errors;
83
+ }, []);
84
+ };
85
+ function groupAllChecked(groups, key) {
86
+ return groups.reduce((value, group) => value && parseBoolean(group[key]), true);
87
+ }
88
+ function groupPartialChecked(groups, key) {
89
+ return groups.reduce((value, group) => value || parseBoolean(group[key]), false);
90
+ }
91
+ const arrayIsValid = ({ groups, validators }) => {
92
+ return validators.reduce((errors, validator) => {
93
+ const error = validator(groups);
94
+ if (error) {
95
+ errors.push(error);
96
+ }
97
+ return errors;
98
+ }, []);
99
+ };
100
+ function hasError(errors, key) {
101
+ return reduceErrors(errors).includes(key);
102
+ }
103
+ function someErrors(errors, keys) {
104
+ return reduceErrors(errors).some((key) => keys.includes(key));
105
105
  }
106
106
 
107
- class FormControl {
108
- constructor(controlOptions, controlValidators) {
109
- this.currentFocused = false;
110
- this.currentTouched = false;
111
- this.currentDirty = false;
112
- this.currentDisabled = false;
113
- this.currentValid = true;
114
- this.currentErrors = [];
115
- const { value, validators } = createFormControlOptions(controlOptions, controlValidators);
116
- this.observable = observable(value);
117
- this.initialValue = value;
118
- this.validators = validators;
119
- this.currentValue = value;
120
- this.updateValueAndValidity(value, validators);
121
- }
122
- get focused() {
123
- return this.currentFocused;
124
- }
125
- get unfocused() {
126
- return !this.currentFocused;
127
- }
128
- get touched() {
129
- return this.currentTouched;
130
- }
131
- get untouched() {
132
- return !this.currentTouched;
133
- }
134
- get dirty() {
135
- return this.currentDirty;
136
- }
137
- get pristine() {
138
- return !this.currentDirty;
139
- }
140
- get disabled() {
141
- return this.currentDisabled;
142
- }
143
- get enabled() {
144
- return !this.currentDisabled;
145
- }
146
- get valid() {
147
- return this.currentValid;
148
- }
149
- get invalid() {
150
- return !this.currentValid;
151
- }
152
- get value() {
153
- return this.currentValue;
154
- }
155
- get errors() {
156
- return this.currentErrors;
157
- }
158
- get error() {
159
- return this.currentError;
160
- }
161
- get wrong() {
162
- return this.touched && this.invalid;
163
- }
164
- hasError(key) {
165
- return hasError(this.errors, key);
166
- }
167
- someErrors(keys) {
168
- return someErrors(this.errors, keys);
169
- }
170
- reset() {
171
- this.setValue(this.initialValue);
172
- this.currentDirty = false;
173
- this.currentTouched = false;
174
- }
175
- focus() {
176
- this.currentFocused = true;
177
- }
178
- blur() {
179
- this.currentFocused = false;
180
- this.currentTouched = true;
181
- }
182
- disable() {
183
- this.currentDisabled = true;
184
- }
185
- enable() {
186
- this.currentDisabled = false;
187
- }
188
- touch() {
189
- this.currentTouched = true;
190
- }
191
- setValue(value) {
192
- if (this.enabled) {
193
- this.currentValue = value;
194
- this.currentDirty = true;
195
- this.updateValueAndValidity(value, this.validators);
196
- this.observable.next(value);
197
- }
198
- }
199
- setValidators(validators = []) {
200
- this.validators = validators;
201
- this.updateValueAndValidity(this.value, validators);
202
- }
203
- subscribe(subscriber) {
204
- return this.observable.subscribe(subscriber);
205
- }
206
- updateValueAndValidity(value, validators) {
207
- if (validators) {
208
- const errors = controlIsValid({ value, validators });
209
- this.currentError = errors[0];
210
- this.currentErrors = errors;
211
- this.currentValid = errors.length === 0;
212
- }
213
- else {
214
- this.currentValid = true;
215
- this.currentError = undefined;
216
- this.currentErrors = [];
217
- }
218
- }
219
- }
220
- function formControl(options, validators) {
221
- return new FormControl(createFormControlOptions(options, validators));
107
+ class FormControl {
108
+ constructor(controlOptions, controlValidators) {
109
+ this.currentFocused = false;
110
+ this.currentTouched = false;
111
+ this.currentDirty = false;
112
+ this.currentDisabled = false;
113
+ this.currentValid = true;
114
+ this.currentErrors = [];
115
+ const { value, validators } = createFormControlOptions(controlOptions, controlValidators);
116
+ this.observable = observable(value);
117
+ this.initialValue = value;
118
+ this.validators = validators;
119
+ this.currentValue = value;
120
+ this.updateValueAndValidity(value, validators);
121
+ }
122
+ get focused() {
123
+ return this.currentFocused;
124
+ }
125
+ get unfocused() {
126
+ return !this.currentFocused;
127
+ }
128
+ get touched() {
129
+ return this.currentTouched;
130
+ }
131
+ get untouched() {
132
+ return !this.currentTouched;
133
+ }
134
+ get dirty() {
135
+ return this.currentDirty;
136
+ }
137
+ get pristine() {
138
+ return !this.currentDirty;
139
+ }
140
+ get disabled() {
141
+ return this.currentDisabled;
142
+ }
143
+ get enabled() {
144
+ return !this.currentDisabled;
145
+ }
146
+ get valid() {
147
+ return this.currentValid;
148
+ }
149
+ get invalid() {
150
+ return !this.currentValid;
151
+ }
152
+ get value() {
153
+ return this.currentValue;
154
+ }
155
+ get errors() {
156
+ return this.currentErrors;
157
+ }
158
+ get error() {
159
+ return this.currentError;
160
+ }
161
+ get wrong() {
162
+ return this.touched && this.invalid;
163
+ }
164
+ hasError(key) {
165
+ return hasError(this.errors, key);
166
+ }
167
+ someErrors(keys) {
168
+ return someErrors(this.errors, keys);
169
+ }
170
+ reset() {
171
+ this.setValue(this.initialValue);
172
+ this.currentDirty = false;
173
+ this.currentTouched = false;
174
+ }
175
+ focus() {
176
+ this.currentFocused = true;
177
+ }
178
+ blur() {
179
+ this.currentFocused = false;
180
+ this.currentTouched = true;
181
+ }
182
+ disable() {
183
+ this.currentDisabled = true;
184
+ }
185
+ enable() {
186
+ this.currentDisabled = false;
187
+ }
188
+ touch() {
189
+ this.currentTouched = true;
190
+ }
191
+ setInitialValue(value) {
192
+ this.initialValue = value;
193
+ this.setValue(value);
194
+ }
195
+ setValue(value) {
196
+ if (this.enabled) {
197
+ this.currentValue = value;
198
+ this.currentDirty = true;
199
+ this.updateValueAndValidity(value, this.validators);
200
+ this.observable.next(value);
201
+ }
202
+ }
203
+ setValidators(validators = []) {
204
+ this.validators = validators;
205
+ this.updateValueAndValidity(this.value, validators);
206
+ }
207
+ subscribe(subscriber) {
208
+ return this.observable.subscribe(subscriber);
209
+ }
210
+ updateValueAndValidity(value, validators) {
211
+ if (validators) {
212
+ const errors = controlIsValid({ value, validators });
213
+ this.currentError = errors[0];
214
+ this.currentErrors = errors;
215
+ this.currentValid = errors.length === 0;
216
+ }
217
+ else {
218
+ this.currentValid = true;
219
+ this.currentError = undefined;
220
+ this.currentErrors = [];
221
+ }
222
+ }
223
+ }
224
+ function formControl(options, validators) {
225
+ return new FormControl(createFormControlOptions(options, validators));
222
226
  }
223
227
 
224
- class FormArrayControl extends FormControl {
225
- constructor(controlOptions, controlValidators) {
226
- const { value, validators } = createFormControlOptions(controlOptions, controlValidators);
227
- super(value, validators);
228
- this.uuid = v4();
229
- }
230
- }
231
- function formArrayControl(options, validators) {
232
- return new FormArrayControl(createFormControlOptions(options, validators));
228
+ class FormArrayControl extends FormControl {
229
+ constructor(controlOptions, controlValidators) {
230
+ const { value, validators } = createFormControlOptions(controlOptions, controlValidators);
231
+ super(value, validators);
232
+ this.uuid = v4();
233
+ }
234
+ }
235
+ function formArrayControl(options, validators) {
236
+ return new FormArrayControl(createFormControlOptions(options, validators));
233
237
  }
234
238
 
235
- class FormGroup {
236
- constructor(groupOptions, groupValidators) {
237
- this.currentErrors = [];
238
- this.currentValid = true;
239
- const { controls, validators } = createFormGroupOptions(groupOptions, groupValidators);
240
- this.currentControls = controls;
241
- this.validators = validators;
242
- this.updateValueAndValidity(controls, validators);
243
- this.observable = observable(this.value);
244
- Object.values(controls).forEach((control) => {
245
- control.subscribe(() => {
246
- this.updateValueAndValidity(this.controls, this.validators);
247
- this.observable.next(this.value);
248
- });
249
- });
250
- }
251
- get controls() {
252
- return this.currentControls;
253
- }
254
- get touched() {
255
- return controlsPartialChecked(this.controls, 'touched');
256
- }
257
- get touchedAll() {
258
- return controlsAllChecked(this.controls, 'touched');
259
- }
260
- get untouched() {
261
- return !this.touched;
262
- }
263
- get untouchedAll() {
264
- return !this.touchedAll;
265
- }
266
- get dirty() {
267
- return controlsPartialChecked(this.controls, 'dirty');
268
- }
269
- get dirtyAll() {
270
- return controlsAllChecked(this.controls, 'dirty');
271
- }
272
- get pristine() {
273
- return !this.dirty;
274
- }
275
- get pristineAll() {
276
- return this.dirtyAll;
277
- }
278
- get valid() {
279
- return this.currentValid && controlsAllChecked(this.controls, 'valid');
280
- }
281
- get invalid() {
282
- return !this.valid;
283
- }
284
- get value() {
285
- return controlsToValue(this.controls);
286
- }
287
- get errors() {
288
- return this.currentErrors;
289
- }
290
- get error() {
291
- return this.currentError;
292
- }
293
- get wrong() {
294
- return this.touched && this.invalid;
295
- }
296
- reset() {
297
- Object.values(this.controls).forEach((control) => {
298
- control.reset();
299
- });
300
- }
301
- setValidators(validators) {
302
- this.validators = validators;
303
- this.updateValueAndValidity(this.controls, validators);
304
- }
305
- subscribe(subscriber) {
306
- return this.observable.subscribe(subscriber);
307
- }
308
- updateValueAndValidity(controls, validators) {
309
- if (validators) {
310
- const errors = groupIsValid({ controls, validators });
311
- this.currentErrors = errors;
312
- this.currentError = errors[0];
313
- this.currentValid = errors.length === 0;
314
- }
315
- else {
316
- this.currentErrors = [];
317
- this.currentError = undefined;
318
- this.currentValid = true;
319
- }
320
- }
321
- }
322
- function formGroup(options, validators) {
323
- return new FormGroup(createFormGroupOptions(options, validators));
239
+ class FormGroup {
240
+ constructor(groupOptions, groupValidators) {
241
+ this.currentErrors = [];
242
+ this.currentValid = true;
243
+ const { controls, validators } = createFormGroupOptions(groupOptions, groupValidators);
244
+ this.currentControls = controls;
245
+ this.validators = validators;
246
+ this.updateValueAndValidity(controls, validators);
247
+ this.observable = observable(this.value);
248
+ Object.values(controls).forEach((control) => {
249
+ control.subscribe(() => {
250
+ this.updateValueAndValidity(this.controls, this.validators);
251
+ this.observable.next(this.value);
252
+ });
253
+ });
254
+ }
255
+ get controls() {
256
+ return this.currentControls;
257
+ }
258
+ get touched() {
259
+ return controlsPartialChecked(this.controls, 'touched');
260
+ }
261
+ get touchedAll() {
262
+ return controlsAllChecked(this.controls, 'touched');
263
+ }
264
+ get untouched() {
265
+ return !this.touched;
266
+ }
267
+ get untouchedAll() {
268
+ return !this.touchedAll;
269
+ }
270
+ get dirty() {
271
+ return controlsPartialChecked(this.controls, 'dirty');
272
+ }
273
+ get dirtyAll() {
274
+ return controlsAllChecked(this.controls, 'dirty');
275
+ }
276
+ get pristine() {
277
+ return !this.dirty;
278
+ }
279
+ get pristineAll() {
280
+ return this.dirtyAll;
281
+ }
282
+ get valid() {
283
+ return this.currentValid && controlsAllChecked(this.controls, 'valid');
284
+ }
285
+ get invalid() {
286
+ return !this.valid;
287
+ }
288
+ get value() {
289
+ return controlsToValue(this.controls);
290
+ }
291
+ get errors() {
292
+ return this.currentErrors;
293
+ }
294
+ get error() {
295
+ return this.currentError;
296
+ }
297
+ get wrong() {
298
+ return this.touched && this.invalid;
299
+ }
300
+ reset() {
301
+ Object.values(this.controls).forEach((control) => {
302
+ control.reset();
303
+ });
304
+ }
305
+ setValidators(validators) {
306
+ this.validators = validators;
307
+ this.updateValueAndValidity(this.controls, validators);
308
+ }
309
+ subscribe(subscriber) {
310
+ return this.observable.subscribe(subscriber);
311
+ }
312
+ updateValueAndValidity(controls, validators) {
313
+ if (validators) {
314
+ const errors = groupIsValid({ controls, validators });
315
+ this.currentErrors = errors;
316
+ this.currentError = errors[0];
317
+ this.currentValid = errors.length === 0;
318
+ }
319
+ else {
320
+ this.currentErrors = [];
321
+ this.currentError = undefined;
322
+ this.currentValid = true;
323
+ }
324
+ }
325
+ }
326
+ function formGroup(options, validators) {
327
+ return new FormGroup(createFormGroupOptions(options, validators));
324
328
  }
325
329
 
326
- class FormArrayGroup extends FormGroup {
327
- constructor(groupOptions, groupValidators) {
328
- const { controls, resource, validators } = createFormGroupOptions(groupOptions, groupValidators);
329
- super(controls, validators);
330
- this.uuid = v4();
331
- this.resource = resource;
332
- }
333
- }
334
- function formArrayGroup(options, validators) {
335
- return new FormArrayGroup(createFormGroupOptions(options, validators));
330
+ class FormArrayGroup extends FormGroup {
331
+ constructor(groupOptions, groupValidators) {
332
+ const { controls, resource, validators } = createFormGroupOptions(groupOptions, groupValidators);
333
+ super(controls, validators);
334
+ this.uuid = v4();
335
+ this.resource = resource;
336
+ }
337
+ }
338
+ function formArrayGroup(options, validators) {
339
+ return new FormArrayGroup(createFormGroupOptions(options, validators));
336
340
  }
337
341
 
338
- class FormArrayList extends FormControl {
339
- constructor(valueToControls, value, validators) {
340
- const initialValue = value || [];
341
- super(initialValue, validators);
342
- this.valueToControls = valueToControls;
343
- this.currentControls = initialValue.map((value) => this.createControls(value));
344
- this.uuid = v4();
345
- }
346
- get controls() {
347
- return this.currentControls;
348
- }
349
- get touched() {
350
- return this.controls.reduce((valid, controls) => valid && controlsPartialChecked(controls, 'touched'), true);
351
- }
352
- get dirty() {
353
- return this.controls.reduce((valid, controls) => valid && controlsPartialChecked(controls, 'dirty'), true);
354
- }
355
- get valid() {
356
- return (this.currentValid &&
357
- this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true));
358
- }
359
- get value() {
360
- return this.controls.map((controls) => controlsToValue(controls));
361
- }
362
- setValue(values) {
363
- this.currentControls = values.map((value) => this.createControls(value));
364
- }
365
- push(controls) {
366
- this.currentControls = this.currentControls.concat([controls]);
367
- }
368
- remove(controls) {
369
- this.currentControls = this.currentControls.filter((currentControls) => currentControls !== controls);
370
- }
371
- createControls(value) {
372
- const controls = this.valueToControls(value);
373
- Object.values(controls).forEach((control) => {
374
- control.subscribe(() => {
375
- const { value, validators } = this;
376
- this.updateValueAndValidity(value, validators);
377
- this.observable.next(value);
378
- });
379
- });
380
- return controls;
381
- }
382
- }
383
- function formArrayList(valueToControl, value, validators) {
384
- return new FormArrayList(valueToControl, value, validators);
342
+ class FormArrayList extends FormControl {
343
+ constructor(valueToControls, value, validators) {
344
+ const initialValue = value || [];
345
+ super(initialValue, validators);
346
+ this.valueToControls = valueToControls;
347
+ this.currentControls = initialValue.map((value) => this.createControls(value));
348
+ this.uuid = v4();
349
+ }
350
+ get controls() {
351
+ return this.currentControls;
352
+ }
353
+ get touched() {
354
+ return this.controls.reduce((valid, controls) => valid && controlsPartialChecked(controls, 'touched'), true);
355
+ }
356
+ get dirty() {
357
+ return this.controls.reduce((valid, controls) => valid && controlsPartialChecked(controls, 'dirty'), true);
358
+ }
359
+ get valid() {
360
+ return (this.currentValid &&
361
+ this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true));
362
+ }
363
+ get value() {
364
+ return this.controls.map((controls) => controlsToValue(controls));
365
+ }
366
+ setValue(values) {
367
+ this.currentControls = values.map((value) => this.createControls(value));
368
+ }
369
+ push(controls) {
370
+ this.currentControls = this.currentControls.concat([controls]);
371
+ }
372
+ remove(controls) {
373
+ this.currentControls = this.currentControls.filter((currentControls) => currentControls !== controls);
374
+ }
375
+ createControls(value) {
376
+ const controls = this.valueToControls(value);
377
+ Object.values(controls).forEach((control) => {
378
+ control.subscribe(() => {
379
+ const { value, validators } = this;
380
+ this.updateValueAndValidity(value, validators);
381
+ this.observable.next(value);
382
+ });
383
+ });
384
+ return controls;
385
+ }
386
+ }
387
+ function formArrayList(valueToControl, value, validators) {
388
+ return new FormArrayList(valueToControl, value, validators);
385
389
  }
386
390
 
387
- class FormArray {
388
- constructor(arrayOptions, arrayValidators) {
389
- this.currentGroups = [];
390
- this.currentValid = true;
391
- this.currentDisabled = false;
392
- this.currentErrors = [];
393
- const { groups, validators } = createFormArrayOptions(arrayOptions, arrayValidators);
394
- this.unsusbcriptions = new Map();
395
- this.initialState = groups;
396
- this.validators = validators;
397
- this.refresh(this.initialState);
398
- this.observable = observable(this.value);
399
- groups?.forEach((group) => {
400
- this.subscription(group);
401
- });
402
- }
403
- get groups() {
404
- return this.currentGroups;
405
- }
406
- get controls() {
407
- return this.groups.map(({ controls }) => controls);
408
- }
409
- get touched() {
410
- return groupPartialChecked(this.groups, 'touched');
411
- }
412
- get touchedAll() {
413
- return groupAllChecked(this.groups, 'touchedAll');
414
- }
415
- get untouched() {
416
- return !this.touched;
417
- }
418
- get untouchedAll() {
419
- return !this.touchedAll;
420
- }
421
- get dirty() {
422
- return groupPartialChecked(this.groups, 'dirty');
423
- }
424
- get dirtyAll() {
425
- return groupAllChecked(this.groups, 'dirtyAll');
426
- }
427
- get pristine() {
428
- return !this.dirty;
429
- }
430
- get pristineAll() {
431
- return !this.dirtyAll;
432
- }
433
- get disabled() {
434
- return this.currentDisabled;
435
- }
436
- get enabled() {
437
- return !this.currentDisabled;
438
- }
439
- get valid() {
440
- return this.currentValid && groupAllChecked(this.groups, 'valid');
441
- }
442
- get invalid() {
443
- return !this.currentValid;
444
- }
445
- get value() {
446
- return this.groups.map(({ value: state }) => state);
447
- }
448
- get error() {
449
- return this.currentError;
450
- }
451
- get errors() {
452
- return this.currentErrors;
453
- }
454
- get wrong() {
455
- return this.touched && this.invalid;
456
- }
457
- hasError(key) {
458
- return hasError(this.errors, key);
459
- }
460
- someErrors(keys) {
461
- return someErrors(this.errors, keys);
462
- }
463
- reset() {
464
- this.refresh(this.initialState);
465
- }
466
- disable() {
467
- this.currentDisabled = true;
468
- }
469
- enable() {
470
- this.currentDisabled = false;
471
- }
472
- push(group) {
473
- this.subscription(group);
474
- this.refresh([...this.groups, group]);
475
- }
476
- merge(groups) {
477
- groups.forEach((group) => {
478
- this.subscription(group);
479
- });
480
- this.refresh([...this.groups, ...groups]);
481
- }
482
- set(groups) {
483
- this.currentGroups.forEach(({ uuid }) => {
484
- this.unsusbcriptions.delete(uuid);
485
- });
486
- groups.forEach((group) => {
487
- this.subscription(group);
488
- });
489
- this.refresh(groups); // Update groups
490
- }
491
- remove({ uuid }) {
492
- this.refresh(this.groups.filter((group) => group.uuid !== uuid));
493
- }
494
- setValidators(validators) {
495
- this.validators = validators;
496
- this.updateValidityStatus(this.groups, validators);
497
- }
498
- subscribe(subscriber) {
499
- return this.observable.subscribe(subscriber);
500
- }
501
- subscription(group) {
502
- const unsusbcription = group.subscribe(() => {
503
- this.updateValidityStatus(this.groups, this.validators);
504
- });
505
- this.unsusbcriptions.set(group.uuid, unsusbcription);
506
- }
507
- updateValidityStatus(groups, validators) {
508
- if (validators) {
509
- const errors = arrayIsValid({ groups, validators });
510
- this.currentErrors = errors;
511
- this.currentError = errors[0];
512
- this.currentValid = errors.length === 0;
513
- }
514
- else {
515
- this.currentValid = true;
516
- this.currentErrors = [];
517
- this.currentError = undefined;
518
- }
519
- }
520
- refresh(newGroups) {
521
- const groups = newGroups || [];
522
- this.currentGroups = groups;
523
- this.updateValidityStatus(groups, this.validators);
524
- }
525
- }
526
- function formArray(options, validators) {
527
- return new FormArray(createFormArrayOptions(options, validators));
391
+ class FormArray {
392
+ constructor(arrayOptions, arrayValidators) {
393
+ this.currentGroups = [];
394
+ this.currentValid = true;
395
+ this.currentDisabled = false;
396
+ this.currentErrors = [];
397
+ const { groups, validators } = createFormArrayOptions(arrayOptions, arrayValidators);
398
+ this.unsusbcriptions = new Map();
399
+ this.initialValue = groups;
400
+ this.validators = validators;
401
+ this.refresh(this.initialValue);
402
+ this.observable = observable(this.value);
403
+ groups?.forEach((group) => {
404
+ this.subscription(group);
405
+ });
406
+ }
407
+ get groups() {
408
+ return this.currentGroups;
409
+ }
410
+ get controls() {
411
+ return this.groups.map(({ controls }) => controls);
412
+ }
413
+ get touched() {
414
+ return groupPartialChecked(this.groups, 'touched');
415
+ }
416
+ get touchedAll() {
417
+ return groupAllChecked(this.groups, 'touchedAll');
418
+ }
419
+ get untouched() {
420
+ return !this.touched;
421
+ }
422
+ get untouchedAll() {
423
+ return !this.touchedAll;
424
+ }
425
+ get dirty() {
426
+ return groupPartialChecked(this.groups, 'dirty');
427
+ }
428
+ get dirtyAll() {
429
+ return groupAllChecked(this.groups, 'dirtyAll');
430
+ }
431
+ get pristine() {
432
+ return !this.dirty;
433
+ }
434
+ get pristineAll() {
435
+ return !this.dirtyAll;
436
+ }
437
+ get disabled() {
438
+ return this.currentDisabled;
439
+ }
440
+ get enabled() {
441
+ return !this.currentDisabled;
442
+ }
443
+ get valid() {
444
+ return this.currentValid && groupAllChecked(this.groups, 'valid');
445
+ }
446
+ get invalid() {
447
+ return !this.currentValid;
448
+ }
449
+ get value() {
450
+ return this.groups.map(({ value: state }) => state);
451
+ }
452
+ get error() {
453
+ return this.currentError;
454
+ }
455
+ get errors() {
456
+ return this.currentErrors;
457
+ }
458
+ get wrong() {
459
+ return this.touched && this.invalid;
460
+ }
461
+ hasError(key) {
462
+ return hasError(this.errors, key);
463
+ }
464
+ someErrors(keys) {
465
+ return someErrors(this.errors, keys);
466
+ }
467
+ reset() {
468
+ this.refresh(this.initialValue);
469
+ }
470
+ disable() {
471
+ this.currentDisabled = true;
472
+ }
473
+ enable() {
474
+ this.currentDisabled = false;
475
+ }
476
+ push(group) {
477
+ this.subscription(group);
478
+ this.refresh([...this.groups, group]);
479
+ }
480
+ merge(groups) {
481
+ groups.forEach((group) => {
482
+ this.subscription(group);
483
+ });
484
+ this.refresh([...this.groups, ...groups]);
485
+ }
486
+ setInitialValue(groups) {
487
+ this.initialValue = groups;
488
+ this.setValue(groups);
489
+ }
490
+ setValue(groups) {
491
+ this.currentGroups.forEach(({ uuid }) => {
492
+ this.unsusbcriptions.delete(uuid);
493
+ });
494
+ groups.forEach((group) => {
495
+ this.subscription(group);
496
+ });
497
+ this.refresh(groups); // Update groups
498
+ }
499
+ remove({ uuid }) {
500
+ this.refresh(this.groups.filter((group) => group.uuid !== uuid));
501
+ }
502
+ setValidators(validators) {
503
+ this.validators = validators;
504
+ this.updateValidityStatus(this.groups, validators);
505
+ }
506
+ subscribe(subscriber) {
507
+ return this.observable.subscribe(subscriber);
508
+ }
509
+ subscription(group) {
510
+ const unsusbcription = group.subscribe(() => {
511
+ this.updateValidityStatus(this.groups, this.validators);
512
+ });
513
+ this.unsusbcriptions.set(group.uuid, unsusbcription);
514
+ }
515
+ updateValidityStatus(groups, validators) {
516
+ if (validators) {
517
+ const errors = arrayIsValid({ groups, validators });
518
+ this.currentErrors = errors;
519
+ this.currentError = errors[0];
520
+ this.currentValid = errors.length === 0;
521
+ }
522
+ else {
523
+ this.currentValid = true;
524
+ this.currentErrors = [];
525
+ this.currentError = undefined;
526
+ }
527
+ }
528
+ refresh(newGroups) {
529
+ const groups = newGroups || [];
530
+ this.currentGroups = groups;
531
+ this.updateValidityStatus(groups, this.validators);
532
+ }
533
+ }
534
+ function formArray(options, validators) {
535
+ return new FormArray(createFormArrayOptions(options, validators));
528
536
  }
529
537
 
530
538
  export { FormArray, FormArrayControl, FormArrayGroup, FormArrayList, FormControl, FormGroup, formArray, formArrayControl, formArrayGroup, formArrayList, formControl, formGroup };