@railway-ts/use-form 0.1.16 → 0.1.18
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 +12 -12
- package/dist/index.d.ts +207 -51
- package/dist/index.mjs +24 -8
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +79 -1
- package/docs/GETTING_STARTED.md +24 -26
- package/docs/RECIPES.md +49 -49
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,13 +66,13 @@ export function LoginForm() {
|
|
|
66
66
|
return (
|
|
67
67
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
68
68
|
<input type="email" {...form.getFieldProps('email')} />
|
|
69
|
-
{form.
|
|
70
|
-
<span>{form.
|
|
69
|
+
{form.getFieldError('email') && (
|
|
70
|
+
<span>{form.getFieldError('email')}</span>
|
|
71
71
|
)}
|
|
72
72
|
|
|
73
73
|
<input type="password" {...form.getFieldProps('password')} />
|
|
74
|
-
{form.
|
|
75
|
-
<span>{form.
|
|
74
|
+
{form.getFieldError('password') && (
|
|
75
|
+
<span>{form.getFieldError('password')}</span>
|
|
76
76
|
)}
|
|
77
77
|
|
|
78
78
|
<button type="submit" disabled={form.isSubmitting}>
|
|
@@ -193,23 +193,23 @@ export function RegistrationForm() {
|
|
|
193
193
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
194
194
|
<input {...form.getFieldProps('username')} />
|
|
195
195
|
{form.validatingFields.username && <span>Checking...</span>}
|
|
196
|
-
{form.
|
|
197
|
-
<span>{form.
|
|
196
|
+
{form.getFieldError('username') && (
|
|
197
|
+
<span>{form.getFieldError('username')}</span>
|
|
198
198
|
)}
|
|
199
199
|
|
|
200
200
|
<input type="email" {...form.getFieldProps('email')} />
|
|
201
|
-
{form.
|
|
202
|
-
<span>{form.
|
|
201
|
+
{form.getFieldError('email') && (
|
|
202
|
+
<span>{form.getFieldError('email')}</span>
|
|
203
203
|
)}
|
|
204
204
|
|
|
205
205
|
<input type="password" {...form.getFieldProps('password')} />
|
|
206
|
-
{form.
|
|
207
|
-
<span>{form.
|
|
206
|
+
{form.getFieldError('password') && (
|
|
207
|
+
<span>{form.getFieldError('password')}</span>
|
|
208
208
|
)}
|
|
209
209
|
|
|
210
210
|
<input type="password" {...form.getFieldProps('confirmPassword')} />
|
|
211
|
-
{form.
|
|
212
|
-
<span>{form.
|
|
211
|
+
{form.getFieldError('confirmPassword') && (
|
|
212
|
+
<span>{form.getFieldError('confirmPassword')}</span>
|
|
213
213
|
)}
|
|
214
214
|
|
|
215
215
|
{form.errors[ROOT_ERROR_KEY] && (
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
|
-
import { ChangeEvent, FormEvent } from 'react';
|
|
2
|
-
import { Result } from '@railway-ts/pipelines/result';
|
|
3
1
|
import { StandardSchemaV1, MaybeAsyncValidator, ValidationError } from '@railway-ts/pipelines/schema';
|
|
2
|
+
import { FormEvent, ChangeEvent } from 'react';
|
|
3
|
+
import { Result } from '@railway-ts/pipelines/result';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Detects whether a value is a Standard Schema v1 object by checking for
|
|
7
|
+
* the `~standard` property with `version === 1`.
|
|
8
|
+
*/
|
|
9
|
+
declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a Standard Schema v1 object into a `MaybeAsyncValidator` compatible
|
|
12
|
+
* with the railway-ts validation pipeline. This allows `useForm` to accept
|
|
13
|
+
* any Standard Schema v1 compliant validator (Valibot, ArkType, etc.).
|
|
14
|
+
*/
|
|
15
|
+
declare function fromStandardSchema<T extends Record<string, unknown>>(schema: StandardSchemaV1<unknown, T>): MaybeAsyncValidator<unknown, T>;
|
|
16
|
+
/**
|
|
17
|
+
* Accepts either a railway-ts `MaybeAsyncValidator` or a Standard Schema v1 object.
|
|
18
|
+
* Used as the validator parameter type for `useForm`.
|
|
19
|
+
*/
|
|
20
|
+
type FormValidator<T extends Record<string, unknown>> = MaybeAsyncValidator<unknown, T> | StandardSchemaV1<unknown, T>;
|
|
4
21
|
|
|
5
22
|
/**
|
|
6
23
|
* Represents a path to a field in a nested object structure.
|
|
@@ -656,23 +673,194 @@ interface ArrayHelpers<TItem, TFieldPaths extends string = ExtractFieldPaths<TIt
|
|
|
656
673
|
*/
|
|
657
674
|
getRadioGroupOptionProps: (index: number, subField: TFieldPaths, optionValue: string | number) => NativeRadioGroupOptionProps;
|
|
658
675
|
}
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Detects whether a value is a Standard Schema v1 object by checking for
|
|
662
|
-
* the `~standard` property with `version === 1`.
|
|
663
|
-
*/
|
|
664
|
-
declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
|
|
665
676
|
/**
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
* Accepts either a railway-ts `MaybeAsyncValidator` or a Standard Schema v1 object.
|
|
673
|
-
* Used as the validator parameter type for `useForm`.
|
|
677
|
+
* The return type of the `useForm` hook.
|
|
678
|
+
*
|
|
679
|
+
* Provides form state, field management methods, server error management,
|
|
680
|
+
* form actions, native HTML field integration, and array field helpers.
|
|
681
|
+
*
|
|
682
|
+
* @template TValues - The shape of form values as a record with string keys
|
|
674
683
|
*/
|
|
675
|
-
|
|
684
|
+
interface UseFormReturn<TValues extends Record<string, unknown>> {
|
|
685
|
+
/** Current values of all form fields. */
|
|
686
|
+
values: TValues;
|
|
687
|
+
/** Record of which fields have been interacted with by the user. */
|
|
688
|
+
touched: Record<FieldPath, boolean>;
|
|
689
|
+
/** Merged validation errors (server errors take precedence over field errors, which take precedence over client errors). */
|
|
690
|
+
errors: Record<FieldPath, string>;
|
|
691
|
+
/** Validation errors from client-side (schema) validation only. */
|
|
692
|
+
clientErrors: Record<FieldPath, string>;
|
|
693
|
+
/** Validation errors set via `setServerErrors()`. Automatically cleared when the related field value changes. */
|
|
694
|
+
serverErrors: Record<FieldPath, string>;
|
|
695
|
+
/** Whether the form is currently being submitted. Useful for showing loading states on submit buttons. */
|
|
696
|
+
isSubmitting: boolean;
|
|
697
|
+
/** Whether any async validation (form-level or field-level) is currently in progress. */
|
|
698
|
+
isValidating: boolean;
|
|
699
|
+
/** Per-field async validation tracking. `true` when that field's async validator is running. */
|
|
700
|
+
validatingFields: Record<FieldPath, boolean>;
|
|
701
|
+
/** Whether the form is currently valid (no errors of any kind). */
|
|
702
|
+
isValid: boolean;
|
|
703
|
+
/** Whether any form values have changed since initialization or last reset. */
|
|
704
|
+
isDirty: boolean;
|
|
705
|
+
/** Number of times the form has been submitted via `handleSubmit`. Incremented at the start of each attempt. */
|
|
706
|
+
submitCount: number;
|
|
707
|
+
/**
|
|
708
|
+
* Updates a single field value and optionally triggers validation. Automatically clears related server errors.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* form.setFieldValue("email", "user@example.com");
|
|
712
|
+
*/
|
|
713
|
+
setFieldValue: <TValue>(field: FieldPath, value: TValue, shouldValidate?: boolean) => void;
|
|
714
|
+
/**
|
|
715
|
+
* Marks a field as touched or untouched and optionally triggers validation.
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* form.setFieldTouched("email");
|
|
719
|
+
*/
|
|
720
|
+
setFieldTouched: (field: FieldPath, isTouched?: boolean, shouldValidate?: boolean) => void;
|
|
721
|
+
/**
|
|
722
|
+
* Updates multiple field values simultaneously. More efficient than calling `setFieldValue` multiple times.
|
|
723
|
+
*
|
|
724
|
+
* @example
|
|
725
|
+
* form.setValues({ firstName: "John", lastName: "Doe" });
|
|
726
|
+
*/
|
|
727
|
+
setValues: (values: DeepPartial<TValues>, shouldValidate?: boolean) => void;
|
|
728
|
+
/**
|
|
729
|
+
* Sets server-side validation errors. These take precedence over client errors and are auto-cleared on field change.
|
|
730
|
+
*
|
|
731
|
+
* @example
|
|
732
|
+
* form.setServerErrors({ email: "Email already taken" });
|
|
733
|
+
*/
|
|
734
|
+
setServerErrors: (errors: Record<FieldPath, string>) => void;
|
|
735
|
+
/**
|
|
736
|
+
* Clears all server-side validation errors. Client-side errors are not affected.
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* form.clearServerErrors();
|
|
740
|
+
*/
|
|
741
|
+
clearServerErrors: () => void;
|
|
742
|
+
/**
|
|
743
|
+
* Validates all fields, marks them as touched, and calls `onSubmit` if valid. Returns a Railway `Result`.
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* <form onSubmit={form.handleSubmit}>
|
|
747
|
+
* <input {...form.getFieldProps("email")} />
|
|
748
|
+
* <button type="submit">Submit</button>
|
|
749
|
+
* </form>
|
|
750
|
+
*/
|
|
751
|
+
handleSubmit: (e?: FormEvent) => Promise<Result<TValues, ValidationError[]>>;
|
|
752
|
+
/**
|
|
753
|
+
* Resets the form to its initial state — clears all values, errors, and touched state.
|
|
754
|
+
*
|
|
755
|
+
* @example
|
|
756
|
+
* form.resetForm();
|
|
757
|
+
*/
|
|
758
|
+
resetForm: () => void;
|
|
759
|
+
/**
|
|
760
|
+
* Runs the validator against the given values and returns a Railway `Result`.
|
|
761
|
+
*
|
|
762
|
+
* @example
|
|
763
|
+
* const result = form.validateForm(currentValues);
|
|
764
|
+
*/
|
|
765
|
+
validateForm: (values: TValues) => Result<TValues, ValidationError[]> | Promise<Result<TValues, ValidationError[]>>;
|
|
766
|
+
/**
|
|
767
|
+
* Returns the error message for a field only if it has been touched, otherwise `undefined`.
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* {form.getFieldError("email") && <span>{form.getFieldError("email")}</span>}
|
|
771
|
+
*/
|
|
772
|
+
getFieldError: <TField extends ExtractFieldPaths<TValues>>(field: TField) => string | undefined;
|
|
773
|
+
/**
|
|
774
|
+
* Returns a stable HTML element ID for a field. Useful for linking `<label htmlFor>` to inputs.
|
|
775
|
+
*
|
|
776
|
+
* @example
|
|
777
|
+
* <label htmlFor={form.getFieldId("username")}>Username</label>
|
|
778
|
+
* <input {...form.getFieldProps("username")} />
|
|
779
|
+
*/
|
|
780
|
+
getFieldId: <TField extends ExtractFieldPaths<TValues>>(field: TField, optionValue?: string | number) => string;
|
|
781
|
+
/**
|
|
782
|
+
* Returns props (`id`, `name`, `value`, `onChange`, `onBlur`) to spread onto a native text input or textarea.
|
|
783
|
+
*
|
|
784
|
+
* @example
|
|
785
|
+
* <input type="text" {...form.getFieldProps("username")} placeholder="Username" />
|
|
786
|
+
*/
|
|
787
|
+
getFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeFieldProps;
|
|
788
|
+
/**
|
|
789
|
+
* Returns props to spread onto a native `<select>` element.
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* <select {...form.getSelectFieldProps("country")}>
|
|
793
|
+
* <option value="">Select a country</option>
|
|
794
|
+
* <option value="US">United States</option>
|
|
795
|
+
* </select>
|
|
796
|
+
*/
|
|
797
|
+
getSelectFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSelectProps;
|
|
798
|
+
/**
|
|
799
|
+
* Returns props (`id`, `name`, `checked`, `onChange`, `onBlur`) to spread onto a native checkbox for a boolean field.
|
|
800
|
+
*
|
|
801
|
+
* @example
|
|
802
|
+
* <label>
|
|
803
|
+
* <input type="checkbox" {...form.getCheckboxProps("acceptTerms")} />
|
|
804
|
+
* I accept the terms and conditions
|
|
805
|
+
* </label>
|
|
806
|
+
*/
|
|
807
|
+
getCheckboxProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeCheckboxProps;
|
|
808
|
+
/**
|
|
809
|
+
* Returns props to spread onto a checkbox styled as a toggle switch for a boolean field.
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* <label className="switch">
|
|
813
|
+
* <input type="checkbox" {...form.getSwitchProps("darkMode")} />
|
|
814
|
+
* <span className="slider" />
|
|
815
|
+
* </label>
|
|
816
|
+
*/
|
|
817
|
+
getSwitchProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSwitchProps;
|
|
818
|
+
/**
|
|
819
|
+
* Returns props to spread onto a native `<input type="range">` for a numeric field.
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* <input type="range" min={0} max={100} {...form.getSliderProps("volume")} />
|
|
823
|
+
*/
|
|
824
|
+
getSliderProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSliderProps;
|
|
825
|
+
/**
|
|
826
|
+
* Returns props for a single checkbox within a checkbox group bound to an array field.
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* <label>
|
|
830
|
+
* <input type="checkbox" {...form.getCheckboxGroupOptionProps("interests", "sports")} />
|
|
831
|
+
* Sports
|
|
832
|
+
* </label>
|
|
833
|
+
*/
|
|
834
|
+
getCheckboxGroupOptionProps: <TField extends ExtractFieldPaths<TValues>>(field: TField, optionValue: string | number) => NativeCheckboxGroupOptionProps;
|
|
835
|
+
/**
|
|
836
|
+
* Returns props to spread onto a native `<input type="file">`. Supports single and multiple file selection.
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* <input type="file" accept="image/*" {...form.getFileFieldProps("avatar")} />
|
|
840
|
+
*/
|
|
841
|
+
getFileFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeFileFieldProps;
|
|
842
|
+
/**
|
|
843
|
+
* Returns props for a single radio button within a radio group bound to a scalar field.
|
|
844
|
+
*
|
|
845
|
+
* @example
|
|
846
|
+
* <label>
|
|
847
|
+
* <input type="radio" {...form.getRadioGroupOptionProps("contactMethod", "email")} />
|
|
848
|
+
* Email
|
|
849
|
+
* </label>
|
|
850
|
+
*/
|
|
851
|
+
getRadioGroupOptionProps: <TField extends ExtractFieldPaths<TValues>>(field: TField, optionValue: string | number) => NativeRadioGroupOptionProps;
|
|
852
|
+
/**
|
|
853
|
+
* Returns helper functions for managing array fields — add, remove, reorder, and bind HTML elements to items.
|
|
854
|
+
*
|
|
855
|
+
* @example
|
|
856
|
+
* const helpers = form.arrayHelpers("contacts");
|
|
857
|
+
* helpers.push({ name: "", email: "" });
|
|
858
|
+
*/
|
|
859
|
+
arrayHelpers: {
|
|
860
|
+
<TField extends keyof TValues & string>(field: TField): ArrayHelpers<GetArrayItemType<TValues, TField>, ExtractFieldPaths<GetArrayItemType<TValues, TField>>>;
|
|
861
|
+
<TField extends ExtractFieldPaths<TValues>, TItem = unknown>(field: TField): ArrayHelpers<TItem, ExtractFieldPaths<TItem>>;
|
|
862
|
+
};
|
|
863
|
+
}
|
|
676
864
|
|
|
677
865
|
/**
|
|
678
866
|
* A powerful React form hook that combines Railway-oriented validation with native HTML support.
|
|
@@ -788,39 +976,7 @@ type FormValidator<T extends Record<string, unknown>> = MaybeAsyncValidator<unkn
|
|
|
788
976
|
* </div>
|
|
789
977
|
* );
|
|
790
978
|
*/
|
|
791
|
-
declare const useForm: <TValues extends Record<string, unknown>>(validatorOrSchema: FormValidator<TValues>, options: FormOptions<TValues>) =>
|
|
792
|
-
values: TValues;
|
|
793
|
-
touched: Record<string, boolean>;
|
|
794
|
-
errors: Record<string, string>;
|
|
795
|
-
clientErrors: Record<string, string>;
|
|
796
|
-
serverErrors: Record<string, string>;
|
|
797
|
-
isSubmitting: boolean;
|
|
798
|
-
isValidating: boolean;
|
|
799
|
-
validatingFields: Record<string, boolean>;
|
|
800
|
-
isValid: boolean;
|
|
801
|
-
isDirty: boolean;
|
|
802
|
-
submitCount: number;
|
|
803
|
-
setFieldValue: <TValue>(field: FieldPath, value: TValue, shouldValidate?: boolean) => void;
|
|
804
|
-
setFieldTouched: (field: FieldPath, isTouched?: boolean, shouldValidate?: boolean) => void;
|
|
805
|
-
setValues: (newValues: DeepPartial<TValues>, shouldValidate?: boolean) => void;
|
|
806
|
-
setServerErrors: (serverErrors: Record<FieldPath, string>) => void;
|
|
807
|
-
clearServerErrors: () => void;
|
|
808
|
-
handleSubmit: (e?: FormEvent) => Promise<Result<TValues, ValidationError[]>>;
|
|
809
|
-
resetForm: () => void;
|
|
810
|
-
validateForm: (values: TValues) => Result<TValues, ValidationError[]> | Promise<Result<TValues, ValidationError[]>>;
|
|
811
|
-
getFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeFieldProps;
|
|
812
|
-
getSelectFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSelectProps;
|
|
813
|
-
getCheckboxProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeCheckboxProps;
|
|
814
|
-
getSwitchProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSwitchProps;
|
|
815
|
-
getSliderProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeSliderProps;
|
|
816
|
-
getCheckboxGroupOptionProps: <TField extends ExtractFieldPaths<TValues>>(field: TField, optionValue: string | number) => NativeCheckboxGroupOptionProps;
|
|
817
|
-
getFileFieldProps: <TField extends ExtractFieldPaths<TValues>>(field: TField) => NativeFileFieldProps;
|
|
818
|
-
getRadioGroupOptionProps: <TField extends ExtractFieldPaths<TValues>>(field: TField, optionValue: string | number) => NativeRadioGroupOptionProps;
|
|
819
|
-
arrayHelpers: {
|
|
820
|
-
<TField extends keyof TValues & string>(field: TField): ArrayHelpers<GetArrayItemType<TValues, TField>, ExtractFieldPaths<GetArrayItemType<TValues, TField>>>;
|
|
821
|
-
<TField extends ExtractFieldPaths<TValues>, TItem = unknown>(field: TField): ArrayHelpers<TItem, ExtractFieldPaths<TItem>>;
|
|
822
|
-
};
|
|
823
|
-
};
|
|
979
|
+
declare const useForm: <TValues extends Record<string, unknown>>(validatorOrSchema: FormValidator<TValues>, options: FormOptions<TValues>) => UseFormReturn<TValues>;
|
|
824
980
|
|
|
825
981
|
/**
|
|
826
982
|
* Represents the subset of form methods needed for auto-submission
|
|
@@ -906,4 +1062,4 @@ declare const useFormAutoSubmission: <T>(form: FormWithAutoSubmit<T>, delay?: nu
|
|
|
906
1062
|
*/
|
|
907
1063
|
declare function useDebounce<T extends (...args: Params) => Return, Params extends unknown[] = Parameters<T>, Return = ReturnType<T>>(callback: T, delay: number): (...args: Params) => void;
|
|
908
1064
|
|
|
909
|
-
export { type FormValidator, fromStandardSchema, isStandardSchema, useDebounce, useForm, useFormAutoSubmission };
|
|
1065
|
+
export { type FormValidator, type UseFormReturn, fromStandardSchema, isStandardSchema, useDebounce, useForm, useFormAutoSubmission };
|
package/dist/index.mjs
CHANGED
|
@@ -547,7 +547,7 @@ var createNativeFieldProps = (field, formValues, handleChange, handleBlur) => {
|
|
|
547
547
|
const raw = getValueByPath(formValues, field);
|
|
548
548
|
const value = raw == null ? "" : typeof raw === "string" || typeof raw === "number" ? raw : raw instanceof Date ? isNaN(raw.getTime()) ? "" : raw.toISOString().split("T")[0] : JSON.stringify(raw);
|
|
549
549
|
return {
|
|
550
|
-
id:
|
|
550
|
+
id: field,
|
|
551
551
|
name: field,
|
|
552
552
|
value,
|
|
553
553
|
onChange: (e) => {
|
|
@@ -560,7 +560,7 @@ var createNativeSelectFieldProps = (field, formValues, handleChange, handleBlur)
|
|
|
560
560
|
const raw = getValueByPath(formValues, field);
|
|
561
561
|
const value = raw == null ? "" : typeof raw === "string" || typeof raw === "number" ? raw : JSON.stringify(raw);
|
|
562
562
|
return {
|
|
563
|
-
id:
|
|
563
|
+
id: field,
|
|
564
564
|
name: field,
|
|
565
565
|
value,
|
|
566
566
|
onChange: (e) => handleChange(field, e.target.value),
|
|
@@ -570,7 +570,7 @@ var createNativeSelectFieldProps = (field, formValues, handleChange, handleBlur)
|
|
|
570
570
|
var createNativeCheckboxProps = (field, formValues, handleChange, handleBlur) => {
|
|
571
571
|
const value = getValueByPath(formValues, field);
|
|
572
572
|
return {
|
|
573
|
-
id:
|
|
573
|
+
id: field,
|
|
574
574
|
name: field,
|
|
575
575
|
checked: !!value,
|
|
576
576
|
onChange: (e) => handleChange(field, e.target.checked),
|
|
@@ -580,7 +580,7 @@ var createNativeCheckboxProps = (field, formValues, handleChange, handleBlur) =>
|
|
|
580
580
|
var createNativeSwitchProps = (field, formValues, handleChange, handleBlur) => {
|
|
581
581
|
const value = getValueByPath(formValues, field);
|
|
582
582
|
return {
|
|
583
|
-
id:
|
|
583
|
+
id: field,
|
|
584
584
|
name: field,
|
|
585
585
|
checked: !!value,
|
|
586
586
|
onChange: (e) => handleChange(field, e.target.checked),
|
|
@@ -592,7 +592,7 @@ var createNativeSliderProps = (field, formValues, handleChange, handleBlur) => {
|
|
|
592
592
|
const toNumber = (v) => typeof v === "string" ? Number(v || 0) : typeof v === "number" ? v : 0;
|
|
593
593
|
const value = Array.isArray(raw) ? toNumber(raw[0]) : toNumber(raw);
|
|
594
594
|
return {
|
|
595
|
-
id:
|
|
595
|
+
id: field,
|
|
596
596
|
name: field,
|
|
597
597
|
type: "range",
|
|
598
598
|
value,
|
|
@@ -605,7 +605,7 @@ var createCheckboxGroupOptionProps = (field, optionValue, formValues, handleChan
|
|
|
605
605
|
const current = Array.isArray(raw) ? raw : [];
|
|
606
606
|
const isChecked = current.some((v) => String(v) === String(optionValue));
|
|
607
607
|
return {
|
|
608
|
-
id:
|
|
608
|
+
id: `${field}-${String(optionValue)}`,
|
|
609
609
|
name: field,
|
|
610
610
|
value: optionValue,
|
|
611
611
|
checked: isChecked,
|
|
@@ -624,7 +624,7 @@ var createCheckboxGroupOptionProps = (field, optionValue, formValues, handleChan
|
|
|
624
624
|
};
|
|
625
625
|
var createNativeFileFieldProps = (field, _formValues, handleChange, handleBlur) => {
|
|
626
626
|
return {
|
|
627
|
-
id:
|
|
627
|
+
id: field,
|
|
628
628
|
name: field,
|
|
629
629
|
onChange: (e) => {
|
|
630
630
|
const input = e.target;
|
|
@@ -642,7 +642,7 @@ var createRadioGroupOptionProps = (field, optionValue, formValues, handleChange,
|
|
|
642
642
|
const current = getValueByPath(formValues, field);
|
|
643
643
|
const checked = String(current) === String(optionValue);
|
|
644
644
|
return {
|
|
645
|
-
id:
|
|
645
|
+
id: `${field}-${String(optionValue)}`,
|
|
646
646
|
name: field,
|
|
647
647
|
value: optionValue,
|
|
648
648
|
checked,
|
|
@@ -1022,6 +1022,18 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1022
1022
|
void validateForm(initialValues);
|
|
1023
1023
|
}
|
|
1024
1024
|
}, [initialValues, validateForm, validateOnMount]);
|
|
1025
|
+
const getFieldError = useCallback(
|
|
1026
|
+
(field) => {
|
|
1027
|
+
return formState.touched[field] ? errors[field] : void 0;
|
|
1028
|
+
},
|
|
1029
|
+
[formState.touched, errors]
|
|
1030
|
+
);
|
|
1031
|
+
const getFieldId = useCallback(
|
|
1032
|
+
(field, optionValue) => {
|
|
1033
|
+
return optionValue !== void 0 ? `${field}-${String(optionValue)}` : field;
|
|
1034
|
+
},
|
|
1035
|
+
[]
|
|
1036
|
+
);
|
|
1025
1037
|
const getFieldProps = useCallback(
|
|
1026
1038
|
(field) => {
|
|
1027
1039
|
return createNativeFieldProps(
|
|
@@ -1187,6 +1199,10 @@ var useForm = (validatorOrSchema, options) => {
|
|
|
1187
1199
|
isValid,
|
|
1188
1200
|
isDirty: formState.isDirty,
|
|
1189
1201
|
submitCount: formState.submitCount,
|
|
1202
|
+
// Error helper
|
|
1203
|
+
getFieldError,
|
|
1204
|
+
// ID helper
|
|
1205
|
+
getFieldId,
|
|
1190
1206
|
// Field management
|
|
1191
1207
|
setFieldValue,
|
|
1192
1208
|
setFieldTouched,
|