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