@rolster/react-forms 18.11.4 → 18.12.1
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 +263 -191
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +264 -192
- 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 +45 -34
- 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,113 @@ 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);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
setInitialValue(value) {
|
|
243
|
+
this.refresh('list', {
|
|
244
|
+
controls: value.map((value) => this.valueToControls(value)),
|
|
245
|
+
initialValue: value
|
|
168
246
|
});
|
|
169
247
|
}
|
|
170
248
|
setValue(value) {
|
|
171
|
-
this.refresh({
|
|
249
|
+
this.refresh('list', {
|
|
172
250
|
controls: value.map((value) => this.valueToControls(value))
|
|
173
251
|
});
|
|
174
252
|
}
|
|
175
|
-
|
|
176
|
-
|
|
253
|
+
reset() {
|
|
254
|
+
this.refresh('list', {
|
|
255
|
+
controls: this.initialValue.map((value) => this.valueToControls(value))
|
|
256
|
+
});
|
|
177
257
|
}
|
|
178
258
|
push(controls) {
|
|
179
|
-
this.refresh({
|
|
259
|
+
this.refresh('list', {
|
|
180
260
|
controls: [...this.controls, controls]
|
|
181
261
|
});
|
|
182
262
|
}
|
|
183
263
|
remove(controls) {
|
|
184
|
-
this.refresh({
|
|
185
|
-
controls: this.controls.filter((
|
|
264
|
+
this.refresh('list', {
|
|
265
|
+
controls: this.controls.filter((_controls) => _controls !== controls)
|
|
186
266
|
});
|
|
187
267
|
}
|
|
188
|
-
|
|
189
|
-
|
|
268
|
+
builder(options) {
|
|
269
|
+
return new RolsterArrayList({
|
|
270
|
+
...this,
|
|
271
|
+
...options,
|
|
272
|
+
valueToControls: this.valueToControls
|
|
273
|
+
});
|
|
190
274
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
275
|
+
refresh(action, options) {
|
|
276
|
+
super.refresh(action, options);
|
|
277
|
+
}
|
|
278
|
+
_subscribe(reactControls) {
|
|
279
|
+
Object.values(reactControls).forEach((control) => {
|
|
280
|
+
control.subscribe((action, _control) => {
|
|
281
|
+
const _reactControls = this.controls.map((_controls) => reactControls !== _controls
|
|
195
282
|
? _controls
|
|
196
|
-
:
|
|
197
|
-
|
|
198
|
-
control.uuid === options.uuid
|
|
199
|
-
? control.clone(options)
|
|
200
|
-
: control;
|
|
201
|
-
return controls;
|
|
202
|
-
}, {}));
|
|
203
|
-
this.refresh({ controls });
|
|
283
|
+
: replaceControl(reactControls, _control));
|
|
284
|
+
this.refresh(action, { controls: _reactControls });
|
|
204
285
|
});
|
|
205
286
|
});
|
|
206
287
|
}
|
|
207
288
|
}
|
|
208
|
-
function formArrayList(
|
|
289
|
+
function formArrayList(options) {
|
|
209
290
|
const value = options?.value || [];
|
|
210
291
|
return new RolsterArrayList({
|
|
211
292
|
...options,
|
|
212
|
-
valueToControls,
|
|
213
|
-
controls: value.map((value) => valueToControls(value)),
|
|
293
|
+
controls: value.map((value) => options.valueToControls(value)),
|
|
214
294
|
initialValue: value,
|
|
215
295
|
uuid: v4()
|
|
216
296
|
});
|
|
217
297
|
}
|
|
218
298
|
|
|
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);
|
|
299
|
+
function refactorForValid$1(groups, validators) {
|
|
300
|
+
const errors = validators ? arrayIsValid({ groups, validators }) : [];
|
|
241
301
|
return {
|
|
242
302
|
errors,
|
|
243
303
|
valid: errors.length === 0 && groupAllChecked(groups, 'valid')
|
|
244
304
|
};
|
|
245
305
|
}
|
|
246
|
-
function
|
|
306
|
+
function refactorForGroups(groups, validators) {
|
|
247
307
|
return {
|
|
248
|
-
...
|
|
308
|
+
...refactorForValid$1(groups, validators),
|
|
249
309
|
groups,
|
|
250
310
|
controls: groups.map(({ controls }) => controls),
|
|
251
311
|
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
252
312
|
};
|
|
253
313
|
}
|
|
314
|
+
function refactorForControls(state, groups, action) {
|
|
315
|
+
switch (action) {
|
|
316
|
+
case 'validators':
|
|
317
|
+
return refactorForValid$1(groups, state.validators);
|
|
318
|
+
case 'reset':
|
|
319
|
+
case 'list':
|
|
320
|
+
case 'value':
|
|
321
|
+
return refactorForGroups(groups, state.validators);
|
|
322
|
+
default:
|
|
323
|
+
return { groups };
|
|
324
|
+
}
|
|
325
|
+
}
|
|
254
326
|
function useFormArray(options, validators) {
|
|
255
327
|
const _options = createFormArrayOptions(options, validators);
|
|
256
328
|
const groups = _options.groups || [];
|
|
257
329
|
const initialValue = useRef(groups);
|
|
258
330
|
const [state, setState] = useState({
|
|
259
|
-
...
|
|
331
|
+
...refactorForValid$1(groups, _options.validators),
|
|
260
332
|
controls: groups.map(({ controls }) => controls),
|
|
261
333
|
dirty: false,
|
|
262
334
|
dirties: false,
|
|
@@ -268,21 +340,14 @@ function useFormArray(options, validators) {
|
|
|
268
340
|
validators: _options.validators
|
|
269
341
|
});
|
|
270
342
|
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
|
-
}));
|
|
343
|
+
const subscriber = (action, group) => {
|
|
344
|
+
setState((state) => {
|
|
345
|
+
const groups = state.groups.map((_group) => _group.uuid === group.uuid ? group : _group);
|
|
346
|
+
return {
|
|
347
|
+
...state,
|
|
348
|
+
...refactorForControls(state, groups, action)
|
|
349
|
+
};
|
|
350
|
+
});
|
|
286
351
|
};
|
|
287
352
|
state.groups.forEach((group) => {
|
|
288
353
|
group.subscribe(subscriber);
|
|
@@ -297,49 +362,49 @@ function useFormArray(options, validators) {
|
|
|
297
362
|
const setValue = useCallback((groups) => {
|
|
298
363
|
setState((state) => ({
|
|
299
364
|
...state,
|
|
300
|
-
...
|
|
365
|
+
...refactorForGroups(groups, state.validators)
|
|
301
366
|
}));
|
|
302
367
|
}, []);
|
|
303
368
|
const setInitialValue = useCallback((groups) => {
|
|
304
369
|
setState((state) => ({
|
|
305
370
|
...state,
|
|
306
|
-
...
|
|
371
|
+
...refactorForGroups(groups, state.validators)
|
|
307
372
|
}));
|
|
308
373
|
initialValue.current = groups;
|
|
309
374
|
}, []);
|
|
310
375
|
const push = useCallback((group) => {
|
|
311
376
|
setState((state) => ({
|
|
312
377
|
...state,
|
|
313
|
-
...
|
|
378
|
+
...refactorForGroups([...state.groups, group], state.validators)
|
|
314
379
|
}));
|
|
315
380
|
}, []);
|
|
316
381
|
const merge = useCallback((groups) => {
|
|
317
382
|
setState((state) => ({
|
|
318
383
|
...state,
|
|
319
|
-
...
|
|
384
|
+
...refactorForGroups([...state.groups, ...groups], state.validators)
|
|
320
385
|
}));
|
|
321
386
|
}, []);
|
|
322
387
|
const remove = useCallback(({ uuid }) => {
|
|
323
388
|
setState((state) => ({
|
|
324
389
|
...state,
|
|
325
|
-
...
|
|
326
|
-
}));
|
|
327
|
-
}, []);
|
|
328
|
-
const reset = useCallback(() => {
|
|
329
|
-
setState((state) => ({
|
|
330
|
-
...state,
|
|
331
|
-
...replaceStateInArray(initialValue.current, state.validators)
|
|
390
|
+
...refactorForGroups(state.groups.filter((group) => group.uuid !== uuid), state.validators)
|
|
332
391
|
}));
|
|
333
392
|
}, []);
|
|
334
393
|
const setValidators = useCallback((validators) => {
|
|
335
394
|
setState((state) => ({
|
|
336
395
|
...state,
|
|
337
|
-
...
|
|
396
|
+
...refactorForValid$1(state.groups, validators),
|
|
338
397
|
validators
|
|
339
398
|
}));
|
|
340
399
|
}, []);
|
|
341
400
|
const hasError$1 = useCallback((key) => hasError(state.errors, key), [state.errors]);
|
|
342
401
|
const someErrors$1 = useCallback((keys) => someErrors(state.errors, keys), [state.errors]);
|
|
402
|
+
const reset = useCallback(() => {
|
|
403
|
+
setState((state) => ({
|
|
404
|
+
...state,
|
|
405
|
+
...refactorForGroups(initialValue.current, state.validators)
|
|
406
|
+
}));
|
|
407
|
+
}, []);
|
|
343
408
|
return {
|
|
344
409
|
...state,
|
|
345
410
|
disable,
|
|
@@ -380,14 +445,6 @@ function useControl(options, validators) {
|
|
|
380
445
|
validators: _options.validators
|
|
381
446
|
});
|
|
382
447
|
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
448
|
const focus = useCallback(() => {
|
|
392
449
|
setState((state) => ({ ...state, focused: true }));
|
|
393
450
|
}, []);
|
|
@@ -473,11 +530,8 @@ function useInputControl(options, validators) {
|
|
|
473
530
|
return useControl(options, validators);
|
|
474
531
|
}
|
|
475
532
|
|
|
476
|
-
function
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
function validStateInGroup(controls, validators) {
|
|
480
|
-
const errors = errorsInGroup(controls, validators);
|
|
533
|
+
function refactorForValid(controls, validators) {
|
|
534
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
481
535
|
return {
|
|
482
536
|
errors,
|
|
483
537
|
valid: errors.length === 0 && controlsAllChecked(controls, 'valid')
|
|
@@ -486,8 +540,14 @@ function validStateInGroup(controls, validators) {
|
|
|
486
540
|
function useFormGroup(options, validators) {
|
|
487
541
|
const _options = createFormGroupOptions(options, validators);
|
|
488
542
|
const { controls } = _options;
|
|
543
|
+
const firstEffects = useRef({
|
|
544
|
+
dirty: true,
|
|
545
|
+
disabledFocused: true,
|
|
546
|
+
touched: true,
|
|
547
|
+
value: true
|
|
548
|
+
});
|
|
489
549
|
const [state, setState] = useState({
|
|
490
|
-
...
|
|
550
|
+
...refactorForValid(controls, _options.validators),
|
|
491
551
|
controls,
|
|
492
552
|
dirties: controlsAllChecked(controls, 'dirty'),
|
|
493
553
|
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
@@ -497,50 +557,62 @@ function useFormGroup(options, validators) {
|
|
|
497
557
|
validators: _options.validators
|
|
498
558
|
});
|
|
499
559
|
useEffect(() => {
|
|
500
|
-
|
|
560
|
+
if (!firstEffects.current.value) {
|
|
501
561
|
setState((state) => ({
|
|
502
562
|
...state,
|
|
503
|
-
|
|
563
|
+
...refactorForValid(controls, state.validators),
|
|
564
|
+
controls,
|
|
565
|
+
value: controlsToValue(controls)
|
|
504
566
|
}));
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
...state,
|
|
510
|
-
...validStateInGroup(controls, state.validators),
|
|
511
|
-
controls,
|
|
512
|
-
value: controlsToValue(controls)
|
|
513
|
-
}));
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
firstEffects.current.value = false;
|
|
570
|
+
}
|
|
514
571
|
}, reduceControlsToArray(controls, 'value'));
|
|
515
572
|
useEffect(() => {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
573
|
+
if (!firstEffects.current.disabledFocused) {
|
|
574
|
+
setState((state) => ({
|
|
575
|
+
...state,
|
|
576
|
+
controls
|
|
577
|
+
}));
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
firstEffects.current.disabledFocused = false;
|
|
581
|
+
}
|
|
520
582
|
}, [
|
|
521
583
|
...reduceControlsToArray(controls, 'disabled'),
|
|
522
584
|
...reduceControlsToArray(controls, 'focused')
|
|
523
585
|
]);
|
|
524
586
|
useEffect(() => {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
587
|
+
if (!firstEffects.current.dirty) {
|
|
588
|
+
setState((state) => ({
|
|
589
|
+
...state,
|
|
590
|
+
controls,
|
|
591
|
+
dirty: controlsPartialChecked(controls, 'dirty'),
|
|
592
|
+
dirties: controlsAllChecked(controls, 'dirty')
|
|
593
|
+
}));
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
firstEffects.current.dirty = false;
|
|
597
|
+
}
|
|
531
598
|
}, reduceControlsToArray(controls, 'dirty'));
|
|
532
599
|
useEffect(() => {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
600
|
+
if (!firstEffects.current.touched) {
|
|
601
|
+
setState((state) => ({
|
|
602
|
+
...state,
|
|
603
|
+
controls,
|
|
604
|
+
touched: controlsPartialChecked(controls, 'touched'),
|
|
605
|
+
toucheds: controlsAllChecked(controls, 'touched')
|
|
606
|
+
}));
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
firstEffects.current.touched = false;
|
|
610
|
+
}
|
|
539
611
|
}, reduceControlsToArray(controls, 'touched'));
|
|
540
612
|
const setValidators = useCallback((validators) => {
|
|
541
613
|
setState((state) => ({
|
|
542
614
|
...state,
|
|
543
|
-
...
|
|
615
|
+
...refactorForValid(state.controls, validators)
|
|
544
616
|
}));
|
|
545
617
|
}, []);
|
|
546
618
|
const reset = useCallback(() => {
|