@railway-ts/use-form 0.1.30 → 0.1.32
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/index.mjs +52 -34
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +70 -8
- package/docs/GETTING_STARTED.md +6 -0
- package/docs/RECIPES.md +148 -25
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -5,10 +5,17 @@ import { formatErrors, validate } from '@railway-ts/pipelines/schema';
|
|
|
5
5
|
// src/useForm.ts
|
|
6
6
|
|
|
7
7
|
// src/utils.ts
|
|
8
|
+
var normalizePath = (path) => path.replace(/\[(\d+)\]/g, ".$1");
|
|
9
|
+
var normalizePathKeys = (map) => {
|
|
10
|
+
const out = {};
|
|
11
|
+
for (const k of Object.keys(map)) {
|
|
12
|
+
out[normalizePath(k)] = map[k];
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
};
|
|
8
16
|
var getValueByPath = (obj, path) => {
|
|
9
17
|
if (!path) return obj;
|
|
10
|
-
const
|
|
11
|
-
const parts = normalizedPath.split(".").filter(Boolean);
|
|
18
|
+
const parts = normalizePath(path).split(".").filter(Boolean);
|
|
12
19
|
let current = obj;
|
|
13
20
|
for (const part of parts) {
|
|
14
21
|
if (current == null) return void 0;
|
|
@@ -18,8 +25,7 @@ var getValueByPath = (obj, path) => {
|
|
|
18
25
|
};
|
|
19
26
|
var setValueByPath = (obj, path, value) => {
|
|
20
27
|
if (!path) return value;
|
|
21
|
-
const
|
|
22
|
-
const parts = normalizedPath.split(".").filter(Boolean);
|
|
28
|
+
const parts = normalizePath(path).split(".").filter(Boolean);
|
|
23
29
|
const result = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
24
30
|
let currNew = Array.isArray(result) ? result : result;
|
|
25
31
|
let currOld = obj;
|
|
@@ -62,8 +68,8 @@ var setValueByPath = (obj, path, value) => {
|
|
|
62
68
|
};
|
|
63
69
|
var isPathAffected = (path, changePath) => {
|
|
64
70
|
if (path === changePath) return true;
|
|
65
|
-
const normalizedPath = path
|
|
66
|
-
const normalizedChangePath = changePath
|
|
71
|
+
const normalizedPath = normalizePath(path);
|
|
72
|
+
const normalizedChangePath = normalizePath(changePath);
|
|
67
73
|
return normalizedPath === normalizedChangePath || normalizedPath.startsWith(`${normalizedChangePath}.`);
|
|
68
74
|
};
|
|
69
75
|
var collectFieldPaths = (obj, prefix = "") => {
|
|
@@ -73,7 +79,7 @@ var collectFieldPaths = (obj, prefix = "") => {
|
|
|
73
79
|
if (path) paths.push(path);
|
|
74
80
|
if (isLeaf(value)) return;
|
|
75
81
|
if (Array.isArray(value)) {
|
|
76
|
-
value.forEach((item, i) => visit(item, `${path}
|
|
82
|
+
value.forEach((item, i) => visit(item, path ? `${path}.${i}` : `${i}`));
|
|
77
83
|
return;
|
|
78
84
|
}
|
|
79
85
|
const entries = Object.entries(value);
|
|
@@ -160,18 +166,18 @@ var formReducer = (state, action, initialValues) => {
|
|
|
160
166
|
...state,
|
|
161
167
|
touched: {
|
|
162
168
|
...state.touched,
|
|
163
|
-
[action.field]: action.isTouched
|
|
169
|
+
[normalizePath(action.field)]: action.isTouched
|
|
164
170
|
}
|
|
165
171
|
};
|
|
166
172
|
case "SET_CLIENT_ERRORS":
|
|
167
173
|
return {
|
|
168
174
|
...state,
|
|
169
|
-
clientErrors: action.errors
|
|
175
|
+
clientErrors: normalizePathKeys(action.errors)
|
|
170
176
|
};
|
|
171
177
|
case "SET_SERVER_ERRORS":
|
|
172
178
|
return {
|
|
173
179
|
...state,
|
|
174
|
-
serverErrors: action.errors
|
|
180
|
+
serverErrors: normalizePathKeys(action.errors)
|
|
175
181
|
};
|
|
176
182
|
case "CLEAR_SERVER_ERRORS":
|
|
177
183
|
return {
|
|
@@ -191,11 +197,12 @@ var formReducer = (state, action, initialValues) => {
|
|
|
191
197
|
isFormValidating: action.isFormValidating
|
|
192
198
|
};
|
|
193
199
|
case "SET_FIELD_VALIDATING": {
|
|
200
|
+
const key = normalizePath(action.field);
|
|
194
201
|
const newValidatingFields = { ...state.validatingFields };
|
|
195
202
|
if (action.isValidating) {
|
|
196
|
-
newValidatingFields[
|
|
203
|
+
newValidatingFields[key] = true;
|
|
197
204
|
} else {
|
|
198
|
-
delete newValidatingFields[
|
|
205
|
+
delete newValidatingFields[key];
|
|
199
206
|
}
|
|
200
207
|
return {
|
|
201
208
|
...state,
|
|
@@ -203,11 +210,12 @@ var formReducer = (state, action, initialValues) => {
|
|
|
203
210
|
};
|
|
204
211
|
}
|
|
205
212
|
case "SET_FIELD_ERROR": {
|
|
213
|
+
const key = normalizePath(action.field);
|
|
206
214
|
const newFieldErrors = { ...state.fieldErrors };
|
|
207
215
|
if (action.error) {
|
|
208
|
-
newFieldErrors[
|
|
216
|
+
newFieldErrors[key] = action.error;
|
|
209
217
|
} else {
|
|
210
|
-
delete newFieldErrors[
|
|
218
|
+
delete newFieldErrors[key];
|
|
211
219
|
}
|
|
212
220
|
return {
|
|
213
221
|
...state,
|
|
@@ -230,7 +238,7 @@ var formReducer = (state, action, initialValues) => {
|
|
|
230
238
|
case "MARK_ALL_TOUCHED": {
|
|
231
239
|
const allTouched = {};
|
|
232
240
|
action.fields.forEach((path) => {
|
|
233
|
-
allTouched[path] = true;
|
|
241
|
+
allTouched[normalizePath(path)] = true;
|
|
234
242
|
});
|
|
235
243
|
return {
|
|
236
244
|
...state,
|
|
@@ -410,7 +418,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
410
418
|
* ))
|
|
411
419
|
*/
|
|
412
420
|
getFieldProps: (index, subField) => {
|
|
413
|
-
const path = `${field}
|
|
421
|
+
const path = `${field}.${index}.${subField}`;
|
|
414
422
|
return getFieldProps(path);
|
|
415
423
|
},
|
|
416
424
|
/**
|
|
@@ -430,7 +438,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
430
438
|
* </select>
|
|
431
439
|
*/
|
|
432
440
|
getSelectFieldProps: (index, subField) => {
|
|
433
|
-
const path = `${field}
|
|
441
|
+
const path = `${field}.${index}.${subField}`;
|
|
434
442
|
return getSelectFieldProps(path);
|
|
435
443
|
},
|
|
436
444
|
/**
|
|
@@ -447,7 +455,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
447
455
|
* <input type="range" min={0} max={10} {...helpers.getSliderProps(index, "priority")} />
|
|
448
456
|
*/
|
|
449
457
|
getSliderProps: (index, subField) => {
|
|
450
|
-
const path = `${field}
|
|
458
|
+
const path = `${field}.${index}.${subField}`;
|
|
451
459
|
return getSliderProps(path);
|
|
452
460
|
},
|
|
453
461
|
/**
|
|
@@ -464,7 +472,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
464
472
|
* <input type="checkbox" {...helpers.getCheckboxProps(index, "isFavorite")} />
|
|
465
473
|
*/
|
|
466
474
|
getCheckboxProps: (index, subField) => {
|
|
467
|
-
const path = `${field}
|
|
475
|
+
const path = `${field}.${index}.${subField}`;
|
|
468
476
|
return getCheckboxProps(path);
|
|
469
477
|
},
|
|
470
478
|
/**
|
|
@@ -484,7 +492,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
484
492
|
* </label>
|
|
485
493
|
*/
|
|
486
494
|
getSwitchProps: (index, subField) => {
|
|
487
|
-
const path = `${field}
|
|
495
|
+
const path = `${field}.${index}.${subField}`;
|
|
488
496
|
return getSwitchProps(path);
|
|
489
497
|
},
|
|
490
498
|
/**
|
|
@@ -501,7 +509,7 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
501
509
|
* <input type="file" accept="image/*" {...helpers.getFileFieldProps(index, "avatar")} />
|
|
502
510
|
*/
|
|
503
511
|
getFileFieldProps: (index, subField) => {
|
|
504
|
-
const path = `${field}
|
|
512
|
+
const path = `${field}.${index}.${subField}`;
|
|
505
513
|
return getFileFieldProps(path);
|
|
506
514
|
},
|
|
507
515
|
/**
|
|
@@ -526,11 +534,11 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
526
534
|
* </label>
|
|
527
535
|
*/
|
|
528
536
|
getRadioGroupOptionProps: (index, subField, optionValue) => {
|
|
529
|
-
const path = `${field}
|
|
537
|
+
const path = `${field}.${index}.${subField}`;
|
|
530
538
|
return getRadioGroupOptionProps(path, optionValue);
|
|
531
539
|
},
|
|
532
|
-
getFieldError: (index, subField) => getFieldError(`${field}
|
|
533
|
-
getFieldId: (index, subField, optionValue) => getFieldId(`${field}
|
|
540
|
+
getFieldError: (index, subField) => getFieldError(`${field}.${index}.${subField}`),
|
|
541
|
+
getFieldId: (index, subField, optionValue) => getFieldId(`${field}.${index}.${subField}`, optionValue)
|
|
534
542
|
};
|
|
535
543
|
};
|
|
536
544
|
|
|
@@ -753,13 +761,14 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
753
761
|
const fieldValue = getValueByPath(values, field);
|
|
754
762
|
const result = validatorFn(fieldValue, values);
|
|
755
763
|
if (result instanceof Promise) {
|
|
756
|
-
|
|
757
|
-
|
|
764
|
+
const seqKey = normalizePath(field);
|
|
765
|
+
if (!fieldValidationSeqRef.current[seqKey]) {
|
|
766
|
+
fieldValidationSeqRef.current[seqKey] = 0;
|
|
758
767
|
}
|
|
759
|
-
const seq = ++fieldValidationSeqRef.current[
|
|
768
|
+
const seq = ++fieldValidationSeqRef.current[seqKey];
|
|
760
769
|
dispatch({ type: "SET_FIELD_VALIDATING", field, isValidating: true });
|
|
761
770
|
void result.then((error) => {
|
|
762
|
-
if (seq === fieldValidationSeqRef.current[
|
|
771
|
+
if (seq === fieldValidationSeqRef.current[seqKey]) {
|
|
763
772
|
dispatch({ type: "SET_FIELD_ERROR", field, error });
|
|
764
773
|
dispatch({
|
|
765
774
|
type: "SET_FIELD_VALIDATING",
|
|
@@ -788,8 +797,8 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
788
797
|
const schemaResult = validateForm(values);
|
|
789
798
|
if (fieldValidators?.[field]) {
|
|
790
799
|
const afterSchema = (result) => {
|
|
791
|
-
const schemaErrors = isErr(result) ? formatErrors(result.error) : {};
|
|
792
|
-
if (!schemaErrors[field]) {
|
|
800
|
+
const schemaErrors = isErr(result) ? normalizePathKeys(formatErrors(result.error)) : {};
|
|
801
|
+
if (!schemaErrors[normalizePath(field)]) {
|
|
793
802
|
runFieldValidator(field, values);
|
|
794
803
|
} else {
|
|
795
804
|
dispatch({ type: "SET_FIELD_ERROR", field, error: void 0 });
|
|
@@ -833,7 +842,7 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
833
842
|
field,
|
|
834
843
|
value
|
|
835
844
|
});
|
|
836
|
-
if (touchOnChange && !formStateRef.current.touched[field]) {
|
|
845
|
+
if (touchOnChange && !formStateRef.current.touched[normalizePath(field)]) {
|
|
837
846
|
dispatch({ type: "SET_FIELD_TOUCHED", field, isTouched: true });
|
|
838
847
|
}
|
|
839
848
|
if (shouldValidate) {
|
|
@@ -865,7 +874,7 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
865
874
|
if (shouldValidate) {
|
|
866
875
|
if (!validateOnChange) {
|
|
867
876
|
runValidationPipeline(field, formState.values);
|
|
868
|
-
} else if (!formState.touched[field]) {
|
|
877
|
+
} else if (!formState.touched[normalizePath(field)]) {
|
|
869
878
|
void validateForm(formState.values);
|
|
870
879
|
}
|
|
871
880
|
}
|
|
@@ -980,7 +989,13 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
980
989
|
}, [initialValues, validateForm, validateOnMount]);
|
|
981
990
|
const getFieldError = useCallback(
|
|
982
991
|
(field) => {
|
|
983
|
-
|
|
992
|
+
const key = normalizePath(field);
|
|
993
|
+
const err3 = errors[key];
|
|
994
|
+
if (!err3) return void 0;
|
|
995
|
+
for (const touchedKey in formState.touched) {
|
|
996
|
+
if (formState.touched[touchedKey] && isPathAffected(touchedKey, key)) return err3;
|
|
997
|
+
}
|
|
998
|
+
return void 0;
|
|
984
999
|
},
|
|
985
1000
|
[formState.touched, errors]
|
|
986
1001
|
);
|
|
@@ -1059,7 +1074,10 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1059
1074
|
const getSwitchPropsAtPath = (path) => createNativeSwitchProps(path, formState.values, setFieldValue, setFieldTouched);
|
|
1060
1075
|
const getFileFieldPropsAtPath = (path) => createNativeFileFieldProps(path, formState.values, setFieldValue, setFieldTouched);
|
|
1061
1076
|
const getRadioGroupOptionPropsAtPath = (path, opt) => createRadioGroupOptionProps(path, opt, formState.values, setFieldValue, setFieldTouched);
|
|
1062
|
-
const getFieldErrorAtPath = (path) =>
|
|
1077
|
+
const getFieldErrorAtPath = (path) => {
|
|
1078
|
+
const key = normalizePath(path);
|
|
1079
|
+
return formState.touched[key] ? errors[key] : void 0;
|
|
1080
|
+
};
|
|
1063
1081
|
const getFieldIdAtPath = (path, optionValue) => optionValue !== void 0 ? `${path}-${String(optionValue)}` : path;
|
|
1064
1082
|
const arrayValue = getValueByPath(formState.values, field) || [];
|
|
1065
1083
|
return createArrayHelpers(
|