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