@rolster/react-forms 18.2.10 → 18.4.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 +300 -226
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +292 -214
- package/dist/es/index.js.map +1 -1
- package/dist/esm/form-array/form-array-control.hook.d.ts +24 -20
- package/dist/esm/form-array/form-array-control.hook.js +34 -38
- package/dist/esm/form-array/form-array-control.hook.js.map +1 -1
- package/dist/esm/form-array/form-array-group.hook.d.ts +14 -13
- package/dist/esm/form-array/form-array-group.hook.js +36 -15
- 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 +62 -62
- 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 +45 -54
- 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 +21 -21
- 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 +18 -18
- 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) => control.disabled ? value : value && parseBoolean(control[key]), true);
|
|
74
|
+
};
|
|
75
|
+
const controlsPartialChecked = (controls, key) => {
|
|
76
|
+
return Object.values(controls).reduce((value, control) => control.disabled ? value : 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) {
|
|
@@ -32,17 +134,7 @@ class RolsterArrayControl {
|
|
|
32
134
|
}
|
|
33
135
|
blur() {
|
|
34
136
|
if (this.focused) {
|
|
35
|
-
this.update({ focused: false });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
touch() {
|
|
39
|
-
if (!this.touched) {
|
|
40
|
-
this.update({ touched: true });
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
untouch() {
|
|
44
|
-
if (this.touched) {
|
|
45
|
-
this.update({ touched: false });
|
|
137
|
+
this.update({ focused: false, touched: true });
|
|
46
138
|
}
|
|
47
139
|
}
|
|
48
140
|
disable() {
|
|
@@ -61,37 +153,43 @@ class RolsterArrayControl {
|
|
|
61
153
|
setValidators(validators) {
|
|
62
154
|
this.update({ validators });
|
|
63
155
|
}
|
|
156
|
+
subscribe(listener) {
|
|
157
|
+
this.subscriber = listener;
|
|
158
|
+
}
|
|
64
159
|
reset() {
|
|
65
|
-
this.update({ state: this.initialState });
|
|
160
|
+
this.update({ state: this.initialState, dirty: false, touched: false });
|
|
66
161
|
}
|
|
67
162
|
update(changes) {
|
|
68
|
-
this.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
163
|
+
if (this.subscriber) {
|
|
164
|
+
this.subscriber({
|
|
165
|
+
...this,
|
|
166
|
+
...changes,
|
|
167
|
+
initialState: this.initialState
|
|
168
|
+
});
|
|
169
|
+
}
|
|
72
170
|
}
|
|
73
171
|
}
|
|
74
|
-
function
|
|
75
|
-
const
|
|
172
|
+
function useArrayControl(options, validators) {
|
|
173
|
+
const controlOptions = createFormControlOptions(options, validators);
|
|
76
174
|
return new RolsterArrayControl({
|
|
77
|
-
...
|
|
175
|
+
...controlOptions,
|
|
78
176
|
uuid: v4(),
|
|
79
|
-
initialState:
|
|
177
|
+
initialState: controlOptions.state
|
|
80
178
|
});
|
|
81
179
|
}
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
180
|
+
function useReactArrayControl(options, validators) {
|
|
181
|
+
return useArrayControl(options, validators);
|
|
182
|
+
}
|
|
183
|
+
function useFormArrayControl(options, validators) {
|
|
184
|
+
return useArrayControl(options, validators);
|
|
185
|
+
}
|
|
186
|
+
function useInputArrayControl(options, validators) {
|
|
187
|
+
return useArrayControl(options, validators);
|
|
89
188
|
}
|
|
90
189
|
|
|
91
190
|
class RolsterArrayGroup {
|
|
92
|
-
constructor(
|
|
93
|
-
const { controls, resource, uuid, validators } =
|
|
94
|
-
Object.values(controls).forEach((control) => (control.group = this));
|
|
191
|
+
constructor(options) {
|
|
192
|
+
const { controls, resource, uuid, validators } = options;
|
|
95
193
|
this.uuid = uuid;
|
|
96
194
|
this.controls = controls;
|
|
97
195
|
this.validators = validators;
|
|
@@ -101,270 +199,250 @@ class RolsterArrayGroup {
|
|
|
101
199
|
this.valid =
|
|
102
200
|
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
103
201
|
this.invalid = !this.valid;
|
|
104
|
-
this.touched = controlsPartialChecked(controls, 'touched');
|
|
105
|
-
this.toucheds = controlsAllChecked(controls, 'touched');
|
|
106
202
|
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
107
|
-
this.
|
|
203
|
+
this.dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
204
|
+
this.touched = controlsPartialChecked(controls, 'touched');
|
|
205
|
+
this.touchedAll = controlsAllChecked(controls, 'touched');
|
|
108
206
|
this.untouched = !this.touched;
|
|
109
|
-
this.
|
|
207
|
+
this.untouchedAll = !this.touchedAll;
|
|
110
208
|
this.pristine = !this.dirty;
|
|
111
|
-
this.
|
|
209
|
+
this.pristineAll = !this.dirtyAll;
|
|
112
210
|
this.wrong = this.touched && this.invalid;
|
|
113
211
|
this.state = controlsToState(controls);
|
|
114
|
-
|
|
212
|
+
const subscriber = (options) => {
|
|
213
|
+
this.update({
|
|
214
|
+
controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
|
|
215
|
+
controls[key] =
|
|
216
|
+
control.uuid === options.uuid
|
|
217
|
+
? new RolsterArrayControl(options)
|
|
218
|
+
: control;
|
|
219
|
+
return controls;
|
|
220
|
+
}, {})
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
Object.values(controls).forEach((control) => {
|
|
224
|
+
control.subscribe(subscriber);
|
|
225
|
+
});
|
|
115
226
|
}
|
|
116
227
|
setValidators(validators) {
|
|
117
|
-
this.
|
|
228
|
+
this.update({ validators });
|
|
229
|
+
}
|
|
230
|
+
subscribe(listener) {
|
|
231
|
+
this.subscriber = listener;
|
|
232
|
+
}
|
|
233
|
+
update(changes) {
|
|
234
|
+
if (this.subscriber) {
|
|
235
|
+
this.subscriber({ ...this, ...changes });
|
|
236
|
+
}
|
|
118
237
|
}
|
|
119
238
|
}
|
|
120
|
-
function useFormArrayGroup(
|
|
121
|
-
const
|
|
122
|
-
return new RolsterArrayGroup({ ...
|
|
239
|
+
function useFormArrayGroup(options, validators) {
|
|
240
|
+
const groupOptions = createFormGroupOptions(options, validators);
|
|
241
|
+
return new RolsterArrayGroup({ ...groupOptions, uuid: v4() });
|
|
123
242
|
}
|
|
124
243
|
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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 || []);
|
|
244
|
+
function useFormArray(options, arrayValidators) {
|
|
245
|
+
const arrayOptions = createFormArrayOptions(options, arrayValidators);
|
|
246
|
+
const { validators } = arrayOptions;
|
|
247
|
+
const groups = arrayOptions.groups || [];
|
|
248
|
+
const [arrayState, setArrayState] = useState({
|
|
249
|
+
controls: groups.map(({ controls }) => controls),
|
|
250
|
+
disabled: false,
|
|
251
|
+
groups,
|
|
252
|
+
state: groups.map(({ controls }) => controlsToState(controls)),
|
|
253
|
+
validators
|
|
254
|
+
});
|
|
255
|
+
const currentState = useRef(groups);
|
|
147
256
|
useEffect(() => {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
257
|
+
const subscriber = (options) => {
|
|
258
|
+
setArrayState((state) => ({
|
|
259
|
+
...state,
|
|
260
|
+
groups: arrayState.groups.map((group) => group.uuid === options.uuid
|
|
261
|
+
? new RolsterArrayGroup(options)
|
|
262
|
+
: group)
|
|
263
|
+
}));
|
|
264
|
+
};
|
|
265
|
+
arrayState.groups.forEach((group) => {
|
|
266
|
+
group.subscribe(subscriber);
|
|
267
|
+
});
|
|
268
|
+
}, [arrayState]);
|
|
151
269
|
const errors = validators ? arrayIsValid({ groups, validators }) : [];
|
|
152
270
|
const error = errors[0];
|
|
153
271
|
const valid = errors.length === 0 && groupAllChecked(groups, 'valid');
|
|
154
|
-
const touched = groupPartialChecked(groups, 'touched');
|
|
155
|
-
const toucheds = groupAllChecked(groups, 'touched');
|
|
156
|
-
const dirties = groupAllChecked(groups, 'dirty');
|
|
157
272
|
const dirty = groupPartialChecked(groups, 'dirty');
|
|
273
|
+
const dirtyAll = groupAllChecked(groups, 'dirty');
|
|
274
|
+
const touched = groupPartialChecked(groups, 'touched');
|
|
275
|
+
const touchedAll = groupAllChecked(groups, 'touched');
|
|
276
|
+
function disable() {
|
|
277
|
+
setArrayState((state) => ({ ...state, disabled: true }));
|
|
278
|
+
}
|
|
279
|
+
function enable() {
|
|
280
|
+
setArrayState((state) => ({ ...state, disabled: false }));
|
|
281
|
+
}
|
|
282
|
+
function setGroups(groups) {
|
|
283
|
+
setArrayState((currentState) => ({
|
|
284
|
+
...currentState,
|
|
285
|
+
groups,
|
|
286
|
+
controls: groups.map(({ controls }) => controls),
|
|
287
|
+
state: groups.map(({ controls }) => controlsToState(controls))
|
|
288
|
+
}));
|
|
289
|
+
}
|
|
158
290
|
function push(group) {
|
|
159
|
-
setGroups([...groups, group]);
|
|
291
|
+
setGroups([...arrayState.groups, group]);
|
|
160
292
|
}
|
|
161
|
-
function merge(
|
|
162
|
-
setGroups([...groups, ...
|
|
293
|
+
function merge(groups) {
|
|
294
|
+
setGroups([...arrayState.groups, ...groups]);
|
|
163
295
|
}
|
|
164
296
|
function set(groups) {
|
|
165
297
|
setGroups(groups);
|
|
166
298
|
}
|
|
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));
|
|
299
|
+
function remove({ uuid }) {
|
|
300
|
+
setGroups(arrayState.groups.filter((group) => group.uuid !== uuid));
|
|
180
301
|
}
|
|
181
302
|
function reset() {
|
|
182
|
-
setGroups(currentState
|
|
303
|
+
setGroups(currentState.current);
|
|
183
304
|
}
|
|
184
|
-
|
|
185
|
-
|
|
305
|
+
function setValidators(validators) {
|
|
306
|
+
setArrayState((state) => ({ ...state, validators }));
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
...arrayState,
|
|
186
310
|
dirty,
|
|
187
|
-
|
|
311
|
+
dirtyAll,
|
|
312
|
+
disable,
|
|
313
|
+
enable,
|
|
314
|
+
enabled: !arrayState.disabled,
|
|
188
315
|
error,
|
|
189
316
|
errors,
|
|
190
|
-
groups,
|
|
191
317
|
invalid: !valid,
|
|
192
318
|
merge,
|
|
193
319
|
pristine: !dirty,
|
|
194
|
-
|
|
320
|
+
pristineAll: !dirtyAll,
|
|
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
|
+
touchedAll,
|
|
205
328
|
untouched: !touched,
|
|
206
|
-
|
|
329
|
+
untouchedAll: !touchedAll,
|
|
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, touched: true }));
|
|
239
354
|
}
|
|
240
355
|
function disable() {
|
|
241
|
-
|
|
356
|
+
setControlState((state) => ({ ...state, disabled: true }));
|
|
242
357
|
}
|
|
243
358
|
function enable() {
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
function touch() {
|
|
247
|
-
setTouched(true);
|
|
248
|
-
}
|
|
249
|
-
function untouch() {
|
|
250
|
-
setTouched(false);
|
|
359
|
+
setControlState((state) => ({ ...state, disabled: false }));
|
|
251
360
|
}
|
|
252
361
|
function setState(state) {
|
|
253
|
-
|
|
254
|
-
|
|
362
|
+
setControlState((currentState) => ({
|
|
363
|
+
...currentState,
|
|
364
|
+
dirty: true,
|
|
365
|
+
state
|
|
366
|
+
}));
|
|
367
|
+
}
|
|
368
|
+
function setValidators(validators) {
|
|
369
|
+
setControlState((state) => ({ ...state, validators }));
|
|
255
370
|
}
|
|
256
371
|
function reset() {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
372
|
+
setControlState((currentState) => ({
|
|
373
|
+
...currentState,
|
|
374
|
+
dirty: false,
|
|
375
|
+
state: initialState.current,
|
|
376
|
+
touched: false
|
|
377
|
+
}));
|
|
260
378
|
}
|
|
261
379
|
return {
|
|
380
|
+
...controlState,
|
|
262
381
|
blur,
|
|
263
|
-
dirty,
|
|
264
382
|
disable,
|
|
265
|
-
disabled,
|
|
266
383
|
elementRef,
|
|
267
384
|
enable,
|
|
268
|
-
enabled: !disabled,
|
|
269
|
-
error,
|
|
385
|
+
enabled: !controlState.disabled,
|
|
386
|
+
error: errors[0],
|
|
270
387
|
errors,
|
|
271
388
|
focus,
|
|
272
|
-
focused,
|
|
273
389
|
invalid: !valid,
|
|
274
|
-
pristine: !dirty,
|
|
390
|
+
pristine: !controlState.dirty,
|
|
275
391
|
reset,
|
|
276
392
|
setState,
|
|
277
393
|
setValidators,
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
touched,
|
|
281
|
-
unfocused: !focused,
|
|
282
|
-
untouch,
|
|
283
|
-
untouched: !touched,
|
|
394
|
+
unfocused: !controlState.focused,
|
|
395
|
+
untouched: !controlState.touched,
|
|
284
396
|
valid,
|
|
285
|
-
|
|
286
|
-
wrong: touched && !valid
|
|
397
|
+
wrong: controlState.touched && !valid
|
|
287
398
|
};
|
|
288
399
|
}
|
|
289
|
-
function useReactControl(
|
|
290
|
-
return useControl(
|
|
400
|
+
function useReactControl(options, validators) {
|
|
401
|
+
return useControl(options, validators);
|
|
291
402
|
}
|
|
292
|
-
function useFormControl(
|
|
293
|
-
return useControl(
|
|
403
|
+
function useFormControl(options, validators) {
|
|
404
|
+
return useControl(options, validators);
|
|
294
405
|
}
|
|
295
|
-
function useInputControl(
|
|
296
|
-
return useControl(
|
|
406
|
+
function useInputControl(options, validators) {
|
|
407
|
+
return useControl(options, validators);
|
|
297
408
|
}
|
|
298
409
|
|
|
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))();
|
|
410
|
+
function useFormGroup(options, groupValidators) {
|
|
411
|
+
const groupOptions = createFormGroupOptions(options, groupValidators);
|
|
412
|
+
const [validators, setValidators] = useState(groupOptions.validators);
|
|
413
|
+
const { controls } = groupOptions;
|
|
414
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
415
|
+
const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
416
|
+
const state = controlsToState(controls);
|
|
417
|
+
const dirty = controlsPartialChecked(controls, 'dirty');
|
|
418
|
+
const dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
419
|
+
const touched = controlsPartialChecked(controls, 'touched');
|
|
420
|
+
const touchedAll = controlsAllChecked(controls, 'touched');
|
|
311
421
|
function reset() {
|
|
312
|
-
Object.values(controls).forEach((control) =>
|
|
422
|
+
Object.values(controls).forEach((control) => {
|
|
423
|
+
control.reset();
|
|
424
|
+
});
|
|
313
425
|
}
|
|
314
426
|
return {
|
|
315
427
|
controls,
|
|
316
428
|
dirty,
|
|
317
|
-
|
|
429
|
+
dirtyAll,
|
|
318
430
|
error: errors[0],
|
|
319
431
|
errors,
|
|
320
432
|
invalid: !valid,
|
|
321
433
|
pristine: !dirty,
|
|
322
|
-
|
|
434
|
+
pristineAll: !dirtyAll,
|
|
323
435
|
reset,
|
|
324
|
-
state,
|
|
325
436
|
setValidators,
|
|
437
|
+
state,
|
|
326
438
|
touched,
|
|
327
|
-
|
|
439
|
+
touchedAll,
|
|
328
440
|
untouched: !touched,
|
|
329
|
-
|
|
441
|
+
untouchedAll: !touchedAll,
|
|
330
442
|
valid,
|
|
331
|
-
value,
|
|
332
443
|
wrong: touched && !valid
|
|
333
444
|
};
|
|
334
445
|
}
|
|
335
446
|
|
|
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 };
|
|
447
|
+
export { useFormArray, useFormArrayControl, useFormArrayGroup, useFormControl, useFormGroup, useInputArrayControl, useInputControl, useReactArrayControl, useReactControl };
|
|
370
448
|
//# sourceMappingURL=index.js.map
|