@railway-ts/use-form 0.1.24 → 0.1.26
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/README.md +3 -2
- package/dist/index.d.ts +33 -1
- package/dist/index.mjs +26 -13
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +131 -18
- package/docs/GETTING_STARTED.md +22 -14
- package/docs/RECIPES.md +81 -27
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ Bring your own Zod, Valibot, or ArkType via [Standard Schema](https://github.com
|
|
|
32
32
|
bun add @railway-ts/use-form @railway-ts/pipelines # or npm, pnpm, yarn
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Requires React 18+ and @railway-ts/pipelines ^0.1.
|
|
35
|
+
Requires React 18+ and @railway-ts/pipelines ^0.1.26.
|
|
36
36
|
|
|
37
37
|
## Quick Start
|
|
38
38
|
|
|
@@ -233,7 +233,7 @@ Cross-field validation, async username check, server errors, and React Query int
|
|
|
233
233
|
- **Standard Schema v1** -- bring your own Zod, Valibot, or ArkType schema
|
|
234
234
|
- **Native HTML bindings** -- spread `getFieldProps` onto inputs, selects, checkboxes, files, radios, sliders
|
|
235
235
|
- **Three error layers** -- client, per-field async, and server errors with automatic priority
|
|
236
|
-
- **Array helpers** -- type-safe `push`, `remove`, `swap`, `
|
|
236
|
+
- **Array helpers** -- type-safe `push`, `remove`, `swap`, `insert`, `replace` with field bindings
|
|
237
237
|
- **Four validation modes** -- `live`, `blur`, `mount`, `submit`
|
|
238
238
|
- **Auto-submission** -- `useFormAutoSubmission` for search/filter forms with debounced submit
|
|
239
239
|
- **React 18 + 19** -- compatible with both, tree-shakeable ESM
|
|
@@ -277,6 +277,7 @@ Then open http://localhost:3000. The interactive app has tabs for:
|
|
|
277
277
|
- **Zod** -- Standard Schema integration with Zod
|
|
278
278
|
- **Valibot** -- Standard Schema integration with Valibot
|
|
279
279
|
- **Field Validators** -- Per-field async validation with loading indicators
|
|
280
|
+
- **Array Helpers** -- Dynamic arrays with push, remove, swap, and per-item field bindings
|
|
280
281
|
|
|
281
282
|
Or try it live on [StackBlitz](https://stackblitz.com/edit/vitejs-vite-c3zpmon9?embed=1&file=src%2FApp.tsx).
|
|
282
283
|
|
package/dist/index.d.ts
CHANGED
|
@@ -491,7 +491,7 @@ type ExtractFieldPaths<T> = T extends readonly unknown[] ? never : T extends obj
|
|
|
491
491
|
* // Used with arrayHelpers for type-safe array operations
|
|
492
492
|
* const contactsHelpers = form.arrayHelpers<GetArrayItemType<UserForm, "contacts">>("contacts");
|
|
493
493
|
*/
|
|
494
|
-
type GetArrayItemType<TValues extends Record<string, unknown>, TField extends keyof TValues> = TValues[TField] extends readonly (infer TItem)[] ? TItem : TValues[TField] extends (infer TItem)[] ? TItem : never;
|
|
494
|
+
type GetArrayItemType<TValues extends Record<string, unknown>, TField extends keyof TValues> = NonNullable<TValues[TField]> extends readonly (infer TItem)[] ? TItem : NonNullable<TValues[TField]> extends (infer TItem)[] ? TItem : never;
|
|
495
495
|
/**
|
|
496
496
|
* Helper functions for working with array fields in forms.
|
|
497
497
|
* Provides type-safe methods to add, remove, update, and reorder array items,
|
|
@@ -672,6 +672,36 @@ interface ArrayHelpers<TItem, TFieldPaths extends string = ExtractFieldPaths<TIt
|
|
|
672
672
|
* <input type="radio" {...helpers.getRadioGroupOptionProps(index, "status", "active")} />
|
|
673
673
|
*/
|
|
674
674
|
getRadioGroupOptionProps: (index: number, subField: TFieldPaths, optionValue: string | number) => NativeRadioGroupOptionProps;
|
|
675
|
+
/**
|
|
676
|
+
* Returns the validation error for a field within an array item, but only
|
|
677
|
+
* if the field has been touched. Mirrors the top-level `form.getFieldError`
|
|
678
|
+
* behaviour using the `(index, subField)` pattern.
|
|
679
|
+
*
|
|
680
|
+
* @param index - The zero-based index of the array item
|
|
681
|
+
* @param subField - Field path within the array item (type-safe)
|
|
682
|
+
* @returns The error message string, or `undefined` if untouched / valid
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* {helpers.getFieldError(index, "email") && (
|
|
686
|
+
* <span>{helpers.getFieldError(index, "email")}</span>
|
|
687
|
+
* )}
|
|
688
|
+
*/
|
|
689
|
+
getFieldError: (index: number, subField: TFieldPaths) => string | undefined;
|
|
690
|
+
/**
|
|
691
|
+
* Returns a stable HTML element ID for a field within an array item.
|
|
692
|
+
* Useful for linking `<label htmlFor>` to an input without calling a full
|
|
693
|
+
* props factory. Mirrors the top-level `form.getFieldId`.
|
|
694
|
+
*
|
|
695
|
+
* @param index - The zero-based index of the array item
|
|
696
|
+
* @param subField - Field path within the array item (type-safe)
|
|
697
|
+
* @param optionValue - Optional value for radio group / checkbox group options
|
|
698
|
+
* @returns The element ID string
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* <label htmlFor={helpers.getFieldId(index, "name")}>Name</label>
|
|
702
|
+
* <input {...helpers.getFieldProps(index, "name")} />
|
|
703
|
+
*/
|
|
704
|
+
getFieldId: (index: number, subField: TFieldPaths, optionValue?: string | number) => string;
|
|
675
705
|
}
|
|
676
706
|
/**
|
|
677
707
|
* The return type of the `useForm` hook.
|
|
@@ -692,6 +722,8 @@ interface UseFormReturn<TValues extends Record<string, unknown>> {
|
|
|
692
722
|
clientErrors: Record<FieldPath, string>;
|
|
693
723
|
/** Validation errors set via `setServerErrors()`. Automatically cleared when the related field value changes. */
|
|
694
724
|
serverErrors: Record<FieldPath, string>;
|
|
725
|
+
/** Validation errors from field-level validators (both sync and async). Takes precedence over client errors but not server errors. */
|
|
726
|
+
fieldErrors: Record<FieldPath, string>;
|
|
695
727
|
/** Whether the form is currently being submitted. Useful for showing loading states on submit buttons. */
|
|
696
728
|
isSubmitting: boolean;
|
|
697
729
|
/** Whether any async validation (form-level or field-level) is currently in progress. */
|
package/dist/index.mjs
CHANGED
|
@@ -128,6 +128,8 @@ var deepEqual = (a, b) => {
|
|
|
128
128
|
var formReducer = (state, action, initialValues) => {
|
|
129
129
|
switch (action.type) {
|
|
130
130
|
case "SET_FIELD_VALUE": {
|
|
131
|
+
if (getValueByPath(state.values, action.field) === action.value)
|
|
132
|
+
return state;
|
|
131
133
|
const newValues = setValueByPath(
|
|
132
134
|
state.values,
|
|
133
135
|
action.field,
|
|
@@ -286,7 +288,7 @@ function fromStandardSchema(schema) {
|
|
|
286
288
|
}
|
|
287
289
|
|
|
288
290
|
// src/arrayHelpersFactory.ts
|
|
289
|
-
var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSelectFieldProps, getSliderProps, getCheckboxProps, getSwitchProps, getFileFieldProps, getRadioGroupOptionProps) => {
|
|
291
|
+
var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSelectFieldProps, getSliderProps, getCheckboxProps, getSwitchProps, getFileFieldProps, getRadioGroupOptionProps, getFieldError, getFieldId) => {
|
|
290
292
|
return {
|
|
291
293
|
/**
|
|
292
294
|
* The current array of values for this field.
|
|
@@ -538,7 +540,9 @@ var createArrayHelpers = (field, arrayValue, setFieldValue, getFieldProps, getSe
|
|
|
538
540
|
getRadioGroupOptionProps: (index, subField, optionValue) => {
|
|
539
541
|
const path = `${field}[${index}].${subField}`;
|
|
540
542
|
return getRadioGroupOptionProps(path, optionValue);
|
|
541
|
-
}
|
|
543
|
+
},
|
|
544
|
+
getFieldError: (index, subField) => getFieldError(`${field}[${index}].${subField}`),
|
|
545
|
+
getFieldId: (index, subField, optionValue) => getFieldId(`${field}[${index}].${subField}`, optionValue)
|
|
542
546
|
};
|
|
543
547
|
};
|
|
544
548
|
|
|
@@ -693,6 +697,7 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
693
697
|
useEffect(() => {
|
|
694
698
|
formStateRef.current = formState;
|
|
695
699
|
});
|
|
700
|
+
const pendingValuesRef = useRef(null);
|
|
696
701
|
const onValuesChangeRef = useRef(onValuesChange);
|
|
697
702
|
useEffect(() => {
|
|
698
703
|
onValuesChangeRef.current = onValuesChange;
|
|
@@ -844,31 +849,28 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
844
849
|
}, [initialValues, validateForm, validateOnMount]);
|
|
845
850
|
const setFieldValue = useCallback(
|
|
846
851
|
(field, value, shouldValidate = validateOnChange) => {
|
|
852
|
+
const baseValues = pendingValuesRef.current ?? formStateRef.current.values;
|
|
847
853
|
const updatedValues = setValueByPath(
|
|
848
|
-
|
|
854
|
+
baseValues,
|
|
849
855
|
field,
|
|
850
856
|
value
|
|
851
857
|
);
|
|
858
|
+
pendingValuesRef.current = updatedValues;
|
|
852
859
|
onFieldChangeRef.current?.(field, value, updatedValues);
|
|
860
|
+
pendingValuesRef.current = null;
|
|
853
861
|
dispatch({
|
|
854
862
|
type: "SET_FIELD_VALUE",
|
|
855
863
|
field,
|
|
856
864
|
value
|
|
857
865
|
});
|
|
858
|
-
if (touchOnChange && !
|
|
866
|
+
if (touchOnChange && !formStateRef.current.touched[field]) {
|
|
859
867
|
dispatch({ type: "SET_FIELD_TOUCHED", field, isTouched: true });
|
|
860
868
|
}
|
|
861
869
|
if (shouldValidate) {
|
|
862
870
|
runValidationPipeline(field, updatedValues);
|
|
863
871
|
}
|
|
864
872
|
},
|
|
865
|
-
[
|
|
866
|
-
formState.values,
|
|
867
|
-
formState.touched,
|
|
868
|
-
validateOnChange,
|
|
869
|
-
touchOnChange,
|
|
870
|
-
runValidationPipeline
|
|
871
|
-
]
|
|
873
|
+
[validateOnChange, touchOnChange, runValidationPipeline]
|
|
872
874
|
);
|
|
873
875
|
const setValues = useCallback(
|
|
874
876
|
(newValues, shouldValidate = validateOnChange) => {
|
|
@@ -1169,6 +1171,8 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1169
1171
|
setFieldValue,
|
|
1170
1172
|
setFieldTouched
|
|
1171
1173
|
);
|
|
1174
|
+
const getFieldErrorAtPath = (path) => formState.touched[path] ? errors[path] : void 0;
|
|
1175
|
+
const getFieldIdAtPath = (path, optionValue) => optionValue !== void 0 ? `${path}-${String(optionValue)}` : path;
|
|
1172
1176
|
const arrayValue = getValueByPath(formState.values, field) || [];
|
|
1173
1177
|
return createArrayHelpers(
|
|
1174
1178
|
field,
|
|
@@ -1180,10 +1184,18 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1180
1184
|
getCheckboxPropsAtPath,
|
|
1181
1185
|
getSwitchPropsAtPath,
|
|
1182
1186
|
getFileFieldPropsAtPath,
|
|
1183
|
-
getRadioGroupOptionPropsAtPath
|
|
1187
|
+
getRadioGroupOptionPropsAtPath,
|
|
1188
|
+
getFieldErrorAtPath,
|
|
1189
|
+
getFieldIdAtPath
|
|
1184
1190
|
);
|
|
1185
1191
|
},
|
|
1186
|
-
[
|
|
1192
|
+
[
|
|
1193
|
+
formState.values,
|
|
1194
|
+
formState.touched,
|
|
1195
|
+
errors,
|
|
1196
|
+
setFieldValue,
|
|
1197
|
+
setFieldTouched
|
|
1198
|
+
]
|
|
1187
1199
|
);
|
|
1188
1200
|
const arrayHelpers = arrayHelpersImpl;
|
|
1189
1201
|
return {
|
|
@@ -1193,6 +1205,7 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1193
1205
|
errors,
|
|
1194
1206
|
clientErrors: formState.clientErrors,
|
|
1195
1207
|
serverErrors: formState.serverErrors,
|
|
1208
|
+
fieldErrors: formState.fieldErrors,
|
|
1196
1209
|
isSubmitting: formState.isSubmitting,
|
|
1197
1210
|
isValidating,
|
|
1198
1211
|
validatingFields: formState.validatingFields,
|