@rolster/react-forms 19.3.0 → 19.4.1

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 (68) hide show
  1. package/dist/cjs/{index.js → index.cjs} +52 -10
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/form-array/form-array-control.d.ts +1 -1
  4. package/dist/esm/form-array/form-array-control.js +1 -1
  5. package/dist/esm/form-array/form-array-control.js.map +1 -1
  6. package/dist/esm/form-array/form-array-control.type.d.ts +22 -0
  7. package/dist/esm/form-array/form-array-control.type.js +2 -0
  8. package/dist/esm/form-array/form-array-control.type.js.map +1 -0
  9. package/dist/esm/form-array/form-array-group.d.ts +2 -1
  10. package/dist/esm/form-array/form-array-group.helper.d.ts +3 -0
  11. package/dist/esm/{utilities.js → form-array/form-array-group.helper.js} +1 -1
  12. package/dist/esm/form-array/form-array-group.helper.js.map +1 -0
  13. package/dist/esm/form-array/form-array-group.js +1 -1
  14. package/dist/esm/form-array/form-array-group.js.map +1 -1
  15. package/dist/esm/form-array/form-array-group.type.d.ts +8 -0
  16. package/dist/esm/form-array/form-array-group.type.js +2 -0
  17. package/dist/esm/form-array/form-array-group.type.js.map +1 -0
  18. package/dist/esm/form-array/form-array-list.d.ts +2 -1
  19. package/dist/esm/form-array/form-array-list.js +1 -1
  20. package/dist/esm/form-array/form-array-list.js.map +1 -1
  21. package/dist/esm/form-array/form-array-list.type.d.ts +9 -0
  22. package/dist/esm/form-array/form-array-list.type.js +2 -0
  23. package/dist/esm/form-array/form-array-list.type.js.map +1 -0
  24. package/dist/esm/form-array/form-array.d.ts +2 -1
  25. package/dist/esm/form-array/form-array.js +7 -7
  26. package/dist/esm/form-array/form-array.js.map +1 -1
  27. package/dist/esm/form-array/form-array.type.d.ts +6 -0
  28. package/dist/esm/form-array/form-array.type.js +2 -0
  29. package/dist/esm/form-array/form-array.type.js.map +1 -0
  30. package/dist/esm/{form-control.d.ts → form-control/form-control.d.ts} +1 -1
  31. package/dist/esm/form-control/form-control.helper.d.ts +2 -0
  32. package/dist/esm/form-control/form-control.helper.js +5 -0
  33. package/dist/esm/form-control/form-control.helper.js.map +1 -0
  34. package/dist/esm/{form-control.js → form-control/form-control.js} +8 -10
  35. package/dist/esm/form-control/form-control.js.map +1 -0
  36. package/dist/esm/form-control/form-control.type.d.ts +13 -0
  37. package/dist/esm/form-control/form-control.type.js +2 -0
  38. package/dist/esm/form-control/form-control.type.js.map +1 -0
  39. package/dist/esm/{form-group.d.ts → form-group/form-group.d.ts} +1 -1
  40. package/dist/esm/{form-group.js → form-group/form-group.js} +7 -0
  41. package/dist/esm/form-group/form-group.js.map +1 -0
  42. package/dist/esm/form-group/form-group.type.d.ts +4 -0
  43. package/dist/esm/form-group/form-group.type.js +2 -0
  44. package/dist/esm/form-group/form-group.type.js.map +1 -0
  45. package/dist/esm/{form-ref.d.ts → form-ref/form-ref.d.ts} +1 -1
  46. package/dist/esm/{form-ref.js → form-ref/form-ref.js} +1 -1
  47. package/dist/esm/form-ref/form-ref.js.map +1 -0
  48. package/dist/esm/helpers.d.ts +4 -4
  49. package/dist/esm/hooks.d.ts +2 -1
  50. package/dist/esm/hooks.js.map +1 -1
  51. package/dist/esm/index.d.ts +9 -3
  52. package/dist/esm/index.js +9 -3
  53. package/dist/esm/index.js.map +1 -1
  54. package/package.json +10 -7
  55. package/dist/cjs/index.js.map +0 -1
  56. package/dist/es/index.js +0 -737
  57. package/dist/es/index.js.map +0 -1
  58. package/dist/esm/definitions.d.ts +0 -5
  59. package/dist/esm/definitions.js +0 -2
  60. package/dist/esm/definitions.js.map +0 -1
  61. package/dist/esm/form-control.js.map +0 -1
  62. package/dist/esm/form-group.js.map +0 -1
  63. package/dist/esm/form-ref.js.map +0 -1
  64. package/dist/esm/types.d.ts +0 -49
  65. package/dist/esm/types.js +0 -2
  66. package/dist/esm/types.js.map +0 -1
  67. package/dist/esm/utilities.d.ts +0 -2
  68. package/dist/esm/utilities.js.map +0 -1
