@rolster/react-forms 18.8.2 → 18.9.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.
package/dist/es/index.js CHANGED
@@ -1,437 +1,440 @@
1
1
  import { createFormControlOptions, createFormGroupOptions, createFormArrayOptions } from '@rolster/forms/arguments';
2
2
  import { controlIsValid, hasError, someErrors, groupIsValid, controlsAllChecked, controlsPartialChecked, controlsToValue, arrayIsValid, groupAllChecked, groupPartialChecked } from '@rolster/forms/helpers';
3
3
  import { v4 } from 'uuid';
4
- import { useRef, useState, useEffect } from 'react';
4
+ import { useRef, useState, useEffect, useCallback } from 'react';
5
5
 
6
- class RolsterReactArrayControl {
7
- constructor(options) {
8
- this.initialValue = options.initialValue;
9
- this.uuid = options.uuid;
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
- const { value, validators } = options;
19
- this.value = value;
20
- this.validators = validators;
21
- this.errors = validators ? controlIsValid({ value, validators }) : [];
22
- this.error = this.errors[0];
23
- }
24
- focus() {
25
- this.unfocused && this.refresh({ focused: true });
26
- }
27
- blur() {
28
- this.focused && this.refresh({ focused: false, touched: true });
29
- }
30
- disable() {
31
- this.enabled && this.refresh({ disabled: true });
32
- }
33
- enable() {
34
- this.disabled && this.refresh({ disabled: false });
35
- }
36
- touch() {
37
- this.untouched && this.refresh({ touched: true });
38
- }
39
- setValue(value) {
40
- this.refresh({ value });
41
- }
42
- setValidators(validators) {
43
- this.refresh({ validators });
44
- }
45
- subscribe(subscriber) {
46
- this.subscriber = subscriber;
47
- }
48
- hasError(key) {
49
- return hasError(this.errors, key);
50
- }
51
- someErrors(keys) {
52
- return someErrors(this.errors, keys);
53
- }
54
- reset() {
55
- this.refresh({
56
- dirty: false,
57
- touched: false,
58
- value: this.initialValue
59
- });
60
- }
61
- refresh(changes) {
62
- this.subscriber &&
63
- this.subscriber({
64
- ...this,
65
- ...changes,
66
- initialValue: this.initialValue
67
- });
68
- }
69
- }
70
- class RolsterArrayControl extends RolsterReactArrayControl {
71
- constructor(options) {
72
- super(options);
73
- this.valid = this.errors.length === 0;
74
- this.invalid = !this.valid;
75
- this.wrong = this.touched && this.invalid;
76
- }
77
- clone(options) {
78
- return new RolsterArrayControl(options);
79
- }
80
- }
81
- function rolsterArrayControl(options, validators) {
82
- const controlOptions = createFormControlOptions(options, validators);
83
- return new RolsterArrayControl({
84
- ...controlOptions,
85
- initialValue: controlOptions.value,
86
- uuid: v4()
87
- });
88
- }
89
- function reactArrayControl(options, validators) {
90
- return rolsterArrayControl(options, validators);
91
- }
92
- function formArrayControl(options, validators) {
93
- return rolsterArrayControl(options, validators);
94
- }
95
- function inputArrayControl(options, validators) {
96
- return rolsterArrayControl(options, validators);
6
+ class RolsterReactArrayControl {
7
+ constructor(options) {
8
+ this.initialValue = options.initialValue;
9
+ this.uuid = options.uuid;
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
+ const { value, validators } = options;
19
+ this.value = value;
20
+ this.validators = validators;
21
+ this.errors = validators ? controlIsValid({ value, validators }) : [];
22
+ this.error = this.errors[0];
23
+ }
24
+ focus() {
25
+ this.unfocused && this.refresh({ focused: true });
26
+ }
27
+ blur() {
28
+ this.focused && this.refresh({ focused: false, touched: true });
29
+ }
30
+ disable() {
31
+ this.enabled && this.refresh({ disabled: true });
32
+ }
33
+ enable() {
34
+ this.disabled && this.refresh({ disabled: false });
35
+ }
36
+ touch() {
37
+ this.untouched && this.refresh({ touched: true });
38
+ }
39
+ setValue(value) {
40
+ this.refresh({ value });
41
+ }
42
+ setValidators(validators) {
43
+ this.refresh({ validators });
44
+ }
45
+ subscribe(subscriber) {
46
+ this.subscriber = subscriber;
47
+ }
48
+ hasError(key) {
49
+ return hasError(this.errors, key);
50
+ }
51
+ someErrors(keys) {
52
+ return someErrors(this.errors, keys);
53
+ }
54
+ reset() {
55
+ this.refresh({
56
+ dirty: false,
57
+ touched: false,
58
+ value: this.initialValue
59
+ });
60
+ }
61
+ refresh(changes) {
62
+ this.subscriber &&
63
+ this.subscriber({
64
+ ...this,
65
+ ...changes,
66
+ initialValue: this.initialValue
67
+ });
68
+ }
69
+ }
70
+ class RolsterArrayControl extends RolsterReactArrayControl {
71
+ constructor(options) {
72
+ super(options);
73
+ this.valid = this.errors.length === 0;
74
+ this.invalid = !this.valid;
75
+ this.wrong = this.touched && this.invalid;
76
+ }
77
+ clone(options) {
78
+ return new RolsterArrayControl(options);
79
+ }
80
+ }
81
+ function rolsterArrayControl(options, validators) {
82
+ const controlOptions = createFormControlOptions(options, validators);
83
+ return new RolsterArrayControl({
84
+ ...controlOptions,
85
+ initialValue: controlOptions.value,
86
+ uuid: v4()
87
+ });
88
+ }
89
+ function reactArrayControl(options, validators) {
90
+ return rolsterArrayControl(options, validators);
91
+ }
92
+ function formArrayControl(options, validators) {
93
+ return rolsterArrayControl(options, validators);
94
+ }
95
+ function inputArrayControl(options, validators) {
96
+ return rolsterArrayControl(options, validators);
97
97
  }
98
98
 
99
- class RolsterArrayGroup {
100
- constructor(options) {
101
- const { controls, resource, uuid, validators } = options;
102
- this.uuid = uuid;
103
- this.controls = controls;
104
- this.validators = validators;
105
- this.resource = resource;
106
- this.errors = validators ? groupIsValid({ controls, validators }) : [];
107
- this.error = this.errors[0];
108
- this.valid =
109
- this.errors.length === 0 && controlsAllChecked(controls, 'valid');
110
- this.invalid = !this.valid;
111
- this.dirty = controlsPartialChecked(controls, 'dirty');
112
- this.dirtyAll = controlsAllChecked(controls, 'dirty');
113
- this.touched = controlsPartialChecked(controls, 'touched');
114
- this.touchedAll = controlsAllChecked(controls, 'touched');
115
- this.untouched = !this.touched;
116
- this.untouchedAll = !this.touchedAll;
117
- this.pristine = !this.dirty;
118
- this.pristineAll = !this.dirtyAll;
119
- this.wrong = this.touched && this.invalid;
120
- this.value = controlsToValue(controls);
121
- const subscriber = (options) => {
122
- this.refresh({
123
- controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
124
- controls[key] =
125
- control.uuid === options.uuid ? control.clone(options) : control;
126
- return controls;
127
- }, {})
128
- });
129
- };
130
- Object.values(controls).forEach((control) => {
131
- control.subscribe(subscriber);
132
- });
133
- }
134
- setValidators(validators) {
135
- this.refresh({ validators });
136
- }
137
- subscribe(subscriber) {
138
- this.subscriber = subscriber;
139
- }
140
- refresh(changes) {
141
- this.subscriber && this.subscriber({ ...this, ...changes });
142
- }
143
- }
144
- function formArrayGroup(options, validators) {
145
- return new RolsterArrayGroup({
146
- ...createFormGroupOptions(options, validators),
147
- uuid: v4()
148
- });
99
+ class RolsterArrayGroup {
100
+ constructor(options) {
101
+ const { controls, resource, uuid, validators } = options;
102
+ this.uuid = uuid;
103
+ this.controls = controls;
104
+ this.validators = validators;
105
+ this.resource = resource;
106
+ this.errors = validators ? groupIsValid({ controls, validators }) : [];
107
+ this.error = this.errors[0];
108
+ this.valid =
109
+ this.errors.length === 0 && controlsAllChecked(controls, 'valid');
110
+ this.invalid = !this.valid;
111
+ this.dirty = controlsPartialChecked(controls, 'dirty');
112
+ this.dirtyAll = controlsAllChecked(controls, 'dirty');
113
+ this.touched = controlsPartialChecked(controls, 'touched');
114
+ this.touchedAll = controlsAllChecked(controls, 'touched');
115
+ this.untouched = !this.touched;
116
+ this.untouchedAll = !this.touchedAll;
117
+ this.pristine = !this.dirty;
118
+ this.pristineAll = !this.dirtyAll;
119
+ this.wrong = this.touched && this.invalid;
120
+ this.value = controlsToValue(controls);
121
+ const subscriber = (options) => {
122
+ this.refresh({
123
+ controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
124
+ controls[key] =
125
+ control.uuid === options.uuid ? control.clone(options) : control;
126
+ return controls;
127
+ }, {})
128
+ });
129
+ };
130
+ Object.values(controls).forEach((control) => {
131
+ control.subscribe(subscriber);
132
+ });
133
+ }
134
+ setValidators(validators) {
135
+ this.refresh({ validators });
136
+ }
137
+ subscribe(subscriber) {
138
+ this.subscriber = subscriber;
139
+ }
140
+ refresh(changes) {
141
+ this.subscriber && this.subscriber({ ...this, ...changes });
142
+ }
143
+ }
144
+ function formArrayGroup(options, validators) {
145
+ return new RolsterArrayGroup({
146
+ ...createFormGroupOptions(options, validators),
147
+ uuid: v4()
148
+ });
149
149
  }
