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