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