package/dist/es/index.js DELETED
@@ -1,737 +0,0 @@
1
- import { createFormControlOptions, formControlIsValid, hasError, someErrors, createFormGroupOptions, formGroupIsValid, controlsToValue, verifyAllTrueInControls, verifyAnyTrueInControls, createFormArrayOptions, formArrayIsValid, verifyAllTrueInGroups, reduceControlsToArray } from '@rolster/forms/helpers';
2
- import { v4 } from 'uuid';
3
- import { useRef, useState, useEffect, useCallback, useMemo } from 'react';
4
-
5
- class RolsterArrayControl {
6
- constructor(options) {
7
- this.uuid = options.uuid;
8
- this.defaultValue = options.defaultValue;
9
- this.value = options.value;
10
- this.focused = !!options.focused;
11
- this.unfocused = !this.focused;
12
- this.touched = !!options.touched;
13
- this.untouched = !this.touched;
14
- this.dirty = !!options.dirty;
15
- this.pristine = !this.dirty;
16
- this.disabled = !!options.disabled;
17
- this.enabled = !this.disabled;
18
- this.valid = this.isValid(options.errors);
19
- this.invalid = !this.valid;
20
- this.wrong = this.touched && this.invalid;
21
- this.errors = options.errors;
22
- this.error = this.errors[0];
23
- this.validators = options.validators;
24
- }
25
- focus() {
26
- this.unfocused && this.refresh('focused', { focused: true });
27
- }
28
- blur() {
29
- this.focused && this.refresh('focused', { focused: false, touched: true });
30
- }
31
- disable() {
32
- this.enabled && this.refresh('disabled', { disabled: true });
33
- }
34
- enable() {
35
- this.disabled && this.refresh('disabled', { disabled: false });
36
- }
37
- touch() {
38
- this.untouched && this.refresh('touched', { touched: true });
39
- }
40
- setDefaultValue(value) {
41
- if (value !== this.defaultValue) {
42
- const errors = this.validators
43
- ? formControlIsValid({ value, validators: this.validators })
44
- : [];
45
- this.refresh('value', { errors, defaultValue: value, value });
46
- }
47
- }
48
- setStartValue(value) {
49
- if (value !== this.value) {
50
- const errors = this.validators
51
- ? formControlIsValid({ value, validators: this.validators })
52
- : [];
53
- this.refresh('value', { errors, value });
54
- }
55
- }
56
- setValue(value) {
57
- if (value !== this.value) {
58
- const errors = this.validators
59
- ? formControlIsValid({ value, validators: this.validators })
60
- : [];
61
- this.refresh('value', { dirty: true, errors, value });
62
- }
63
- }
64
- setValidators(validators) {
65
- const errors = validators
66
- ? formControlIsValid({ value: this.value, validators })
67
- : [];
68
- this.refresh('validators', { errors, validators });
69
- }
70
- subscribe(subscriber) {
71
- this.subscriber = subscriber;
72
- }
73
- hasError(key) {
74
- return hasError(this.errors, key);
75
- }
76
- someErrors(keys) {
77
- return someErrors(this.errors, keys);
78
- }
79
- reset() {
80
- const errors = this.validators
81
- ? formControlIsValid({
82
- value: this.defaultValue,
83
- validators: this.validators
84
- })
85
- : [];
86
- this.refresh('reset', {
87
- dirty: false,
88
- errors,
89
- touched: false,
90
- value: this.defaultValue
91
- });
92
- }
93
- isValid(errors) {
94
- return errors.length === 0;
95
- }
96
- builder(options) {
97
- return new RolsterArrayControl({ ...this, ...options });
98
- }
99
- refresh(action, options) {
100
- this.subscriber && this.subscriber(action, this.builder(options));
101
- }
102
- }
103
- class ReactRolsterArrayControl extends RolsterArrayControl {
104
- constructor(options) {
105
- const { value, validators } = options;
106
- const errors = validators ? formControlIsValid({ value, validators }) : [];
107
- super({ ...options, errors });
108
- }
109
- }
110
- function rolsterArrayControl(options, validators) {
111
- const formControl = createFormControlOptions(options, validators);
112
- return new ReactRolsterArrayControl({
113
- ...formControl,
114
- defaultValue: formControl.value,
115
- uuid: v4()
116
- });
117
- }
118
- function reactArrayControl(options, validators) {
119
- return rolsterArrayControl(options, validators);
120
- }
121
- function formArrayControl(options, validators) {
122
- return rolsterArrayControl(options, validators);
123
- }
124
- function inputArrayControl(options, validators) {
125
- return rolsterArrayControl(options, validators);
126
- }
127
-
128
- function replaceControl(controls, control) {
129
- return Object.entries(controls).reduce((controls, [key, _control]) => {
130
- if (_control.uuid === control.uuid) {
131
- controls[key] = control;
132
- }
133
- return controls;
134
- }, controls);
135
- }
136
-
137
- function refactorForValid$2(controls, validators) {
138
- const errors = validators ? formGroupIsValid({ controls, validators }) : [];
139
- return {
140
- errors,
141
- valid: errors.length === 0 && verifyAllTrueInControls(controls, 'valid')
142
- };
143
- }
144
- function refactorForControls$1(action, group, controls) {
145
- switch (action) {
146
- case 'focused':
147
- case 'touched':
148
- return {
149
- touched: verifyAnyTrueInControls(controls, 'touched'),
150
- toucheds: verifyAllTrueInControls(controls, 'touched')
151
- };
152
- case 'validators':
153
- return refactorForValid$2(controls, group.validators);
154
- case 'reset':
155
- return {
156
- ...refactorForValid$2(controls, group.validators),
157
- dirty: verifyAnyTrueInControls(controls, 'dirty'),
158
- dirties: verifyAllTrueInControls(controls, 'dirty'),
159
- touched: verifyAnyTrueInControls(controls, 'touched'),
160
- toucheds: verifyAllTrueInControls(controls, 'touched'),
161
- value: controlsToValue(controls)
162
- };
163
- case 'list':
164
- case 'value':
165
- return {
166
- ...refactorForValid$2(controls, group.validators),
167
- dirty: verifyAnyTrueInControls(controls, 'dirty'),
168
- dirties: verifyAllTrueInControls(controls, 'dirty'),
169
- value: controlsToValue(controls)
170
- };
171
- default:
172
- return {};
173
- }
174
- }
175
- class RolsterArrayGroup {
176
- constructor(options) {
177
- this.uuid = options.uuid;
178
- this.controls = options.controls;
179
- this.value = options.value;
180
- this.resource = options.resource;
181
- this.dirty = !!options.dirty;
182
- this.dirties = !!options.dirties;
183
- this.pristine = !this.dirty;
184
- this.pristines = !this.dirties;
185
- this.touched = !!options.touched;
186
- this.toucheds = !!options.toucheds;
187
- this.untouched = !this.touched;
188
- this.untoucheds = !this.toucheds;
189
- this.valid = !!options.valid;
190
- this.invalid = !this.valid;
191
- this.wrong = this.touched && this.invalid;
192
- this.errors = options.errors;
193
- this.error = this.errors[0];
194
- this.validators = options.validators;
195
- this.subscriberControl = (action, control) => {
196
- const controls = replaceControl(this.controls, control);
197
- this.refresh(action, {
198
- ...refactorForControls$1(action, this, controls),
199
- controls
200
- });
201
- };
202
- }
203
- subscribe(subscriber) {
204
- this.subscriber = subscriber;
205
- Object.values(this.controls).forEach((control) => {
206
- control.subscribe(this.subscriberControl);
207
- });
208
- }
209
- setValidators(validators) {
210
- this.refresh('validators', {
211
- ...refactorForValid$2(this.controls, validators),
212
- validators
213
- });
214
- }
215
- setResource(resource) {
216
- this.refresh('resource', { resource });
217
- }
218
- refresh(action, options) {
219
- this.subscriber &&
220
- this.subscriber(action, new RolsterArrayGroup({ ...this, ...options }));
221
- }
222
- }
223
- class ReactRolsterArrayGroup extends RolsterArrayGroup {
224
- constructor(options) {
225
- const { controls, validators } = options;
226
- const errors = validators ? formGroupIsValid({ controls, validators }) : [];
227
- super({
228
- ...options,
229
- errors,
230
- dirties: verifyAllTrueInControls(controls, 'dirty'),
231
- dirty: verifyAnyTrueInControls(controls, 'dirty'),
232
- touched: verifyAnyTrueInControls(controls, 'touched'),
233
- toucheds: verifyAllTrueInControls(controls, 'touched'),
234
- valid: errors.length === 0 && verifyAllTrueInControls(controls, 'valid'),
235
- value: controlsToValue(controls)
236
- });
237
- }
238
- }
239
- function valueIsGroupOptions(options) {
240
- return typeof options === 'object' && 'controls' in options;
241
- }
242
- function formArrayGroup(options, validators) {
243
- const _uuid = valueIsGroupOptions(options) ? options.uuid || v4() : v4();
244
- return new ReactRolsterArrayGroup({
245
- ...createFormGroupOptions(options, validators),
246
- uuid: _uuid
247
- });
248
- }
249
-
250
- class RolsterArrayList extends RolsterArrayControl {
251
- constructor(options) {
252
- const { controls, valueToControls, validators } = options;
253
- const value = controls.map(controlsToValue);
254
- const errors = validators ? formControlIsValid({ value, validators }) : [];
255
- super({ ...options, errors, value });
256
- this.valueToControls = valueToControls;
257
- this.controls = controls;
258
- this.valid =
259
- errors.length === 0 &&
260
- controls.reduce((valid, controls) => valid && verifyAllTrueInControls(controls, 'valid'), true);
261
- this.invalid = !this.valid;
262
- this.wrong = this.touched && this.invalid;
263
- controls.forEach((reactControls) => {
264
- this._subscribe(reactControls);
265
- });
266
- }
267
- setDefaultValue(value) {
268
- this.refresh('list', {
269
- controls: value.map(this.valueToControls),
270
- defaultValue: value
271
- });
272
- }
273
- setStartValue(value) {
274
- this.refresh('list', {
275
- controls: value.map(this.valueToControls)
276
- });
277
- }
278
- setValue(value) {
279
- this.refresh('list', {
280
- controls: value.map(this.valueToControls),
281
- dirty: true
282
- });
283
- }
284
- reset() {
285
- this.refresh('list', {
286
- controls: this.defaultValue.map(this.valueToControls),
287
- dirty: false,
288
- touched: false
289
- });
290
- }
291
- push(controls) {
292
- this.refresh('list', {
293
- controls: [...this.controls, controls]
294
- });
295
- }
296
- remove(controls) {
297
- this.refresh('list', {
298
- controls: this.controls.filter((_controls) => _controls !== controls)
299
- });
300
- }
301
- builder(options) {
302
- return new RolsterArrayList({
303
- ...this,
304
- ...options,
305
- valueToControls: this.valueToControls
306
- });
307
- }
308
- refresh(action, options) {
309
- super.refresh(action, options);
310
- }
311
- _subscribe(reactControls) {
312
- Object.values(reactControls).forEach((control) => {
313
- control.subscribe((action, _control) => {
314
- const _reactControls = this.controls.map((_controls) => reactControls !== _controls
315
- ? _controls
316
- : replaceControl(reactControls, _control));
317
- this.refresh(action, { controls: _reactControls });
318
- });
319
- });
320
- }
321
- }
322
- function formArrayList(options) {
323
- const value = options?.value || [];
324
- return new RolsterArrayList({
325
- ...options,
326
- controls: value.map((value) => options.valueToControls(value)),
327
- defaultValue: value,
328
- uuid: v4()
329
- });
330
- }
331
-
332
- function refactorForValid$1(groups, validators) {
333
- const errors = validators ? formArrayIsValid({ groups, validators }) : [];
334
- return {
335
- errors,
336
- valid: errors.length === 0 && verifyAllTrueInGroups(groups, 'valid')
337
- };
338
- }
339
- function refactorForGroups(groups, validators) {
340
- return {
341
- ...refactorForValid$1(groups, validators),
342
- groups,
343
- controls: groups.map(({ controls }) => controls),
344
- value: groups.map(({ controls }) => controlsToValue(controls))
345
- };
346
- }
347
- function refactorForControls(action, state, groups) {
348
- switch (action) {
349
- case 'validators':
350
- return refactorForValid$1(groups, state.validators);
351
- case 'reset':
352
- case 'list':
353
- case 'value':
354
- return refactorForGroups(groups, state.validators);
355
- default:
356
- return { groups };
357
- }
358
- }
359
- function useFormArray(options, validators) {
360
- const formArray = createFormArrayOptions(options, validators);
361
- const defaultValue = useRef(formArray.groups || []);
362
- const formGroups = useRef(new Map());
363
- const [state, setState] = useState(() => {
364
- return {
365
- ...refactorForValid$1(defaultValue.current, formArray.validators),
366
- controls: defaultValue.current.map(({ controls }) => controls),
367
- dirty: false,
368
- dirties: false,
369
- disabled: false,
370
- groups: defaultValue.current,
371
- touched: false,
372
- toucheds: false,
373
- value: defaultValue.current.map(({ controls }) => controlsToValue(controls)),
374
- validators: formArray.validators
375
- };
376
- });
377
- useEffect(() => {
378
- formGroups.current.clear();
379
- state.groups.forEach((group) => {
380
- formGroups.current.set(group.uuid, group);
381
- group.subscribe(subscriber);
382
- });
383
- }, [state.groups]);
384
- const subscriber = useCallback((action, group) => {
385
- setState((state) => {
386
- const groups = state.groups.map((currentGroup) => {
387
- return currentGroup.uuid === group.uuid ? group : currentGroup;
388
- });
389
- return {
390
- ...state,
391
- ...refactorForControls(action, state, groups)
392
- };
393
- });
394
- }, []);
395
- const disable = useCallback(() => {
396
- setState((state) => ({ ...state, disabled: true }));
397
- }, []);
398
- const enable = useCallback(() => {
399
- setState((state) => ({ ...state, disabled: false }));
400
- }, []);
401
- const setValue = useCallback((groups) => {
402
- setState((state) => ({
403
- ...state,
404
- ...refactorForGroups(groups, state.validators)
405
- }));
406
- }, []);
407
- const setDefaultValue = useCallback((groups) => {
408
- setState((state) => ({
409
- ...state,
410
- ...refactorForGroups(groups, state.validators)
411
- }));
412
- defaultValue.current = groups;
413
- }, []);
414
- const push = useCallback((group) => {
415
- setState((state) => ({
416
- ...state,
417
- ...refactorForGroups([...state.groups, group], state.validators)
418
- }));
419
- }, []);
420
- const merge = useCallback((groups) => {
421
- setState((state) => ({
422
- ...state,
423
- ...refactorForGroups([...state.groups, ...groups], state.validators)
424
- }));
425
- }, []);
426
- const remove = useCallback(({ uuid }) => {
427
- setState((state) => ({
428
- ...state,
429
- ...refactorForGroups(state.groups.filter((group) => group.uuid !== uuid), state.validators)
430
- }));
431
- }, []);
432
- const findByUuid = useCallback((uuid) => {
433
- return formGroups.current.get(uuid);
434
- }, [state.groups]);
435
- const setValidators = useCallback((validators) => {
436
- setState((state) => ({
437
- ...state,
438
- ...refactorForValid$1(state.groups, validators),
439
- validators
440
- }));
441
- }, []);
442
- const hasError$1 = useCallback((key) => {
443
- return hasError(state.errors, key);
444
- }, [state.errors]);
445
- const someErrors$1 = useCallback((keys) => {
446
- return someErrors(state.errors, keys);
447
- }, [state.errors]);
448
- const reset = useCallback(() => {
449
- setState((state) => ({
450
- ...state,
451
- ...refactorForGroups(defaultValue.current, state.validators)
452
- }));
453
- }, []);
454
- return {
455
- ...state,
456
- disable,
457
- enable,
458
- enabled: !state.disabled,
459
- error: state.errors[0],
460
- findByUuid,
461
- hasError: hasError$1,
462
- invalid: !state.valid,
463
- merge,
464
- pristine: !state.dirty,
465
- pristines: !state.dirties,
466
- push,
467
- remove,
468
- reset,
469
- setDefaultValue,
470
- setValidators,
471
- setValue,
472
- someErrors: someErrors$1,
473
- untouched: !state.touched,
474
- untoucheds: !state.toucheds,
475
- wrong: state.touched && !state.valid
476
- };
477
- }
478
-
479
- function errorsInControl(value, validators) {
480
- return validators ? formControlIsValid({ value, validators }) : [];
481
- }
482
- function useControl(options, validators) {
483
- const formControl = createFormControlOptions(options, validators);
484
- const defaultValue = useRef(formControl.value);
485
- const [state, setState] = useState(() => {
486
- return {
487
- dirty: false,
488
- disabled: false,
489
- errors: errorsInControl(formControl.value, formControl.validators),
490
- focused: false,
491
- touched: !!formControl.touched,
492
- value: formControl.value,
493
- validators: formControl.validators
494
- };
495
- });
496
- const elementRef = useRef(null);
497
- const focus = useCallback(() => {
498
- setState((state) => ({ ...state, focused: true }));
499
- }, []);
500
- const blur = useCallback(() => {
501
- setState((state) => ({ ...state, focused: false, touched: true }));
502
- }, []);
503
- const disable = useCallback(() => {
504
- setState((state) => ({ ...state, disabled: true }));
505
- }, []);
506
- const enable = useCallback(() => {
507
- setState((state) => ({ ...state, disabled: false }));
508
- }, []);
509
- const touch = useCallback(() => {
510
- setState((state) => ({ ...state, touched: true }));
511
- }, []);
512
- const setDefaultValue = useCallback((value) => {
513
- defaultValue.current = value;
514
- setState((state) => ({
515
- ...state,
516
- errors: errorsInControl(value, state.validators),
517
- value
518
- }));
519
- }, []);
520
- const setStartValue = useCallback((value) => {
521
- setState((state) => ({
522
- ...state,
523
- errors: errorsInControl(value, state.validators),
524
- value
525
- }));
526
- }, []);
527
- const setValue = useCallback((value) => {
528
- setState((state) => ({
529
- ...state,
530
- dirty: true,
531
- errors: errorsInControl(value, state.validators),
532
- value
533
- }));
534
- }, []);
535
- const setValidators = useCallback((validators) => {
536
- setState((state) => ({
537
- ...state,
538
- errors: errorsInControl(state.value, validators),
539
- validators
540
- }));
541
- }, []);
542
- const reset = useCallback(() => {
543
- setState((state) => ({
544
- ...state,
545
- dirty: false,
546
- errors: errorsInControl(defaultValue.current, state.validators),
547
- value: defaultValue.current,
548
- touched: false
549
- }));
550
- }, []);
551
- const hasError$1 = useCallback((key) => {
552
- return hasError(state.errors, key);
553
- }, [state.errors]);
554
- const someErrors$1 = useCallback((keys) => {
555
- return someErrors(state.errors, keys);
556
- }, [state.errors]);
557
- const valid = state.errors.length === 0;
558
- return {
559
- ...state,
560
- blur,
561
- disable,
562
- elementRef,
563
- enable,
564
- enabled: !state.disabled,
565
- error: state.errors[0],
566
- focus,
567
- hasError: hasError$1,
568
- invalid: !valid,
569
- pristine: !state.dirty,
570
- reset,
571
- setDefaultValue,
572
- setStartValue,
573
- setValidators,
574
- setValue,
575
- someErrors: someErrors$1,
576
- touch,
577
- unfocused: !state.focused,
578
- untouched: !state.touched,
579
- valid,
580
- wrong: state.touched && !valid
581
- };
582
- }
583
- function useReactControl(options, validators) {
584
- return useControl(options, validators);
585
- }
586
- function useFormControl(options, validators) {
587
- return useControl(options, validators);
588
- }
589
- function useInputControl(options, validators) {
590
- return useControl(options, validators);
591
- }
592
-
593
- function refactorForValid(controls, validators) {
594
- const errors = validators ? formGroupIsValid({ controls, validators }) : [];
595
- return {
596
- errors,
597
- valid: errors.length === 0 && verifyAllTrueInControls(controls, 'valid')
598
- };
599
- }
600
- function checkAllSuccess(status) {
601
- return status.reduce((success, status) => success && status, true);
602
- }
603
- function checkPartialSuccess(status) {
604
- return status.reduce((success, status) => success || status, false);
605
- }
606
- function useFormGroup(options, validators) {
607
- const formGroup = createFormGroupOptions(options, validators);
608
- const formInitialized = useRef({
609
- dirty: false,
610
- touched: false,
611
- value: false,
612
- visual: false
613
- });
614
- const formGroupStatus = useMemo(() => {
615
- const dirty = [];
616
- const touched = [];
617
- const value = [];
618
- const visual = [];
619
- Object.values(formGroup.controls).forEach((control) => {
620
- dirty.push(control.dirty);
621
- touched.push(control.touched);
622
- value.push(control.value);
623
- visual.push(control.disabled);
624
- visual.push(control.focused);
625
- });
626
- return {
627
- dirty,
628
- touched,
629
- value,
630
- visual
631
- };
632
- }, [formGroup.controls]);
633
- const [state, setState] = useState(() => {
634
- return {
635
- ...refactorForValid(formGroup.controls, formGroup.validators),
636
- controls: formGroup.controls,
637
- dirties: checkAllSuccess(formGroupStatus.dirty),
638
- dirty: checkPartialSuccess(formGroupStatus.dirty),
639
- touched: checkPartialSuccess(formGroupStatus.touched),
640
- toucheds: checkAllSuccess(formGroupStatus.touched),
641
- validators: formGroup.validators,
642
- value: controlsToValue(formGroup.controls)
643
- };
644
- });
645
- useEffect(() => {
646
- if (formInitialized.current.value) {
647
- setState((state) => ({
648
- ...state,
649
- ...refactorForValid(formGroup.controls, state.validators),
650
- controls: formGroup.controls,
651
- value: controlsToValue(formGroup.controls)
652
- }));
653
- }
654
- else {
655
- formInitialized.current.value = true;
656
- }
657
- }, formGroupStatus.value);
658
- useEffect(() => {
659
- if (formInitialized.current.dirty) {
660
- setState((state) => ({
661
- ...state,
662
- controls: formGroup.controls,
663
- dirty: checkPartialSuccess(formGroupStatus.dirty),
664
- dirties: checkAllSuccess(formGroupStatus.dirty)
665
- }));
666
- }
667
- else {
668
- formInitialized.current.dirty = true;
669
- }
670
- }, formGroupStatus.dirty);
671
- useEffect(() => {
672
- if (formInitialized.current.touched) {
673
- setState((state) => ({
674
- ...state,
675
- controls: formGroup.controls,
676
- touched: checkPartialSuccess(formGroupStatus.touched),
677
- toucheds: checkAllSuccess(formGroupStatus.touched)
678
- }));
679
- }
680
- else {
681
- formInitialized.current.touched = true;
682
- }
683
- }, formGroupStatus.touched);
684
- useEffect(() => {
685
- if (formInitialized.current.visual) {
686
- setState((state) => ({
687
- ...state,
688
- controls: formGroup.controls
689
- }));
690
- }
691
- else {
692
- formInitialized.current.visual = true;
693
- }
694
- }, formGroupStatus.visual);
695
- const setValidators = useCallback((validators) => {
696
- setState((state) => ({
697
- ...state,
698
- ...refactorForValid(state.controls, validators)
699
- }));
700
- }, []);
701
- const reset = useCallback(() => {
702
- Object.values(formGroup.controls).forEach((control) => {
703
- control.reset();
704
- });
705
- }, []);
706
- return {
707
- ...state,
708
- error: state.errors[0],
709
- invalid: !state.valid,
710
- pristine: !state.dirty,
711
- pristines: !state.dirties,
712
- reset,
713
- setValidators,
714
- untouched: !state.touched,
715
- untoucheds: !state.toucheds,
716
- wrong: state.touched && !state.valid
717
- };
718
- }
719
-
720
- function reduceControlsToValues(controls) {
721
- return reduceControlsToArray(controls, 'value');
722
- }
723
- function reduceGroupToValues(group) {
724
- return reduceControlsToValues(group.controls);
725
- }
726
-
727
- function useFormArrayGroupSelect({ formArray }) {
728
- const [formSelect, setFormGroup] = useState();
729
- const formGroup = useMemo(() => {
730
- return (formSelect &&
731
- formArray.groups.find(({ uuid }) => formSelect.uuid === uuid));
732
- }, [formArray.value, formSelect]);
733
- return { formGroup, setFormGroup };
734
- }
735
-
736
- export { formArrayControl, formArrayGroup, formArrayList, inputArrayControl, reactArrayControl, reduceControlsToValues, reduceGroupToValues, useFormArray, useFormArrayGroupSelect, useFormControl, useFormGroup, useInputControl, useReactControl };
737
- //# sourceMappingURL=index.js.map