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