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