@rolster/react-forms 18.8.1 → 18.9.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 +419 -414
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +419 -414
- package/dist/es/index.js.map +1 -1
- package/dist/esm/form-array/form-array-control.d.ts +67 -68
- package/dist/esm/form-array/form-array-control.js +95 -98
- package/dist/esm/form-array/form-array-control.js.map +1 -1
- package/dist/esm/form-array/form-array-group.d.ts +32 -32
- package/dist/esm/form-array/form-array-group.js +54 -52
- package/dist/esm/form-array/form-array-group.js.map +1 -1
- package/dist/esm/form-array/form-array-list.d.ts +26 -25
- package/dist/esm/form-array/form-array-list.js +66 -60
- package/dist/esm/form-array/form-array-list.js.map +1 -1
- package/dist/esm/form-array/form-array.d.ts +5 -5
- package/dist/esm/form-array/form-array.js +100 -100
- package/dist/esm/form-array/index.d.ts +4 -4
- package/dist/esm/form-array/index.js +4 -4
- package/dist/esm/form-control.d.ts +27 -27
- package/dist/esm/form-control.js +90 -90
- package/dist/esm/form-group.d.ts +4 -4
- package/dist/esm/form-group.js +39 -39
- package/dist/esm/form-ref.d.ts +17 -17
- package/dist/esm/form-ref.js +35 -35
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +4 -4
- package/dist/esm/types.d.ts +51 -50
- package/dist/esm/types.js +1 -1
- package/package.json +5 -5
package/dist/es/index.js
CHANGED
|
@@ -3,430 +3,435 @@ import { controlIsValid, hasError, someErrors, groupIsValid, controlsAllChecked,
|
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import { useRef, useState, useEffect } from 'react';
|
|
5
5
|
|
|
6
|
-
class RolsterReactArrayControl {
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.initialValue = options.initialValue;
|
|
9
|
-
this.uuid = options.uuid;
|
|
10
|
-
this.focused = !!options.focused;
|
|
11
|
-
this.unfocused = !this.focused;
|
|
12
|
-
this.touched = !!options.touched;
|
|
13
|
-
this.untouched = !this.touched;
|
|
14
|
-
this.dirty = !!options.dirty;
|
|
15
|
-
this.pristine = !this.dirty;
|
|
16
|
-
this.disabled = !!options.disabled;
|
|
17
|
-
this.enabled = !this.disabled;
|
|
18
|
-
const { value, validators } = options;
|
|
19
|
-
this.value = value;
|
|
20
|
-
this.validators = validators;
|
|
21
|
-
this.errors = validators ? controlIsValid({ value, validators }) : [];
|
|
22
|
-
this.error = this.errors[0];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
this.
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
this.
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
this.
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
this.refresh({
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
function
|
|
93
|
-
return rolsterArrayControl(options, validators);
|
|
94
|
-
}
|
|
95
|
-
function
|
|
96
|
-
return rolsterArrayControl(options, validators);
|
|
97
|
-
}
|
|
98
|
-
function inputArrayControl(options, validators) {
|
|
99
|
-
return rolsterArrayControl(options, validators);
|
|
6
|
+
class RolsterReactArrayControl {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.initialValue = options.initialValue;
|
|
9
|
+
this.uuid = options.uuid;
|
|
10
|
+
this.focused = !!options.focused;
|
|
11
|
+
this.unfocused = !this.focused;
|
|
12
|
+
this.touched = !!options.touched;
|
|
13
|
+
this.untouched = !this.touched;
|
|
14
|
+
this.dirty = !!options.dirty;
|
|
15
|
+
this.pristine = !this.dirty;
|
|
16
|
+
this.disabled = !!options.disabled;
|
|
17
|
+
this.enabled = !this.disabled;
|
|
18
|
+
const { value, validators } = options;
|
|
19
|
+
this.value = value;
|
|
20
|
+
this.validators = validators;
|
|
21
|
+
this.errors = validators ? controlIsValid({ value, validators }) : [];
|
|
22
|
+
this.error = this.errors[0];
|
|
23
|
+
}
|
|
24
|
+
focus() {
|
|
25
|
+
this.unfocused && this.refresh({ focused: true });
|
|
26
|
+
}
|
|
27
|
+
blur() {
|
|
28
|
+
this.focused && this.refresh({ focused: false, touched: true });
|
|
29
|
+
}
|
|
30
|
+
disable() {
|
|
31
|
+
this.enabled && this.refresh({ disabled: true });
|
|
32
|
+
}
|
|
33
|
+
enable() {
|
|
34
|
+
this.disabled && this.refresh({ disabled: false });
|
|
35
|
+
}
|
|
36
|
+
touch() {
|
|
37
|
+
this.untouched && this.refresh({ touched: true });
|
|
38
|
+
}
|
|
39
|
+
setValue(value) {
|
|
40
|
+
this.refresh({ value });
|
|
41
|
+
}
|
|
42
|
+
setValidators(validators) {
|
|
43
|
+
this.refresh({ validators });
|
|
44
|
+
}
|
|
45
|
+
subscribe(subscriber) {
|
|
46
|
+
this.subscriber = subscriber;
|
|
47
|
+
}
|
|
48
|
+
hasError(key) {
|
|
49
|
+
return hasError(this.errors, key);
|
|
50
|
+
}
|
|
51
|
+
someErrors(keys) {
|
|
52
|
+
return someErrors(this.errors, keys);
|
|
53
|
+
}
|
|
54
|
+
reset() {
|
|
55
|
+
this.refresh({
|
|
56
|
+
dirty: false,
|
|
57
|
+
touched: false,
|
|
58
|
+
value: this.initialValue
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
refresh(changes) {
|
|
62
|
+
this.subscriber &&
|
|
63
|
+
this.subscriber({
|
|
64
|
+
...this,
|
|
65
|
+
...changes,
|
|
66
|
+
initialValue: this.initialValue
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
class RolsterArrayControl extends RolsterReactArrayControl {
|
|
71
|
+
constructor(options) {
|
|
72
|
+
super(options);
|
|
73
|
+
this.valid = this.errors.length === 0;
|
|
74
|
+
this.invalid = !this.valid;
|
|
75
|
+
this.wrong = this.touched && this.invalid;
|
|
76
|
+
}
|
|
77
|
+
clone(options) {
|
|
78
|
+
return new RolsterArrayControl(options);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function rolsterArrayControl(options, validators) {
|
|
82
|
+
const controlOptions = createFormControlOptions(options, validators);
|
|
83
|
+
return new RolsterArrayControl({
|
|
84
|
+
...controlOptions,
|
|
85
|
+
initialValue: controlOptions.value,
|
|
86
|
+
uuid: v4()
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function reactArrayControl(options, validators) {
|
|
90
|
+
return rolsterArrayControl(options, validators);
|
|
91
|
+
}
|
|
92
|
+
function formArrayControl(options, validators) {
|
|
93
|
+
return rolsterArrayControl(options, validators);
|
|
94
|
+
}
|
|
95
|
+
function inputArrayControl(options, validators) {
|
|
96
|
+
return rolsterArrayControl(options, validators);
|
|
100
97
|
}
|
|
101
98
|
|
|
102
|
-
class RolsterArrayGroup {
|
|
103
|
-
constructor(options) {
|
|
104
|
-
const { controls, resource, uuid, validators } = options;
|
|
105
|
-
this.uuid = uuid;
|
|
106
|
-
this.controls = controls;
|
|
107
|
-
this.validators = validators;
|
|
108
|
-
this.resource = resource;
|
|
109
|
-
this.errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
110
|
-
this.error = this.errors[0];
|
|
111
|
-
this.valid =
|
|
112
|
-
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
113
|
-
this.invalid = !this.valid;
|
|
114
|
-
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
115
|
-
this.dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
116
|
-
this.touched = controlsPartialChecked(controls, 'touched');
|
|
117
|
-
this.touchedAll = controlsAllChecked(controls, 'touched');
|
|
118
|
-
this.untouched = !this.touched;
|
|
119
|
-
this.untouchedAll = !this.touchedAll;
|
|
120
|
-
this.pristine = !this.dirty;
|
|
121
|
-
this.pristineAll = !this.dirtyAll;
|
|
122
|
-
this.wrong = this.touched && this.invalid;
|
|
123
|
-
this.value = controlsToValue(controls);
|
|
124
|
-
const subscriber = (options) => {
|
|
125
|
-
this.
|
|
126
|
-
controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
|
|
127
|
-
controls[key] =
|
|
128
|
-
control.uuid === options.uuid ? control.clone(options) : control;
|
|
129
|
-
return controls;
|
|
130
|
-
}, {})
|
|
131
|
-
});
|
|
132
|
-
};
|
|
133
|
-
Object.values(controls).forEach((control) => {
|
|
134
|
-
control.subscribe(subscriber);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
setValidators(validators) {
|
|
138
|
-
this.
|
|
139
|
-
}
|
|
140
|
-
subscribe(subscriber) {
|
|
141
|
-
this.subscriber = subscriber;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this.subscriber && this.subscriber({ ...this, ...changes });
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
function formArrayGroup(options, validators) {
|
|
148
|
-
|
|
149
|
-
|
|
99
|
+
class RolsterArrayGroup {
|
|
100
|
+
constructor(options) {
|
|
101
|
+
const { controls, resource, uuid, validators } = options;
|
|
102
|
+
this.uuid = uuid;
|
|
103
|
+
this.controls = controls;
|
|
104
|
+
this.validators = validators;
|
|
105
|
+
this.resource = resource;
|
|
106
|
+
this.errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
107
|
+
this.error = this.errors[0];
|
|
108
|
+
this.valid =
|
|
109
|
+
this.errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
110
|
+
this.invalid = !this.valid;
|
|
111
|
+
this.dirty = controlsPartialChecked(controls, 'dirty');
|
|
112
|
+
this.dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
113
|
+
this.touched = controlsPartialChecked(controls, 'touched');
|
|
114
|
+
this.touchedAll = controlsAllChecked(controls, 'touched');
|
|
115
|
+
this.untouched = !this.touched;
|
|
116
|
+
this.untouchedAll = !this.touchedAll;
|
|
117
|
+
this.pristine = !this.dirty;
|
|
118
|
+
this.pristineAll = !this.dirtyAll;
|
|
119
|
+
this.wrong = this.touched && this.invalid;
|
|
120
|
+
this.value = controlsToValue(controls);
|
|
121
|
+
const subscriber = (options) => {
|
|
122
|
+
this.refresh({
|
|
123
|
+
controls: Object.entries(this.controls).reduce((controls, [key, control]) => {
|
|
124
|
+
controls[key] =
|
|
125
|
+
control.uuid === options.uuid ? control.clone(options) : control;
|
|
126
|
+
return controls;
|
|
127
|
+
}, {})
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
Object.values(controls).forEach((control) => {
|
|
131
|
+
control.subscribe(subscriber);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
setValidators(validators) {
|
|
135
|
+
this.refresh({ validators });
|
|
136
|
+
}
|
|
137
|
+
subscribe(subscriber) {
|
|
138
|
+
this.subscriber = subscriber;
|
|
139
|
+
}
|
|
140
|
+
refresh(changes) {
|
|
141
|
+
this.subscriber && this.subscriber({ ...this, ...changes });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function formArrayGroup(options, validators) {
|
|
145
|
+
return new RolsterArrayGroup({
|
|
146
|
+
...createFormGroupOptions(options, validators),
|
|
147
|
+
uuid: v4()
|
|
148
|
+
});
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
class RolsterArrayList extends RolsterReactArrayControl {
|
|
153
|
-
constructor(options) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
this.
|
|
157
|
-
this.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
this.
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
151
|
+
class RolsterArrayList extends RolsterReactArrayControl {
|
|
152
|
+
constructor(options) {
|
|
153
|
+
const value = options.controls.map((controls) => controlsToValue(controls));
|
|
154
|
+
super({ ...options, value });
|
|
155
|
+
this.valueToControls = options.valueToControls;
|
|
156
|
+
this.controls = options.controls;
|
|
157
|
+
this.valid =
|
|
158
|
+
this.errors.length === 0 &&
|
|
159
|
+
this.controls.reduce((valid, controls) => valid && controlsAllChecked(controls, 'valid'), true);
|
|
160
|
+
this.invalid = !this.valid;
|
|
161
|
+
this.wrong = this.touched && this.invalid;
|
|
162
|
+
options.controls.forEach((controls) => {
|
|
163
|
+
this.subscribeControls(controls);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
setValue(value) {
|
|
167
|
+
this.refresh({
|
|
168
|
+
controls: value.map((value) => this.valueToControls(value))
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
clone(options) {
|
|
172
|
+
return new RolsterArrayList(options);
|
|
173
|
+
}
|
|
174
|
+
push(controls) {
|
|
175
|
+
this.refresh({
|
|
176
|
+
controls: [...this.controls, controls]
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
remove(controls) {
|
|
180
|
+
this.refresh({
|
|
181
|
+
controls: this.controls.filter((currentControls) => currentControls !== controls)
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
refresh(changes) {
|
|
185
|
+
super.refresh(changes);
|
|
186
|
+
}
|
|
187
|
+
subscribeControls(newControls) {
|
|
188
|
+
Object.values(newControls).forEach((control) => {
|
|
189
|
+
control.subscribe((options) => {
|
|
190
|
+
const controls = this.controls.map((currentControls) => currentControls !== newControls
|
|
191
|
+
? currentControls
|
|
192
|
+
: Object.entries(newControls).reduce((controls, [key, control]) => {
|
|
193
|
+
controls[key] =
|
|
194
|
+
control.uuid === options.uuid
|
|
195
|
+
? control.clone(options)
|
|
196
|
+
: control;
|
|
197
|
+
return controls;
|
|
198
|
+
}, {}));
|
|
199
|
+
this.refresh({ controls });
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function formArrayList(valueToControls, options) {
|
|
205
|
+
const value = options?.value || [];
|
|
206
|
+
return new RolsterArrayList({
|
|
207
|
+
...options,
|
|
208
|
+
valueToControls,
|
|
209
|
+
controls: value.map((value) => valueToControls(value)),
|
|
210
|
+
initialValue: value,
|
|
211
|
+
uuid: v4()
|
|
212
|
+
});
|
|
208
213
|
}
|
|
209
214
|
|
|
210
|
-
function useFormArray(options, arrayValidators) {
|
|
211
|
-
const arrayOptions = createFormArrayOptions(options, arrayValidators);
|
|
212
|
-
const groups = arrayOptions.groups || [];
|
|
213
|
-
const currentState = useRef(groups);
|
|
214
|
-
const [state, setState] = useState({
|
|
215
|
-
controls: groups.map(({ controls }) => controls),
|
|
216
|
-
disabled: false,
|
|
217
|
-
groups,
|
|
218
|
-
value: groups.map(({ controls }) => controlsToValue(controls)),
|
|
219
|
-
validators: arrayOptions.validators
|
|
220
|
-
});
|
|
221
|
-
const errors = state.validators
|
|
222
|
-
? arrayIsValid({ groups, validators: state.validators })
|
|
223
|
-
: [];
|
|
224
|
-
const error = errors[0];
|
|
225
|
-
const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
|
|
226
|
-
const dirty = groupPartialChecked(state.groups, 'dirty');
|
|
227
|
-
const dirtyAll = groupAllChecked(state.groups, 'dirty');
|
|
228
|
-
const touched = groupPartialChecked(state.groups, 'touched');
|
|
229
|
-
const touchedAll = groupAllChecked(state.groups, 'touched');
|
|
230
|
-
useEffect(() => {
|
|
231
|
-
const subscriber = (options) => {
|
|
232
|
-
setGroups(state.groups.map((group) => group.uuid === options.uuid
|
|
233
|
-
? new RolsterArrayGroup(options)
|
|
234
|
-
: group));
|
|
235
|
-
};
|
|
236
|
-
state.groups.forEach((group) => {
|
|
237
|
-
group.subscribe(subscriber);
|
|
238
|
-
});
|
|
239
|
-
}, [state.groups]);
|
|
240
|
-
function disable() {
|
|
241
|
-
setState((state) => ({ ...state, disabled: true }));
|
|
242
|
-
}
|
|
243
|
-
function enable() {
|
|
244
|
-
setState((state) => ({ ...state, disabled: false }));
|
|
245
|
-
}
|
|
246
|
-
function setGroups(groups) {
|
|
247
|
-
setState((currentState) => ({
|
|
248
|
-
...currentState,
|
|
249
|
-
groups,
|
|
250
|
-
controls: groups.map(({ controls }) => controls),
|
|
251
|
-
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
252
|
-
}));
|
|
253
|
-
}
|
|
254
|
-
function push(group) {
|
|
255
|
-
setGroups([...state.groups, group]);
|
|
256
|
-
}
|
|
257
|
-
function merge(groups) {
|
|
258
|
-
setGroups([...state.groups, ...groups]);
|
|
259
|
-
}
|
|
260
|
-
function set(groups) {
|
|
261
|
-
setGroups(groups);
|
|
262
|
-
}
|
|
263
|
-
function remove({ uuid }) {
|
|
264
|
-
setGroups(state.groups.filter((group) => group.uuid !== uuid));
|
|
265
|
-
}
|
|
266
|
-
function hasError$1(key) {
|
|
267
|
-
return hasError(errors, key);
|
|
268
|
-
}
|
|
269
|
-
function someErrors$1(keys) {
|
|
270
|
-
return someErrors(errors, keys);
|
|
271
|
-
}
|
|
272
|
-
function reset() {
|
|
273
|
-
setGroups(currentState.current);
|
|
274
|
-
}
|
|
275
|
-
function setValidators(validators) {
|
|
276
|
-
setState((state) => ({ ...state, validators }));
|
|
277
|
-
}
|
|
278
|
-
return {
|
|
279
|
-
...state,
|
|
280
|
-
dirty,
|
|
281
|
-
dirtyAll,
|
|
282
|
-
disable,
|
|
283
|
-
enable,
|
|
284
|
-
enabled: !state.disabled,
|
|
285
|
-
error,
|
|
286
|
-
errors,
|
|
287
|
-
hasError: hasError$1,
|
|
288
|
-
invalid: !valid,
|
|
289
|
-
merge,
|
|
290
|
-
pristine: !dirty,
|
|
291
|
-
pristineAll: !dirtyAll,
|
|
292
|
-
push,
|
|
293
|
-
remove,
|
|
294
|
-
reset,
|
|
295
|
-
set,
|
|
296
|
-
setValidators,
|
|
297
|
-
someErrors: someErrors$1,
|
|
298
|
-
touched,
|
|
299
|
-
touchedAll,
|
|
300
|
-
untouched: !touched,
|
|
301
|
-
untouchedAll: !touchedAll,
|
|
302
|
-
valid,
|
|
303
|
-
wrong: touched && !valid
|
|
304
|
-
};
|
|
215
|
+
function useFormArray(options, arrayValidators) {
|
|
216
|
+
const arrayOptions = createFormArrayOptions(options, arrayValidators);
|
|
217
|
+
const groups = arrayOptions.groups || [];
|
|
218
|
+
const currentState = useRef(groups);
|
|
219
|
+
const [state, setState] = useState({
|
|
220
|
+
controls: groups.map(({ controls }) => controls),
|
|
221
|
+
disabled: false,
|
|
222
|
+
groups,
|
|
223
|
+
value: groups.map(({ controls }) => controlsToValue(controls)),
|
|
224
|
+
validators: arrayOptions.validators
|
|
225
|
+
});
|
|
226
|
+
const errors = state.validators
|
|
227
|
+
? arrayIsValid({ groups, validators: state.validators })
|
|
228
|
+
: [];
|
|
229
|
+
const error = errors[0];
|
|
230
|
+
const valid = errors.length === 0 && groupAllChecked(state.groups, 'valid');
|
|
231
|
+
const dirty = groupPartialChecked(state.groups, 'dirty');
|
|
232
|
+
const dirtyAll = groupAllChecked(state.groups, 'dirty');
|
|
233
|
+
const touched = groupPartialChecked(state.groups, 'touched');
|
|
234
|
+
const touchedAll = groupAllChecked(state.groups, 'touched');
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
const subscriber = (options) => {
|
|
237
|
+
setGroups(state.groups.map((group) => group.uuid === options.uuid
|
|
238
|
+
? new RolsterArrayGroup(options)
|
|
239
|
+
: group));
|
|
240
|
+
};
|
|
241
|
+
state.groups.forEach((group) => {
|
|
242
|
+
group.subscribe(subscriber);
|
|
243
|
+
});
|
|
244
|
+
}, [state.groups]);
|
|
245
|
+
function disable() {
|
|
246
|
+
setState((state) => ({ ...state, disabled: true }));
|
|
247
|
+
}
|
|
248
|
+
function enable() {
|
|
249
|
+
setState((state) => ({ ...state, disabled: false }));
|
|
250
|
+
}
|
|
251
|
+
function setGroups(groups) {
|
|
252
|
+
setState((currentState) => ({
|
|
253
|
+
...currentState,
|
|
254
|
+
groups,
|
|
255
|
+
controls: groups.map(({ controls }) => controls),
|
|
256
|
+
value: groups.map(({ controls }) => controlsToValue(controls))
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
function push(group) {
|
|
260
|
+
setGroups([...state.groups, group]);
|
|
261
|
+
}
|
|
262
|
+
function merge(groups) {
|
|
263
|
+
setGroups([...state.groups, ...groups]);
|
|
264
|
+
}
|
|
265
|
+
function set(groups) {
|
|
266
|
+
setGroups(groups);
|
|
267
|
+
}
|
|
268
|
+
function remove({ uuid }) {
|
|
269
|
+
setGroups(state.groups.filter((group) => group.uuid !== uuid));
|
|
270
|
+
}
|
|
271
|
+
function hasError$1(key) {
|
|
272
|
+
return hasError(errors, key);
|
|
273
|
+
}
|
|
274
|
+
function someErrors$1(keys) {
|
|
275
|
+
return someErrors(errors, keys);
|
|
276
|
+
}
|
|
277
|
+
function reset() {
|
|
278
|
+
setGroups(currentState.current);
|
|
279
|
+
}
|
|
280
|
+
function setValidators(validators) {
|
|
281
|
+
setState((state) => ({ ...state, validators }));
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
...state,
|
|
285
|
+
dirty,
|
|
286
|
+
dirtyAll,
|
|
287
|
+
disable,
|
|
288
|
+
enable,
|
|
289
|
+
enabled: !state.disabled,
|
|
290
|
+
error,
|
|
291
|
+
errors,
|
|
292
|
+
hasError: hasError$1,
|
|
293
|
+
invalid: !valid,
|
|
294
|
+
merge,
|
|
295
|
+
pristine: !dirty,
|
|
296
|
+
pristineAll: !dirtyAll,
|
|
297
|
+
push,
|
|
298
|
+
remove,
|
|
299
|
+
reset,
|
|
300
|
+
set,
|
|
301
|
+
setValidators,
|
|
302
|
+
someErrors: someErrors$1,
|
|
303
|
+
touched,
|
|
304
|
+
touchedAll,
|
|
305
|
+
untouched: !touched,
|
|
306
|
+
untouchedAll: !touchedAll,
|
|
307
|
+
valid,
|
|
308
|
+
wrong: touched && !valid
|
|
309
|
+
};
|
|
305
310
|
}
|
|
306
311
|
|
|
307
|
-
function useControl(controlOptions, controlValidators) {
|
|
308
|
-
const { value, touched, validators } = createFormControlOptions(controlOptions, controlValidators);
|
|
309
|
-
const [state, setState] = useState({
|
|
310
|
-
dirty: false,
|
|
311
|
-
disabled: false,
|
|
312
|
-
focused: false,
|
|
313
|
-
touched: !!touched,
|
|
314
|
-
validators,
|
|
315
|
-
value
|
|
316
|
-
});
|
|
317
|
-
const initialValue = useRef(value);
|
|
318
|
-
const elementRef = useRef(null);
|
|
319
|
-
const errors = state.validators
|
|
320
|
-
? controlIsValid({
|
|
321
|
-
value: state.value,
|
|
322
|
-
validators: state.validators
|
|
323
|
-
})
|
|
324
|
-
: [];
|
|
325
|
-
const valid = errors.length === 0;
|
|
326
|
-
function focus() {
|
|
327
|
-
setState((state) => ({ ...state, focused: true }));
|
|
328
|
-
}
|
|
329
|
-
function blur() {
|
|
330
|
-
setState((state) => ({ ...state, focused: false, touched: true }));
|
|
331
|
-
}
|
|
332
|
-
function disable() {
|
|
333
|
-
setState((state) => ({ ...state, disabled: true }));
|
|
334
|
-
}
|
|
335
|
-
function enable() {
|
|
336
|
-
setState((state) => ({ ...state, disabled: false }));
|
|
337
|
-
}
|
|
338
|
-
function touch() {
|
|
339
|
-
setState((state) => ({ ...state, touched: true }));
|
|
340
|
-
}
|
|
341
|
-
function setValue(value) {
|
|
342
|
-
setState((state) => ({ ...state, dirty: true, value }));
|
|
343
|
-
}
|
|
344
|
-
function setValidators(validators) {
|
|
345
|
-
setState((state) => ({ ...state, validators }));
|
|
346
|
-
}
|
|
347
|
-
function hasError$1(key) {
|
|
348
|
-
return hasError(errors, key);
|
|
349
|
-
}
|
|
350
|
-
function someErrors$1(keys) {
|
|
351
|
-
return someErrors(errors, keys);
|
|
352
|
-
}
|
|
353
|
-
function reset() {
|
|
354
|
-
setState((state) => ({
|
|
355
|
-
...state,
|
|
356
|
-
dirty: false,
|
|
357
|
-
value: initialValue.current,
|
|
358
|
-
touched: false
|
|
359
|
-
}));
|
|
360
|
-
}
|
|
361
|
-
return {
|
|
362
|
-
...state,
|
|
363
|
-
blur,
|
|
364
|
-
disable,
|
|
365
|
-
elementRef,
|
|
366
|
-
enable,
|
|
367
|
-
enabled: !state.disabled,
|
|
368
|
-
error: errors[0],
|
|
369
|
-
errors,
|
|
370
|
-
focus,
|
|
371
|
-
hasError: hasError$1,
|
|
372
|
-
invalid: !valid,
|
|
373
|
-
pristine: !state.dirty,
|
|
374
|
-
reset,
|
|
375
|
-
setValidators,
|
|
376
|
-
setValue,
|
|
377
|
-
someErrors: someErrors$1,
|
|
378
|
-
touch,
|
|
379
|
-
unfocused: !state.focused,
|
|
380
|
-
untouched: !state.touched,
|
|
381
|
-
valid,
|
|
382
|
-
wrong: state.touched && !valid
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
function useReactControl(options, validators) {
|
|
386
|
-
return useControl(options, validators);
|
|
387
|
-
}
|
|
388
|
-
function useFormControl(options, validators) {
|
|
389
|
-
return useControl(options, validators);
|
|
390
|
-
}
|
|
391
|
-
function useInputControl(options, validators) {
|
|
392
|
-
return useControl(options, validators);
|
|
312
|
+
function useControl(controlOptions, controlValidators) {
|
|
313
|
+
const { value, touched, validators } = createFormControlOptions(controlOptions, controlValidators);
|
|
314
|
+
const [state, setState] = useState({
|
|
315
|
+
dirty: false,
|
|
316
|
+
disabled: false,
|
|
317
|
+
focused: false,
|
|
318
|
+
touched: !!touched,
|
|
319
|
+
validators,
|
|
320
|
+
value
|
|
321
|
+
});
|
|
322
|
+
const initialValue = useRef(value);
|
|
323
|
+
const elementRef = useRef(null);
|
|
324
|
+
const errors = state.validators
|
|
325
|
+
? controlIsValid({
|
|
326
|
+
value: state.value,
|
|
327
|
+
validators: state.validators
|
|
328
|
+
})
|
|
329
|
+
: [];
|
|
330
|
+
const valid = errors.length === 0;
|
|
331
|
+
function focus() {
|
|
332
|
+
setState((state) => ({ ...state, focused: true }));
|
|
333
|
+
}
|
|
334
|
+
function blur() {
|
|
335
|
+
setState((state) => ({ ...state, focused: false, touched: true }));
|
|
336
|
+
}
|
|
337
|
+
function disable() {
|
|
338
|
+
setState((state) => ({ ...state, disabled: true }));
|
|
339
|
+
}
|
|
340
|
+
function enable() {
|
|
341
|
+
setState((state) => ({ ...state, disabled: false }));
|
|
342
|
+
}
|
|
343
|
+
function touch() {
|
|
344
|
+
setState((state) => ({ ...state, touched: true }));
|
|
345
|
+
}
|
|
346
|
+
function setValue(value) {
|
|
347
|
+
setState((state) => ({ ...state, dirty: true, value }));
|
|
348
|
+
}
|
|
349
|
+
function setValidators(validators) {
|
|
350
|
+
setState((state) => ({ ...state, validators }));
|
|
351
|
+
}
|
|
352
|
+
function hasError$1(key) {
|
|
353
|
+
return hasError(errors, key);
|
|
354
|
+
}
|
|
355
|
+
function someErrors$1(keys) {
|
|
356
|
+
return someErrors(errors, keys);
|
|
357
|
+
}
|
|
358
|
+
function reset() {
|
|
359
|
+
setState((state) => ({
|
|
360
|
+
...state,
|
|
361
|
+
dirty: false,
|
|
362
|
+
value: initialValue.current,
|
|
363
|
+
touched: false
|
|
364
|
+
}));
|
|
365
|
+
}
|
|
366
|
+
return {
|
|
367
|
+
...state,
|
|
368
|
+
blur,
|
|
369
|
+
disable,
|
|
370
|
+
elementRef,
|
|
371
|
+
enable,
|
|
372
|
+
enabled: !state.disabled,
|
|
373
|
+
error: errors[0],
|
|
374
|
+
errors,
|
|
375
|
+
focus,
|
|
376
|
+
hasError: hasError$1,
|
|
377
|
+
invalid: !valid,
|
|
378
|
+
pristine: !state.dirty,
|
|
379
|
+
reset,
|
|
380
|
+
setValidators,
|
|
381
|
+
setValue,
|
|
382
|
+
someErrors: someErrors$1,
|
|
383
|
+
touch,
|
|
384
|
+
unfocused: !state.focused,
|
|
385
|
+
untouched: !state.touched,
|
|
386
|
+
valid,
|
|
387
|
+
wrong: state.touched && !valid
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function useReactControl(options, validators) {
|
|
391
|
+
return useControl(options, validators);
|
|
392
|
+
}
|
|
393
|
+
function useFormControl(options, validators) {
|
|
394
|
+
return useControl(options, validators);
|
|
395
|
+
}
|
|
396
|
+
function useInputControl(options, validators) {
|
|
397
|
+
return useControl(options, validators);
|
|
393
398
|
}
|
|
394
399
|
|
|
395
|
-
function useFormGroup(options, groupValidators) {
|
|
396
|
-
const groupOptions = createFormGroupOptions(options, groupValidators);
|
|
397
|
-
const [validators, setValidators] = useState(groupOptions.validators);
|
|
398
|
-
const { controls } = groupOptions;
|
|
399
|
-
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
400
|
-
const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
401
|
-
const value = controlsToValue(controls);
|
|
402
|
-
const dirty = controlsPartialChecked(controls, 'dirty');
|
|
403
|
-
const dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
404
|
-
const touched = controlsPartialChecked(controls, 'touched');
|
|
405
|
-
const touchedAll = controlsAllChecked(controls, 'touched');
|
|
406
|
-
function reset() {
|
|
407
|
-
Object.values(controls).forEach((control) => {
|
|
408
|
-
control.reset();
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
return {
|
|
412
|
-
controls,
|
|
413
|
-
dirty,
|
|
414
|
-
dirtyAll,
|
|
415
|
-
error: errors[0],
|
|
416
|
-
errors,
|
|
417
|
-
invalid: !valid,
|
|
418
|
-
pristine: !dirty,
|
|
419
|
-
pristineAll: !dirtyAll,
|
|
420
|
-
reset,
|
|
421
|
-
setValidators,
|
|
422
|
-
touched,
|
|
423
|
-
touchedAll,
|
|
424
|
-
untouched: !touched,
|
|
425
|
-
untouchedAll: !touchedAll,
|
|
426
|
-
valid,
|
|
427
|
-
value,
|
|
428
|
-
wrong: touched && !valid
|
|
429
|
-
};
|
|
400
|
+
function useFormGroup(options, groupValidators) {
|
|
401
|
+
const groupOptions = createFormGroupOptions(options, groupValidators);
|
|
402
|
+
const [validators, setValidators] = useState(groupOptions.validators);
|
|
403
|
+
const { controls } = groupOptions;
|
|
404
|
+
const errors = validators ? groupIsValid({ controls, validators }) : [];
|
|
405
|
+
const valid = errors.length === 0 && controlsAllChecked(controls, 'valid');
|
|
406
|
+
const value = controlsToValue(controls);
|
|
407
|
+
const dirty = controlsPartialChecked(controls, 'dirty');
|
|
408
|
+
const dirtyAll = controlsAllChecked(controls, 'dirty');
|
|
409
|
+
const touched = controlsPartialChecked(controls, 'touched');
|
|
410
|
+
const touchedAll = controlsAllChecked(controls, 'touched');
|
|
411
|
+
function reset() {
|
|
412
|
+
Object.values(controls).forEach((control) => {
|
|
413
|
+
control.reset();
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
controls,
|
|
418
|
+
dirty,
|
|
419
|
+
dirtyAll,
|
|
420
|
+
error: errors[0],
|
|
421
|
+
errors,
|
|
422
|
+
invalid: !valid,
|
|
423
|
+
pristine: !dirty,
|
|
424
|
+
pristineAll: !dirtyAll,
|
|
425
|
+
reset,
|
|
426
|
+
setValidators,
|
|
427
|
+
touched,
|
|
428
|
+
touchedAll,
|
|
429
|
+
untouched: !touched,
|
|
430
|
+
untouchedAll: !touchedAll,
|
|
431
|
+
valid,
|
|
432
|
+
value,
|
|
433
|
+
wrong: touched && !valid
|
|
434
|
+
};
|
|
430
435
|
}
|
|
431
436
|
|
|
432
437
|
export { formArrayControl, formArrayGroup, formArrayList, inputArrayControl, reactArrayControl, useFormArray, useFormControl, useFormGroup, useInputControl, useReactControl };
|