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