@rolster/react-forms 18.11.4 → 18.12.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 +253 -192
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +254 -193
- package/dist/es/index.js.map +1 -1
- package/dist/esm/form-array/form-array-control.d.ts +24 -22
- package/dist/esm/form-array/form-array-control.js +43 -36
- package/dist/esm/form-array/form-array-control.js.map +1 -1
- package/dist/esm/form-array/form-array-group.d.ts +4 -30
- package/dist/esm/form-array/form-array-group.js +84 -29
- package/dist/esm/form-array/form-array-group.js.map +1 -1
- package/dist/esm/form-array/form-array-list.d.ts +3 -18
- package/dist/esm/form-array/form-array-list.js +35 -35
- package/dist/esm/form-array/form-array-list.js.map +1 -1
- package/dist/esm/form-array/form-array.js +37 -37
- package/dist/esm/form-array/form-array.js.map +1 -1
- package/dist/esm/form-control.js +1 -10
- package/dist/esm/form-control.js.map +1 -1
- package/dist/esm/form-group.js +50 -36
- package/dist/esm/form-group.js.map +1 -1
- package/dist/esm/types.d.ts +6 -6
- package/dist/esm/utilities.d.ts +2 -0
- package/dist/esm/utilities.js +9 -0
- package/dist/esm/utilities.js.map +1 -0
- package/package.json +69 -70
package/dist/es/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { createFormControlOptions, createFormGroupOptions, createFormArrayOptions } from '@rolster/forms/arguments';
|
|
2
|
-
import { controlIsValid, hasError, someErrors, groupIsValid, controlsAllChecked, controlsPartialChecked,
|
|
2
|
+
import { controlIsValid, hasError, someErrors, groupIsValid, controlsToValue, controlsAllChecked, controlsPartialChecked, arrayIsValid, groupAllChecked, reduceControlsToArray } from '@rolster/forms/helpers';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import { useRef, useState, useEffect, useCallback } from 'react';
|
|
5
5
|
|
|
6
|
-
class
|
|
6
|
+
class RolsterArrayControl {
|
|
7
7
|
constructor(options) {
|
|
8
|
-
this.initialValue = options.initialValue;
|
|
9
8
|
this.uuid = options.uuid;
|
|
9
|
+
this.value = options.value;
|
|
10
|
+
this.initialValue = options.initialValue;
|
|
10
11
|
this.focused = !!options.focused;
|
|
11
12
|
this.unfocused = !this.focused;
|
|
12
13
|
this.touched = !!options.touched;
|
|
@@ -15,36 +16,42 @@ class RolsterReactArrayControl {
|
|
|
15
16
|
this.pristine = !this.dirty;
|
|
16
17
|
this.disabled = !!options.disabled;
|
|
17
18
|
this.enabled = !this.disabled;
|
|
18
|
-
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.errors =
|
|
19
|
+
this.valid = this.isValid(options.errors);
|
|
20
|
+
this.invalid = !this.valid;
|
|
21
|
+
this.wrong = this.touched && this.invalid;
|
|
22
|
+
this.errors = options.errors;
|
|
22
23
|
this.error = this.errors[0];
|
|
24
|
+
this.validators = options.validators;
|
|
23
25
|
}
|
|
24
26
|
focus() {
|
|
25
|
-
this.unfocused && this.refresh({ focused: true });
|
|
27
|
+
this.unfocused && this.refresh('focused', { focused: true });
|
|
26
28
|
}
|
|
27
29
|
blur() {
|
|
28
|
-
this.focused && this.refresh({ focused: false, touched: true });
|
|
30
|
+
this.focused && this.refresh('focused', { focused: false, touched: true });
|
|
29
31
|
}
|
|
30
32
|
disable() {
|
|
31
|
-
this.enabled && this.refresh({ disabled: true });
|
|
33
|
+
this.enabled && this.refresh('disabled', { disabled: true });
|
|
32
34
|
}
|
|
33
35
|
enable() {
|
|
34
|
-
this.disabled && this.refresh({ disabled: false });
|
|
36
|
+
this.disabled && this.refresh('disabled', { disabled: false });
|
|
35
37
|
}
|
|
36
38
|
touch() {
|
|
37
|
-
this.untouched && this.refresh({ touched: true });
|
|
39
|
+
this.untouched && this.refresh('touched', { touched: true });
|
|
38
40
|
}
|
|
39
41
|
setInitialValue(value) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
const { validators } = this;
|
|
43
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
44
|
+
this.refresh('value', { dirty: false, errors, initialValue: value, value });
|
|
42
45
|
}
|
|
43
46
|
setValue(value) {
|
|
44
|
-
|
|
47
|
+
const { validators } = this;
|
|
48
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
49
|
+
this.refresh('value', { dirty: true, errors, value });
|
|
45
50
|
}
|
|
46
51
|
setValidators(validators) {
|
|
47
|
-
|
|
52
|
+
const { value } = this;
|
|
53
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
54
|
+
this.refresh('validators', { errors, validators });
|
|
48
55
|
}
|
|
49
56
|
subscribe(subscriber) {
|
|
50
57
|
this.subscriber = subscriber;
|
|
@@ -56,37 +63,37 @@ class RolsterReactArrayControl {
|
|
|
56
63
|
return someErrors(this.errors, keys);
|
|
57
64
|
}
|
|
58
65
|
reset() {
|
|
59
|
-
this
|
|
66
|
+
const { initialValue: value, validators } = this;
|
|
67
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
68
|
+
this.refresh('reset', {
|
|
60
69
|
dirty: false,
|
|
70
|
+
errors,
|
|
61
71
|
touched: false,
|
|
62
|
-
value
|
|
72
|
+
value
|
|
63
73
|
});
|
|
64
74
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
isValid(errors) {
|
|
76
|
+
return errors.length === 0;
|
|
77
|
+
}
|
|
78
|
+
builder(options) {
|
|
79
|
+
return new RolsterArrayControl({ ...this, ...options });
|
|
80
|
+
}
|
|
81
|
+
refresh(action, options) {
|
|
82
|
+
this.subscriber && this.subscriber(action, this.builder(options));
|
|
72
83
|
}
|
|
73
84
|
}
|
|
74
|
-
class
|
|
85
|
+
class ReactRolsterArrayControl extends RolsterArrayControl {
|
|
75
86
|
constructor(options) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.wrong = this.touched && this.invalid;
|
|
80
|
-
}
|
|
81
|
-
clone(options) {
|
|
82
|
-
return new RolsterArrayControl(options);
|
|
87
|
+
const { value, validators } = options;
|
|
88
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
89
|
+
super({ ...options, errors });
|
|
83
90
|
}
|
|
84
91
|
}
|
|
85
92
|
function rolsterArrayControl(options, validators) {
|
|
86
|
-
const
|
|
87
|
-
return new
|
|
88
|
-
...
|
|
89
|
-
initialValue:
|
|
93
|
+
const _options = createFormControlOptions(options, validators);
|
|
94
|
+
return new ReactRolsterArrayControl({
|
|
95
|
+
..._options,
|
|
96
|
+
initialValue: _options.value,
|
|
90
97
|
uuid: v4()
|
|
91
98
|
});
|
|
92
99
|
}
|
|
@@ -100,49 +107,112 @@ function inputArrayControl(options, validators) {
|
|
|
100
107
|
return rolsterArrayControl(options, validators);
|
|
101
108
|
}
|
|
102
109
|
|
|
103
|
-
|
|
110
|
+
function replaceControl(controls, control) {
|
|
111
|
+
return Object.entries(controls).reduce((controls, [key, _control]) => {
|
|
112
|
+
if (_control.uuid === control.uuid) {
|
|
113
|
+
controls[key] = control;
|
|
114
|
+
}
|
|
115
|
+
return controls;
|
|
116
|
+
}, controls);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function refactorForValid$2(controls, validators) {
|
|
120
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
121
|
+
return {
|
|
122
|
+
errors,
|
|
123
|
+
valid: errors.length === 0 && controlsAllChecked(controls, 'valid')
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function refactorForControls$1(group, controls, action) {
|
|
127
|
+
switch (action) {
|
|
128
|
+
case 'focused':
|
|
129
|
+
case 'touched':
|
|
130
|
+
return {
|
|
131
|
+
touched: controlsPartialChecked(controls, 'touched'),
|
|
132
|
+
toucheds: controlsAllChecked(controls, 'touched')
|
|
133
|
+
};
|
|
134
|
+
case 'validators':
|
|
135
|
+
return refactorForValid$2(controls, group.validators);
|
|
136
|
+
case 'reset':
|
|
137
|
+
return {
|
|
138
|
+
...refactorForValid$2(controls, group.validators),
|
|
139
|
+
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
140
|
+
dirties: controlsAllChecked(controls, 'dirty'),
|
|
141
|
+
touched: controlsPartialChecked(controls, 'touched'),
|
|
142
|
+
toucheds: controlsAllChecked(controls, 'touched'),
|
|
143
|
+
value: controlsToValue(controls)
|
|
144
|
+
};
|
|
145
|
+
case 'list':
|
|
146
|
+
case 'value':
|
|
147
|
+
return {
|
|
148
|
+
...refactorForValid$2(controls, group.validators),
|
|
149
|
+
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
150
|
+
dirties: controlsAllChecked(controls, 'dirty'),
|
|
151
|
+
value: controlsToValue(controls)
|
|
152
|
+
};
|
|
153
|
+
default:
|
|
154
|
+
return {};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
class BaseArrayGroup {
|
|
104
158
|
constructor(options) {
|
|
105
|
-
|
|
106
|
-
this.
|
|
107
|
-
this.
|
|
108
|
-
this.
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
111
|
-
this.error = this.errors[0];
|
|
112
|
-
this.valid =
|
|
113
|
-
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
114
|
-
this.invalid = !this.valid;
|
|
115
|
-
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
116
|
-
this.dirties = controlsAllChecked(controls, 'dirty');
|
|
117
|
-
this.touched = controlsPartialChecked(controls, 'touched');
|
|
118
|
-
this.toucheds = controlsAllChecked(controls, 'touched');
|
|
159
|
+
this.uuid = options.uuid;
|
|
160
|
+
this.controls = options.controls;
|
|
161
|
+
this.value = options.value;
|
|
162
|
+
this.resource = options.resource;
|
|
163
|
+
this.dirty = !!options.dirty;
|
|
164
|
+
this.dirties = !!options.dirties;
|
|
119
165
|
this.pristine = !this.dirty;
|
|
120
166
|
this.pristines = !this.dirties;
|
|
167
|
+
this.touched = !!options.touched;
|
|
168
|
+
this.toucheds = !!options.toucheds;
|
|
121
169
|
this.untouched = !this.touched;
|
|
122
170
|
this.untoucheds = !this.toucheds;
|
|
171
|
+
this.valid = !!options.valid;
|
|
172
|
+
this.invalid = !this.valid;
|
|
123
173
|
this.wrong = this.touched && this.invalid;
|
|
124
|
-
this.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
174
|
+
this.errors = options.errors;
|
|
175
|
+
this.error = this.errors[0];
|
|
176
|
+
this.validators = options.validators;
|
|
177
|
+
const subscriber = (action, control) => {
|
|
178
|
+
const controls = replaceControl(this.controls, control);
|
|
179
|
+
this.refresh(action, {
|
|
180
|
+
...refactorForControls$1(this, controls, action),
|
|
181
|
+
controls
|
|
132
182
|
});
|
|
133
183
|
};
|
|
134
|
-
Object.values(controls).forEach((control) => {
|
|
184
|
+
Object.values(this.controls).forEach((control) => {
|
|
135
185
|
control.subscribe(subscriber);
|
|
136
186
|
});
|
|
137
187
|
}
|
|
138
|
-
setValidators(validators) {
|
|
139
|
-
this.refresh({ validators });
|
|
140
|
-
}
|
|
141
188
|
subscribe(subscriber) {
|
|
142
189
|
this.subscriber = subscriber;
|
|
143
190
|
}
|
|
144
|
-
|
|
145
|
-
this.
|
|
191
|
+
setValidators(validators) {
|
|
192
|
+
this.refresh('validators', {
|
|
193
|
+
...refactorForValid$2(this.controls, validators),
|
|
194
|
+
validators
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
refresh(action, options) {
|
|
198
|
+
this.subscriber &&
|
|
199
|
+
this.subscriber(action, new BaseArrayGroup({ ...this, ...options }));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
class RolsterArrayGroup extends BaseArrayGroup {
|
|
203
|
+
constructor(options) {
|
|
204
|
+
const { controls, validators } = options;
|
|
205
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
206
|
+
super({
|
|
207
|
+
...options,
|
|
208
|
+
errors,
|
|
209
|
+
value: controlsToValue(controls),
|
|
210
|
+
dirties: controlsAllChecked(controls, 'dirty'),
|
|
211
|
+
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
212
|
+
touched: controlsPartialChecked(controls, 'touched'),
|
|
213
|
+
toucheds: controlsAllChecked(controls, 'touched'),
|
|
214
|
+
valid: errors.length === 0 && controlsAllChecked(controls, 'valid')
|
|
215
|
+
});
|
|
146
216
|
}
|
|
147
217
|
}
|
|
148
218
|
function formArrayGroup(options, validators) {
|
|
@@ -152,111 +222,102 @@ function formArrayGroup(options, validators) {
|
|
|
152
222
|
});
|
|
153
223
|
}
|
|
154
224
|
|
|
155
|
-
class RolsterArrayList extends
|
|
225
|
+
class RolsterArrayList extends RolsterArrayControl {
|
|
156
226
|
constructor(options) {
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
227
|
+
const { controls, valueToControls, validators } = options;
|
|
228
|
+
const value = controls.map((controls) => controlsToValue(controls));
|
|
229
|
+
const errors = validators ? controlIsValid({ value, validators }) : [];
|
|
230
|
+
super({ ...options, errors, value });
|
|
231
|
+
this.valueToControls = valueToControls;
|
|
232
|
+
this.controls = controls;
|
|
161
233
|
this.valid =
|
|
162
|
-
|
|
163
|
-
|
|
234
|
+
errors.length === 0 &&
|
|
235
|
+
controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
|
|
164
236
|
this.invalid = !this.valid;
|
|
165
237
|
this.wrong = this.touched && this.invalid;
|
|
166
|
-
|
|
167
|
-
this.
|
|
238
|
+
controls.forEach((reactControls) => {
|
|
239
|
+
this._subscribe(reactControls);
|
|
168
240
|
});
|
|
169
241
|
}
|
|
170
242
|
setValue(value) {
|
|
171
|
-
this.refresh({
|
|
243
|
+
this.refresh('list', {
|
|
172
244
|
controls: value.map((value) => this.valueToControls(value))
|
|
173
245
|
});
|
|
174
246
|
}
|
|
175
|
-
clone(options) {
|
|
176
|
-
return new RolsterArrayList(options);
|
|
177
|
-
}
|
|
178
247
|
push(controls) {
|
|
179
|
-
this.refresh({
|
|
248
|
+
this.refresh('list', {
|
|
180
249
|
controls: [...this.controls, controls]
|
|
181
250
|
});
|
|
182
251
|
}
|
|
183
252
|
remove(controls) {
|
|
184
|
-
this.refresh({
|
|
185
|
-
controls: this.controls.filter((
|
|
253
|
+
this.refresh('list', {
|
|
254
|
+
controls: this.controls.filter((_controls) => _controls !== controls)
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
builder(options) {
|
|
258
|
+
return new RolsterArrayList({
|
|
259
|
+
...this,
|
|
260
|
+
...options,
|
|
261
|
+
valueToControls: this.valueToControls
|
|
186
262
|
});
|
|
187
263
|
}
|
|
188
|
-
refresh(
|
|
189
|
-
super.refresh(
|
|
264
|
+
refresh(action, options) {
|
|
265
|
+
super.refresh(action, options);
|
|
190
266
|
}
|
|
191
|
-
|
|
192
|
-
Object.values(
|
|
193
|
-
control.subscribe((
|
|
194
|
-
const
|
|
267
|
+
_subscribe(reactControls) {
|
|
268
|
+
Object.values(reactControls).forEach((control) => {
|
|
269
|
+
control.subscribe((action, _control) => {
|
|
270
|
+
const _reactControls = this.controls.map((_controls) => reactControls !== _controls
|
|
195
271
|
? _controls
|
|
196
|
-
:
|
|
197
|
-
|
|
198
|
-
control.uuid === options.uuid
|
|
199
|
-
? control.clone(options)
|
|
200
|
-
: control;
|
|
201
|
-
return controls;
|
|
202
|
-
}, {}));
|
|
203
|
-
this.refresh({ controls });
|
|
272
|
+
: replaceControl(reactControls, _control));
|
|
273
|
+
this.refresh(action, { controls: _reactControls });
|
|
204
274
|
});
|
|
205
275
|
});
|
|
206
276
|
}
|
|
207
277
|
}
|
|
208
|
-
function formArrayList(
|
|
278
|
+
function formArrayList(options) {
|
|
209
279
|
const value = options?.value || [];
|
|
210
280
|
return new RolsterArrayList({
|
|
211
281
|
...options,
|
|
212
|
-
valueToControls,
|
|
213
|
-
controls: value.map((value) => valueToControls(value)),
|
|
282
|
+
controls: value.map((value) => options.valueToControls(value)),
|
|
214
283
|
initialValue: value,
|
|
215
284
|
uuid: v4()
|
|
216
285
|
});
|
|
217
286
|
}
|
|
218
287
|
|
|
219
|
-
|
|
220
|
-
(
|
|
221
|
-
Language["English"] = "en";
|
|
222
|
-
Language["French"] = "fr";
|
|
223
|
-
Language["Portuguese"] = "pt";
|
|
224
|
-
Language["Spanish"] = "es";
|
|
225
|
-
})(Language || (Language = {}));
|
|
226
|
-
|
|
227
|
-
Language.Spanish;
|
|
228
|
-
let subscribers = [];
|
|
229
|
-
function i18nSubscribe(subscriber) {
|
|
230
|
-
subscribers.push(subscriber);
|
|
231
|
-
return () => {
|
|
232
|
-
subscribers = subscribers.filter((currentSubscriber) => subscriber !== currentSubscriber);
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function errorsInArray(groups, validators) {
|
|
237
|
-
return validators ? arrayIsValid({ groups, validators }) : [];
|
|
238
|
-
}
|
|
239
|
-
function validStateInArray(groups, validators) {
|
|
240
|
-
const errors = errorsInArray(groups, validators);
|
|
288
|
+
function refactorForValid$1(groups, validators) {
|
|
289
|
+
const errors = validators ? arrayIsValid({ groups, validators }) : [];
|
|
241
290
|
return {
|
|
242
291
|
errors,
|
|
243
292
|
valid: errors.length === 0 && groupAllChecked(groups, 'valid')
|
|
244
293
|
};
|
|
245
294
|
}
|
|
246
|
-
function
|
|
295
|
+
function refactorForGroups(groups, validators) {
|
|
247
296
|
return {
|
|
248
|
-
...
|
|
297
|
+
...refactorForValid$1(groups, validators),
|
|
249
298
|
groups,
|
|
250
299
|
controls: groups.map(({ controls }) => controls),
|
|
251
300
|
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
252
301
|
};
|
|
253
302
|
}
|
|
303
|
+
function refactorForControls(state, groups, action) {
|
|
304
|
+
switch (action) {
|
|
305
|
+
case 'validators':
|
|
306
|
+
return refactorForValid$1(groups, state.validators);
|
|
307
|
+
case 'reset':
|
|
308
|
+
case 'list':
|
|
309
|
+
case 'value':
|
|
310
|
+
return refactorForGroups(groups, state.validators);
|
|
311
|
+
default:
|
|
312
|
+
return { groups };
|
|
313
|
+
}
|
|
314
|
+
}
|
|
254
315
|
function useFormArray(options, validators) {
|
|
255
316
|
const _options = createFormArrayOptions(options, validators);
|
|
256
317
|
const groups = _options.groups || [];
|
|
257
318
|
const initialValue = useRef(groups);
|
|
258
319
|
const [state, setState] = useState({
|
|
259
|
-
...
|
|
320
|
+
...refactorForValid$1(groups, _options.validators),
|
|
260
321
|
controls: groups.map(({ controls }) => controls),
|
|
261
322
|
dirty: false,
|
|
262
323
|
dirties: false,
|
|
@@ -268,21 +329,14 @@ function useFormArray(options, validators) {
|
|
|
268
329
|
validators: _options.validators
|
|
269
330
|
});
|
|
270
331
|
useEffect(() => {
|
|
271
|
-
|
|
272
|
-
setState((state) =>
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const subscriber = (options) => {
|
|
280
|
-
setState((state) => ({
|
|
281
|
-
...state,
|
|
282
|
-
...replaceStateInArray(state.groups.map((group) => group.uuid === options.uuid
|
|
283
|
-
? new RolsterArrayGroup(options)
|
|
284
|
-
: group), state.validators)
|
|
285
|
-
}));
|
|
332
|
+
const subscriber = (action, group) => {
|
|
333
|
+
setState((state) => {
|
|
334
|
+
const groups = state.groups.map((_group) => _group.uuid === group.uuid ? group : _group);
|
|
335
|
+
return {
|
|
336
|
+
...state,
|
|
337
|
+
...refactorForControls(state, groups, action)
|
|
338
|
+
};
|
|
339
|
+
});
|
|
286
340
|
};
|
|
287
341
|
state.groups.forEach((group) => {
|
|
288
342
|
group.subscribe(subscriber);
|
|
@@ -297,49 +351,49 @@ function useFormArray(options, validators) {
|
|
|
297
351
|
const setValue = useCallback((groups) => {
|
|
298
352
|
setState((state) => ({
|
|
299
353
|
...state,
|
|
300
|
-
...
|
|
354
|
+
...refactorForGroups(groups, state.validators)
|
|
301
355
|
}));
|
|
302
356
|
}, []);
|
|
303
357
|
const setInitialValue = useCallback((groups) => {
|
|
304
358
|
setState((state) => ({
|
|
305
359
|
...state,
|
|
306
|
-
...
|
|
360
|
+
...refactorForGroups(groups, state.validators)
|
|
307
361
|
}));
|
|
308
362
|
initialValue.current = groups;
|
|
309
363
|
}, []);
|
|
310
364
|
const push = useCallback((group) => {
|
|
311
365
|
setState((state) => ({
|
|
312
366
|
...state,
|
|
313
|
-
...
|
|
367
|
+
...refactorForGroups([...state.groups, group], state.validators)
|
|
314
368
|
}));
|
|
315
369
|
}, []);
|
|
316
370
|
const merge = useCallback((groups) => {
|
|
317
371
|
setState((state) => ({
|
|
318
372
|
...state,
|
|
319
|
-
...
|
|
373
|
+
...refactorForGroups([...state.groups, ...groups], state.validators)
|
|
320
374
|
}));
|
|
321
375
|
}, []);
|
|
322
376
|
const remove = useCallback(({ uuid }) => {
|
|
323
377
|
setState((state) => ({
|
|
324
378
|
...state,
|
|
325
|
-
...
|
|
326
|
-
}));
|
|
327
|
-
}, []);
|
|
328
|
-
const reset = useCallback(() => {
|
|
329
|
-
setState((state) => ({
|
|
330
|
-
...state,
|
|
331
|
-
...replaceStateInArray(initialValue.current, state.validators)
|
|
379
|
+
...refactorForGroups(state.groups.filter((group) => group.uuid !== uuid), state.validators)
|
|
332
380
|
}));
|
|
333
381
|
}, []);
|
|
334
382
|
const setValidators = useCallback((validators) => {
|
|
335
383
|
setState((state) => ({
|
|
336
384
|
...state,
|
|
337
|
-
...
|
|
385
|
+
...refactorForValid$1(state.groups, validators),
|
|
338
386
|
validators
|
|
339
387
|
}));
|
|
340
388
|
}, []);
|
|
341
389
|
const hasError$1 = useCallback((key) => hasError(state.errors, key), [state.errors]);
|
|
342
390
|
const someErrors$1 = useCallback((keys) => someErrors(state.errors, keys), [state.errors]);
|
|
391
|
+
const reset = useCallback(() => {
|
|
392
|
+
setState((state) => ({
|
|
393
|
+
...state,
|
|
394
|
+
...refactorForGroups(initialValue.current, state.validators)
|
|
395
|
+
}));
|
|
396
|
+
}, []);
|
|
343
397
|
return {
|
|
344
398
|
...state,
|
|
345
399
|
disable,
|
|
@@ -380,14 +434,6 @@ function useControl(options, validators) {
|
|
|
380
434
|
validators: _options.validators
|
|
381
435
|
});
|
|
382
436
|
const elementRef = useRef(null);
|
|
383
|
-
useEffect(() => {
|
|
384
|
-
return i18nSubscribe(() => {
|
|
385
|
-
setState((state) => ({
|
|
386
|
-
...state,
|
|
387
|
-
errors: errorsInControl(state.value, state.validators)
|
|
388
|
-
}));
|
|
389
|
-
});
|
|
390
|
-
}, []);
|
|
391
437
|
const focus = useCallback(() => {
|
|
392
438
|
setState((state) => ({ ...state, focused: true }));
|
|
393
439
|
}, []);
|
|
@@ -473,11 +519,8 @@ function useInputControl(options, validators) {
|
|
|
473
519
|
return useControl(options, validators);
|
|
474
520
|
}
|
|
475
521
|
|
|
476
|
-
function
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
function validStateInGroup(controls, validators) {
|
|
480
|
-
const errors = errorsInGroup(controls, validators);
|
|
522
|
+
function refactorForValid(controls, validators) {
|
|
523
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
481
524
|
return {
|
|
482
525
|
errors,
|
|
483
526
|
valid: errors.length === 0 && controlsAllChecked(controls, 'valid')
|
|
@@ -486,8 +529,14 @@ function validStateInGroup(controls, validators) {
|
|
|
486
529
|
function useFormGroup(options, validators) {
|
|
487
530
|
const _options = createFormGroupOptions(options, validators);
|
|
488
531
|
const { controls } = _options;
|
|
532
|
+
const firstEffects = useRef({
|
|
533
|
+
dirty: true,
|
|
534
|
+
disabledFocused: true,
|
|
535
|
+
touched: true,
|
|
536
|
+
value: true
|
|
537
|
+
});
|
|
489
538
|
const [state, setState] = useState({
|
|
490
|
-
...
|
|
539
|
+
...refactorForValid(controls, _options.validators),
|
|
491
540
|
controls,
|
|
492
541
|
dirties: controlsAllChecked(controls, 'dirty'),
|
|
493
542
|
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
@@ -497,50 +546,62 @@ function useFormGroup(options, validators) {
|
|
|
497
546
|
validators: _options.validators
|
|
498
547
|
});
|
|
499
548
|
useEffect(() => {
|
|
500
|
-
|
|
549
|
+
if (!firstEffects.current.value) {
|
|
501
550
|
setState((state) => ({
|
|
502
551
|
...state,
|
|
503
|
-
|
|
552
|
+
...refactorForValid(controls, state.validators),
|
|
553
|
+
controls,
|
|
554
|
+
value: controlsToValue(controls)
|
|
504
555
|
}));
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
...state,
|
|
510
|
-
...validStateInGroup(controls, state.validators),
|
|
511
|
-
controls,
|
|
512
|
-
value: controlsToValue(controls)
|
|
513
|
-
}));
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
firstEffects.current.value = false;
|
|
559
|
+
}
|
|
514
560
|
}, reduceControlsToArray(controls, 'value'));
|
|
515
561
|
useEffect(() => {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
562
|
+
if (!firstEffects.current.disabledFocused) {
|
|
563
|
+
setState((state) => ({
|
|
564
|
+
...state,
|
|
565
|
+
controls
|
|
566
|
+
}));
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
firstEffects.current.disabledFocused = false;
|
|
570
|
+
}
|
|
520
571
|
}, [
|
|
521
572
|
...reduceControlsToArray(controls, 'disabled'),
|
|
522
573
|
...reduceControlsToArray(controls, 'focused')
|
|
523
574
|
]);
|
|
524
575
|
useEffect(() => {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
576
|
+
if (!firstEffects.current.dirty) {
|
|
577
|
+
setState((state) => ({
|
|
578
|
+
...state,
|
|
579
|
+
controls,
|
|
580
|
+
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
581
|
+
dirties: controlsAllChecked(controls, 'dirty')
|
|
582
|
+
}));
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
firstEffects.current.dirty = false;
|
|
586
|
+
}
|
|
531
587
|
}, reduceControlsToArray(controls, 'dirty'));
|
|
532
588
|
useEffect(() => {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
589
|
+
if (!firstEffects.current.touched) {
|
|
590
|
+
setState((state) => ({
|
|
591
|
+
...state,
|
|
592
|
+
controls,
|
|
593
|
+
touched: controlsPartialChecked(controls, 'touched'),
|
|
594
|
+
toucheds: controlsAllChecked(controls, 'touched')
|
|
595
|
+
}));
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
firstEffects.current.touched = false;
|
|
599
|
+
}
|
|
539
600
|
}, reduceControlsToArray(controls, 'touched'));
|
|
540
601
|
const setValidators = useCallback((validators) => {
|
|
541
602
|
setState((state) => ({
|
|
542
603
|
...state,
|
|
543
|
-
...
|
|
604
|
+
...refactorForValid(state.controls, validators)
|
|
544
605
|
}));
|
|
545
606
|
}, []);
|
|
546
607
|
const reset = useCallback(() => {
|