150
150
 
151
- class RolsterArrayList extends RolsterReactArrayControl {
152
- constructor(options) {
153
- const value = options.controls.map((controls) => controlsToValue(controls));
154
- super({ ...options, value });
155
- this.valueToControls = options.valueToControls;
156
- this.controls = options.controls;
157
- this.valid =
158
- this.errors.length === 0 &&
159
- this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
160
- this.invalid = !this.valid;
161
- this.wrong = this.touched && this.invalid;
162
- options.controls.forEach((controls) => {
163
- this.subscribeControls(controls);
164
- });
165
- }
166
- setValue(value) {
167
- this.refresh({
168
- controls: value.map((value) => this.valueToControls(value))
169
- });
170
- }
171
- clone(options) {
172
- return new RolsterArrayList(options);
173
- }
174
- push(controls) {
175
- this.refresh({
176
- controls: [...this.controls, controls]
177
- });
178
- }
179
- remove(controls) {
180
- this.refresh({
181
- controls: this.controls.filter((currentControls) => currentControls !== controls)
182
- });
183
- }
184
- refresh(changes) {
185
- super.refresh(changes);
186
- }
187
- subscribeControls(newControls) {
188
- Object.values(newControls).forEach((control) => {
189
- control.subscribe((options) => {
190
- const controls = this.controls.map((currentControls) => currentControls !== newControls
191
- ? currentControls
192
- : Object.entries(newControls).reduce((controls, [key, control]) => {
193
- controls[key] =
194
- control.uuid === options.uuid
195
- ? control.clone(options)
196
- : control;
197
- return controls;
198
- }, {}));
199
- this.refresh({ controls });
200
- });
201
- });
202
- }
203
- }
204
- function formArrayList(valueToControls, options) {
205
- const value = options?.value || [];
206
- return new RolsterArrayList({
207
- ...options,
208
- valueToControls,
209
- controls: value.map((value) => valueToControls(value)),
210
- initialValue: value,
211
- uuid: v4()
212
- });
151
+ class RolsterArrayList extends RolsterReactArrayControl {
152
+ constructor(options) {
153
+ const value = options.controls.map((controls) => controlsToValue(controls));
154
+ super({ ...options, value });
155
+ this.valueToControls = options.valueToControls;
156
+ this.controls = options.controls;
157
+ this.valid =
158
+ this.errors.length === 0 &&
159
+ this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
160
+ this.invalid = !this.valid;
161
+ this.wrong = this.touched && this.invalid;
162
+ options.controls.forEach((controls) => {
163
+ this.subscribeControls(controls);
164
+ });
165
+ }
166
+ setValue(value) {
167
+ this.refresh({
168
+ controls: value.map((value) => this.valueToControls(value))
169
+ });
170
+ }
171
+ clone(options) {
172
+ return new RolsterArrayList(options);
173
+ }
174
+ push(controls) {
175
+ this.refresh({
176
+ controls: [...this.controls, controls]
177
+ });
178
+ }
179
+ remove(controls) {
180
+ this.refresh({
181
+ controls: this.controls.filter((currentControls) => currentControls !== controls)
182
+ });
183
+ }
184
+ refresh(changes) {
185
+ super.refresh(changes);
186
+ }
187
+ subscribeControls(newControls) {
188
+ Object.values(newControls).forEach((control) => {
189
+ control.subscribe((options) => {
190
+ const controls = this.controls.map((currentControls) => currentControls !== newControls
191
+ ? currentControls
192
+ : Object.entries(newControls).reduce((controls, [key, control]) => {
193
+ controls[key] =
194
+ control.uuid === options.uuid
195
+ ? control.clone(options)
196
+ : control;
197
+ return controls;
198
+ }, {}));
199
+ this.refresh({ controls });
200
+ });
201
+ });
202
+ }
203
+ }
204
+ function formArrayList(valueToControls, options) {
205
+ const value = options?.value || [];
206
+ return new RolsterArrayList({
207
+ ...options,
208
+ valueToControls,
209
+ controls: value.map((value) => valueToControls(value)),
210
+ initialValue: value,
211
+ uuid: v4()
212
+ });
213
213
  }
214
214
 
215
- function useFormArray(options, arrayValidators) {
216
- const arrayOptions = createFormArrayOptions(options, arrayValidators);
217
- const groups = arrayOptions.groups || [];
218
- const currentState = useRef(groups);
219
- const [state, setState] = useState({
220
- controls: groups.map(({ controls }) => controls),
221
- disabled: false,
222
- groups,
223
- value: groups.map(({ controls }) => controlsToValue(controls)),
224
- validators: arrayOptions.validators
225
- });
226
- const errors = state.validators
227
- ? arrayIsValid({ groups, validators: state.validators })
228
- : [];
229
- const error = errors[0];
230
- const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
231
- const dirty = groupPartialChecked(state.groups, 'dirty');
232
- const dirtyAll = groupAllChecked(state.groups, 'dirty');
233
- const touched = groupPartialChecked(state.groups, 'touched');
234
- const touchedAll = groupAllChecked(state.groups, 'touched');
235
- useEffect(() => {
236
- const subscriber = (options) => {
237
- setGroups(state.groups.map((group) => group.uuid === options.uuid
238
- ? new RolsterArrayGroup(options)
239
- : group));
240
- };
241
- state.groups.forEach((group) => {
242
- group.subscribe(subscriber);
243
- });
244
- }, [state.groups]);
245
- function disable() {
246
- setState((state) => ({ ...state, disabled: true }));
247
- }
248
- function enable() {
249
- setState((state) => ({ ...state, disabled: false }));
250
- }
251
- function setGroups(groups) {
252
- setState((currentState) => ({
253
- ...currentState,
254
- groups,
255
- controls: groups.map(({ controls }) => controls),
256
- value: groups.map(({ controls }) => controlsToValue(controls))
257
- }));
258
- }
259
- function push(group) {
260
- setGroups([...state.groups, group]);
261
- }
262
- function merge(groups) {
263
- setGroups([...state.groups, ...groups]);
264
- }
265
- function set(groups) {
266
- setGroups(groups);
267
- }
268
- function remove({ uuid }) {
269
- setGroups(state.groups.filter((group) => group.uuid !== uuid));
270
- }
271
- function hasError$1(key) {
272
- return hasError(errors, key);
273
- }
274
- function someErrors$1(keys) {
275
- return someErrors(errors, keys);
276
- }
277
- function reset() {
278
- setGroups(currentState.current);
279
- }
280
- function setValidators(validators) {
281
- setState((state) => ({ ...state, validators }));
282
- }
283
- return {
284
- ...state,
285
- dirty,
286
- dirtyAll,
287
- disable,
288
- enable,
289
- enabled: !state.disabled,
290
- error,
291
- errors,
292
- hasError: hasError$1,
293
- invalid: !valid,
294
- merge,
295
- pristine: !dirty,
296
- pristineAll: !dirtyAll,
297
- push,
298
- remove,
299
- reset,
300
- set,
301
- setValidators,
302
- someErrors: someErrors$1,
303
- touched,
304
- touchedAll,
305
- untouched: !touched,
306
- untouchedAll: !touchedAll,
307
- valid,
308
- wrong: touched && !valid
309
- };
215
+ function useFormArray(options, arrayValidators) {
216
+ const arrayOptions = createFormArrayOptions(options, arrayValidators);
217
+ const groups = arrayOptions.groups || [];
218
+ const currentState = useRef(groups);
219
+ const [state, setState] = useState({
220
+ controls: groups.map(({ controls }) => controls),
221
+ disabled: false,
222
+ groups,
223
+ value: groups.map(({ controls }) => controlsToValue(controls)),
224
+ validators: arrayOptions.validators
225
+ });
226
+ const errors = state.validators
227
+ ? arrayIsValid({ groups, validators: state.validators })
228
+ : [];
229
+ const error = errors[0];
230
+ const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
231
+ const dirty = groupPartialChecked(state.groups, 'dirty');
232
+ const dirtyAll = groupAllChecked(state.groups, 'dirty');
233
+ const touched = groupPartialChecked(state.groups, 'touched');
234
+ const touchedAll = groupAllChecked(state.groups, 'touched');
235
+ useEffect(() => {
236
+ const subscriber = (options) => {
237
+ setGroups(state.groups.map((group) => group.uuid === options.uuid
238
+ ? new RolsterArrayGroup(options)
239
+ : group));
240
+ };
241
+ state.groups.forEach((group) => {
242
+ group.subscribe(subscriber);
243
+ });
244
+ }, [state.groups]);
245
+ function disable() {
246
+ setState((state) => ({ ...state, disabled: true }));
247
+ }
248
+ function enable() {
249
+ setState((state) => ({ ...state, disabled: false }));
250
+ }
251
+ function setGroups(groups) {
252
+ setState((currentState) => ({
253
+ ...currentState,
254
+ groups,
255
+ controls: groups.map(({ controls }) => controls),
256
+ value: groups.map(({ controls }) => controlsToValue(controls))
257
+ }));
258
+ }
259
+ function push(group) {
260
+ setGroups([...state.groups, group]);
261
+ }
262
+ function merge(groups) {
263
+ setGroups([...state.groups, ...groups]);
264
+ }
265
+ function set(groups) {
266
+ setGroups(groups);
267
+ }
268
+ function remove({ uuid }) {
269
+ setGroups(state.groups.filter((group) => group.uuid !== uuid));
270
+ }
271
+ function hasError$1(key) {
272
+ return hasError(errors, key);
273
+ }
274
+ function someErrors$1(keys) {
275
+ return someErrors(errors, keys);
276
+ }
277
+ function reset() {
278
+ setGroups(currentState.current);
279
+ }
280
+ function setValidators(validators) {
281
+ setState((state) => ({ ...state, validators }));
282
+ }
283
+ return {
284
+ ...state,
285
+ dirty,
286
+ dirtyAll,
287
+ disable,
288
+ enable,
289
+ enabled: !state.disabled,
290
+ error,
291
+ errors,
292
+ hasError: hasError$1,
293
+ invalid: !valid,
294
+ merge,
295
+ pristine: !dirty,
296
+ pristineAll: !dirtyAll,
297
+ push,
298
+ remove,
299
+ reset,
300
+ set,
301
+ setValidators,
302
+ someErrors: someErrors$1,
303
+ touched,
304
+ touchedAll,
305
+ untouched: !touched,
306
+ untouchedAll: !touchedAll,
307
+ valid,
308
+ wrong: touched && !valid
309
+ };
310
310
  }
311
311
 
312
- function useControl(controlOptions, controlValidators) {
313
- const { value, touched, validators } = createFormControlOptions(controlOptions, controlValidators);
314
- const [state, setState] = useState({
315
- dirty: false,
316
- disabled: false,
317
- focused: false,
318
- touched: !!touched,
319
- validators,
320
- value
321
- });
322
- const initialValue = useRef(value);
323
- const elementRef = useRef(null);
324
- const errors = state.validators
325
- ? controlIsValid({
326
- value: state.value,
327
- validators: state.validators
328
- })
329
- : [];
330
- const valid = errors.length === 0;
331
- function focus() {
332
- setState((state) => ({ ...state, focused: true }));
333
- }
334
- function blur() {
335
- setState((state) => ({ ...state, focused: false, touched: true }));
336
- }
337
- function disable() {
338
- setState((state) => ({ ...state, disabled: true }));
339
- }
340
- function enable() {
341
- setState((state) => ({ ...state, disabled: false }));
342
- }
343
- function touch() {
344
- setState((state) => ({ ...state, touched: true }));
345
- }
346
- function setValue(value) {
347
- setState((state) => ({ ...state, dirty: true, value }));
348
- }
349
- function setValidators(validators) {
350
- setState((state) => ({ ...state, validators }));
351
- }
352
- function hasError$1(key) {
353
- return hasError(errors, key);
354
- }
355
- function someErrors$1(keys) {
356
- return someErrors(errors, keys);
357
- }
358
- function reset() {
359
- setState((state) => ({
360
- ...state,
361
- dirty: false,
362
- value: initialValue.current,
363
- touched: false
364
- }));
365
- }
366
- return {
367
- ...state,
368
- blur,
369
- disable,
370
- elementRef,
371
- enable,
372
- enabled: !state.disabled,
373
- error: errors[0],
374
- errors,
375
- focus,
376
- hasError: hasError$1,
377
- invalid: !valid,
378
- pristine: !state.dirty,
379
- reset,
380
- setValidators,
381
- setValue,
382
- someErrors: someErrors$1,
383
- touch,
384
- unfocused: !state.focused,
385
- untouched: !state.touched,
386
- valid,
387
- wrong: state.touched && !valid
388
- };
389
- }
390
- function useReactControl(options, validators) {
391
- return useControl(options, validators);
392
- }
393
- function useFormControl(options, validators) {
394
- return useControl(options, validators);
395
- }
396
- function useInputControl(options, validators) {
397
- return useControl(options, validators);
312
+ function useControl(controlOptions, controlValidators) {
313
+ const { value, touched, validators } = createFormControlOptions(controlOptions, controlValidators);
314
+ const [state, setState] = useState({
315
+ dirty: false,
316
+ disabled: false,
317
+ focused: false,
318
+ touched: !!touched,
319
+ validators,
320
+ value
321
+ });
322
+ const [errors, setErrors] = useState([]);
323
+ const initialValue = useRef(value);
324
+ const elementRef = useRef(null);
325
+ useEffect(() => {
326
+ setErrors(state.validators
327
+ ? controlIsValid({
328
+ value: state.value,
329
+ validators: state.validators
330
+ })
331
+ : []);
332
+ }, [state.value, state.validators]);
333
+ const focus = useCallback(() => {
334
+ setState((state) => ({ ...state, focused: true }));
335
+ }, []);
336
+ const blur = useCallback(() => {
337
+ setState((state) => ({ ...state, focused: false, touched: true }));
338
+ }, []);
339
+ const disable = useCallback(() => {
340
+ setState((state) => ({ ...state, disabled: true }));
341
+ }, []);
342
+ const enable = useCallback(() => {
343
+ setState((state) => ({ ...state, disabled: false }));
344
+ }, []);
345
+ const touch = useCallback(() => {
346
+ setState((state) => ({ ...state, touched: true }));
347
+ }, []);
348
+ const setValue = useCallback((value) => {
349
+ setState((state) => ({ ...state, dirty: true, value }));
350
+ }, []);
351
+ const setValidators = useCallback((validators) => {
352
+ setState((state) => ({ ...state, validators }));
353
+ }, []);
354
+ const reset = useCallback(() => {
355
+ setState((state) => ({
356
+ ...state,
357
+ dirty: false,
358
+ value: initialValue.current,
359
+ touched: false
360
+ }));
361
+ }, []);
362
+ function hasError$1(key) {
363
+ return hasError(errors, key);
364
+ }
365
+ function someErrors$1(keys) {
366
+ return someErrors(errors, keys);
367
+ }
368
+ const valid = errors.length === 0;
369
+ return {
370
+ ...state,
371
+ blur,
372
+ disable,
373
+ elementRef,
374
+ enable,
375
+ enabled: !state.disabled,
376
+ error: errors[0],
377
+ errors,
378
+ focus,
379
+ hasError: hasError$1,
380
+ invalid: !valid,
381
+ pristine: !state.dirty,
382
+ reset,
383
+ setValidators,
384
+ setValue,
385
+ someErrors: someErrors$1,
386
+ touch,
387
+ unfocused: !state.focused,
388
+ untouched: !state.touched,
389
+ valid,
390
+ wrong: state.touched && !valid
391
+ };
392
+ }
393
+ function useReactControl(options, validators) {
394
+ return useControl(options, validators);
395
+ }
396
+ function useFormControl(options, validators) {
397
+ return useControl(options, validators);
398
+ }
399
+ function useInputControl(options, validators) {
400
+ return useControl(options, validators);
398
401
  }
399
402
 
400
- function useFormGroup(options, groupValidators) {
401
- const groupOptions = createFormGroupOptions(options, groupValidators);
402
- const [validators, setValidators] = useState(groupOptions.validators);
403
- const { controls } = groupOptions;
404
- const errors = validators ? groupIsValid({ controls, validators }) : [];
405
- const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
406
- const value = controlsToValue(controls);
407
- const dirty = controlsPartialChecked(controls, 'dirty');
408
- const dirtyAll = controlsAllChecked(controls, 'dirty');
409
- const touched = controlsPartialChecked(controls, 'touched');
410
- const touchedAll = controlsAllChecked(controls, 'touched');
411
- function reset() {
412
- Object.values(controls).forEach((control) => {
413
- control.reset();
414
- });
415
- }
416
- return {
417
- controls,
418
- dirty,
419
- dirtyAll,
420
- error: errors[0],
421
- errors,
422
- invalid: !valid,
423
- pristine: !dirty,
424
- pristineAll: !dirtyAll,
425
- reset,
426
- setValidators,
427
- touched,
428
- touchedAll,
429
- untouched: !touched,
430
- untouchedAll: !touchedAll,
431
- valid,
432
- value,
433
- wrong: touched && !valid
434
- };
403
+ function useFormGroup(options, groupValidators) {
404
+ const groupOptions = createFormGroupOptions(options, groupValidators);
405
+ const [validators, setValidators] = useState(groupOptions.validators);
406
+ const { controls } = groupOptions;
407
+ const errors = validators ? groupIsValid({ controls, validators }) : [];
408
+ const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
409
+ const value = controlsToValue(controls);
410
+ const dirty = controlsPartialChecked(controls, 'dirty');
411
+ const dirtyAll = controlsAllChecked(controls, 'dirty');
412
+ const touched = controlsPartialChecked(controls, 'touched');
413
+ const touchedAll = controlsAllChecked(controls, 'touched');
414
+ function reset() {
415
+ Object.values(controls).forEach((control) => {
416
+ control.reset();
417
+ });
418
+ }
419
+ return {
420
+ controls,
421
+ dirty,
422
+ dirtyAll,
423
+ error: errors[0],
424
+ errors,
425
+ invalid: !valid,
426
+ pristine: !dirty,
427
+ pristineAll: !dirtyAll,
428
+ reset,
429
+ setValidators,
430
+ touched,
431
+ touchedAll,
432
+ untouched: !touched,
433
+ untouchedAll: !touchedAll,
434
+ valid,
435
+ value,
436
+ wrong: touched && !valid
437
+ };
435
438
  }
436
439
 
437
440
  export { formArrayControl, formArrayGroup, formArrayList, inputArrayControl, reactArrayControl, useFormArray, useFormControl, useFormGroup, useInputControl, useReactControl };