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