formstand 0.15.0 → 0.16.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/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-C9hZffCQ.cjs';
2
- export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, S as SettledFieldValidationResult, h as SettledFieldsValidationResult, i as SettledValidationResult, j as SubmitHandler, k as SubmitOptions, l as SubmitResult, V as ValidationMode, m as ValidationResult, n as ValidationTrigger, o as createForm, p as emptyValueForSchema, q as flattenIssues, r as isAsyncRequiredError, s as shouldValidateOn, v as validateAsync, t as validateSync } from './createForm-C9hZffCQ.cjs';
1
+ import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-jlzK2lCG.cjs';
2
+ export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, S as SettledFieldValidationResult, h as SettledFieldsValidationResult, i as SettledValidationResult, j as SubmitHandler, k as SubmitOptions, l as SubmitResult, V as ValidationMode, m as ValidationResult, n as ValidationTrigger, o as createForm, p as emptyValueForSchema, q as flattenIssues, r as isAsyncRequiredError, s as shouldValidateOn, v as validateAsync, t as validateSync } from './createForm-jlzK2lCG.cjs';
3
3
  import { z } from 'zod';
4
4
  import * as react from 'react';
5
5
  import { ReactNode, ChangeEvent } from 'react';
@@ -61,6 +61,7 @@ type UseFieldReturn<TValue> = Readonly<{
61
61
  initialValue: TValue;
62
62
  emptyValue: null | undefined;
63
63
  error: readonly string[] | undefined;
64
+ firstError: string | undefined;
64
65
  touched: boolean;
65
66
  dirty: boolean;
66
67
  isValidating: boolean;
@@ -92,7 +93,7 @@ type VariantFieldValue<V, TField extends string> = V extends unknown ? TField ex
92
93
  type CommonKeys<V> = keyof V;
93
94
  type AllKeys<V> = V extends unknown ? keyof V : never;
94
95
  type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
95
- type UnionValueAt<TValues, P extends string> = P extends keyof TValues ? NonNullable<TValues[P]> : P extends `${infer Head}.${infer Tail}` ? Head extends keyof TValues ? UnionValueAt<NonNullable<TValues[Head]>, Tail> : never : never;
96
+ type UnionValueAt<TValues, P extends string> = P extends keyof TValues ? NonNullable<TValues[P]> : TValues extends readonly (infer Item)[] ? P extends `${number}` ? NonNullable<Item> : P extends `${infer Head}.${infer Tail}` ? Head extends `${number}` ? UnionValueAt<NonNullable<Item>, Tail> : never : never : P extends `${infer Head}.${infer Tail}` ? Head extends keyof TValues ? UnionValueAt<NonNullable<TValues[Head]>, Tail> : never : never;
96
97
  declare const inferredCallsOnly$1: unique symbol;
97
98
  interface InferredTypeArg$1 {
98
99
  readonly [inferredCallsOnly$1]: typeof inferredCallsOnly$1;
@@ -212,6 +213,22 @@ declare function useIsValid<TSchema extends z.ZodType, P extends FieldPath<z.inp
212
213
  declare const useIsSubmitting: (form: FormStateApi) => boolean;
213
214
  declare const useSubmitCount: (form: FormStateApi) => number;
214
215
 
216
+ type UseFieldsReturn<TFields extends readonly unknown[]> = Readonly<{
217
+ fields: TFields;
218
+ error: readonly string[] | undefined;
219
+ firstError: string | undefined;
220
+ touched: boolean;
221
+ dirty: boolean;
222
+ isValidating: boolean;
223
+ onBlur: () => void;
224
+ }>;
225
+ declare function useFields(form: FieldFormApi & {
226
+ readonly schema?: undefined;
227
+ }, paths: readonly string[]): UseFieldsReturn<readonly UseFieldReturn<unknown>[]>;
228
+ declare function useFields<TSchema extends z.ZodType, const P extends readonly FieldPath<z.input<TSchema>, D>[], D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, paths: P): UseFieldsReturn<{
229
+ readonly [K in keyof P]: UseFieldReturn<FieldValue<z.input<TSchema>, P[K] & string>>;
230
+ }>;
231
+
215
232
  type PathsOf<F extends FieldFormApi> = F extends Form<infer TSchema extends z.ZodType, infer D extends PathDepth> ? FieldPath<z.input<TSchema>, D> : string;
216
233
  type FieldRef<T> = ((instance: T | null) => void) | Readonly<{
217
234
  current: T | null;
@@ -341,4 +358,4 @@ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFiel
341
358
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
342
359
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
343
360
 
344
- export { type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, DefaultPathDepth, ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, FieldPath, type FieldPathArg, type FieldRef, FieldValidationResult, FieldValue, Form, type FormContextApi, type FormHooks, type FormProviderProps, FormState, type FormStateApi, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, checkboxProps, createFormContext, createFormHooks, dateInputProps, dateToInputText, focusField, focusFirstError, getAtPath, hasFieldError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField };
361
+ export { type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, DefaultPathDepth, ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, FieldPath, type FieldPathArg, type FieldRef, FieldValidationResult, FieldValue, Form, type FormContextApi, type FormHooks, type FormProviderProps, FormState, type FormStateApi, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type UseFieldsReturn, checkboxProps, createFormContext, createFormHooks, dateInputProps, dateToInputText, focusField, focusFirstError, getAtPath, hasFieldError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, textInputProps, useField, useFieldArray, useFields, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-C9hZffCQ.js';
2
- export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, S as SettledFieldValidationResult, h as SettledFieldsValidationResult, i as SettledValidationResult, j as SubmitHandler, k as SubmitOptions, l as SubmitResult, V as ValidationMode, m as ValidationResult, n as ValidationTrigger, o as createForm, p as emptyValueForSchema, q as flattenIssues, r as isAsyncRequiredError, s as shouldValidateOn, v as validateAsync, t as validateSync } from './createForm-C9hZffCQ.js';
1
+ import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-jlzK2lCG.js';
2
+ export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, S as SettledFieldValidationResult, h as SettledFieldsValidationResult, i as SettledValidationResult, j as SubmitHandler, k as SubmitOptions, l as SubmitResult, V as ValidationMode, m as ValidationResult, n as ValidationTrigger, o as createForm, p as emptyValueForSchema, q as flattenIssues, r as isAsyncRequiredError, s as shouldValidateOn, v as validateAsync, t as validateSync } from './createForm-jlzK2lCG.js';
3
3
  import { z } from 'zod';
4
4
  import * as react from 'react';
5
5
  import { ReactNode, ChangeEvent } from 'react';
@@ -61,6 +61,7 @@ type UseFieldReturn<TValue> = Readonly<{
61
61
  initialValue: TValue;
62
62
  emptyValue: null | undefined;
63
63
  error: readonly string[] | undefined;
64
+ firstError: string | undefined;
64
65
  touched: boolean;
65
66
  dirty: boolean;
66
67
  isValidating: boolean;
@@ -92,7 +93,7 @@ type VariantFieldValue<V, TField extends string> = V extends unknown ? TField ex
92
93
  type CommonKeys<V> = keyof V;
93
94
  type AllKeys<V> = V extends unknown ? keyof V : never;
94
95
  type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
95
- type UnionValueAt<TValues, P extends string> = P extends keyof TValues ? NonNullable<TValues[P]> : P extends `${infer Head}.${infer Tail}` ? Head extends keyof TValues ? UnionValueAt<NonNullable<TValues[Head]>, Tail> : never : never;
96
+ type UnionValueAt<TValues, P extends string> = P extends keyof TValues ? NonNullable<TValues[P]> : TValues extends readonly (infer Item)[] ? P extends `${number}` ? NonNullable<Item> : P extends `${infer Head}.${infer Tail}` ? Head extends `${number}` ? UnionValueAt<NonNullable<Item>, Tail> : never : never : P extends `${infer Head}.${infer Tail}` ? Head extends keyof TValues ? UnionValueAt<NonNullable<TValues[Head]>, Tail> : never : never;
96
97
  declare const inferredCallsOnly$1: unique symbol;
97
98
  interface InferredTypeArg$1 {
98
99
  readonly [inferredCallsOnly$1]: typeof inferredCallsOnly$1;
@@ -212,6 +213,22 @@ declare function useIsValid<TSchema extends z.ZodType, P extends FieldPath<z.inp
212
213
  declare const useIsSubmitting: (form: FormStateApi) => boolean;
213
214
  declare const useSubmitCount: (form: FormStateApi) => number;
214
215
 
216
+ type UseFieldsReturn<TFields extends readonly unknown[]> = Readonly<{
217
+ fields: TFields;
218
+ error: readonly string[] | undefined;
219
+ firstError: string | undefined;
220
+ touched: boolean;
221
+ dirty: boolean;
222
+ isValidating: boolean;
223
+ onBlur: () => void;
224
+ }>;
225
+ declare function useFields(form: FieldFormApi & {
226
+ readonly schema?: undefined;
227
+ }, paths: readonly string[]): UseFieldsReturn<readonly UseFieldReturn<unknown>[]>;
228
+ declare function useFields<TSchema extends z.ZodType, const P extends readonly FieldPath<z.input<TSchema>, D>[], D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, paths: P): UseFieldsReturn<{
229
+ readonly [K in keyof P]: UseFieldReturn<FieldValue<z.input<TSchema>, P[K] & string>>;
230
+ }>;
231
+
215
232
  type PathsOf<F extends FieldFormApi> = F extends Form<infer TSchema extends z.ZodType, infer D extends PathDepth> ? FieldPath<z.input<TSchema>, D> : string;
216
233
  type FieldRef<T> = ((instance: T | null) => void) | Readonly<{
217
234
  current: T | null;
@@ -341,4 +358,4 @@ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFiel
341
358
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
342
359
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
343
360
 
344
- export { type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, DefaultPathDepth, ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, FieldPath, type FieldPathArg, type FieldRef, FieldValidationResult, FieldValue, Form, type FormContextApi, type FormHooks, type FormProviderProps, FormState, type FormStateApi, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, checkboxProps, createFormContext, createFormHooks, dateInputProps, dateToInputText, focusField, focusFirstError, getAtPath, hasFieldError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField };
361
+ export { type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, DefaultPathDepth, ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, FieldPath, type FieldPathArg, type FieldRef, FieldValidationResult, FieldValue, Form, type FormContextApi, type FormHooks, type FormProviderProps, FormState, type FormStateApi, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type UseFieldsReturn, checkboxProps, createFormContext, createFormHooks, dateInputProps, dateToInputText, focusField, focusFirstError, getAtPath, hasFieldError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, textInputProps, useField, useFieldArray, useFields, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField };
package/dist/index.js CHANGED
@@ -1256,6 +1256,8 @@ function useField(form, pathArg, options) {
1256
1256
  initialValue: slice.initialValue,
1257
1257
  emptyValue,
1258
1258
  error: slice.error,
1259
+ // `?.[0]` covers both the undefined channel and an empty array.
1260
+ firstError: slice.error?.[0],
1259
1261
  touched: slice.touched,
1260
1262
  dirty: slice.dirty,
1261
1263
  isValidating: slice.isValidating,
@@ -1529,6 +1531,30 @@ function useFormValues(form) {
1529
1531
  return useStore(form.store, (state) => state.values);
1530
1532
  }
1531
1533
 
1534
+ // src/react/useFields.ts
1535
+ function useFields(form, paths) {
1536
+ const fields = paths.map(
1537
+ (path) => (
1538
+ // eslint-disable-next-line react-hooks/rules-of-hooks -- a fixed hook count by the stable-arity contract above; each element must be a REAL UseFieldReturn so it spreads into the prop builders
1539
+ useField(form, path)
1540
+ )
1541
+ );
1542
+ const merged = [...new Set(fields.flatMap((field) => field.error ?? []))];
1543
+ return {
1544
+ fields,
1545
+ error: merged.length > 0 ? merged : void 0,
1546
+ firstError: merged[0],
1547
+ touched: fields.some((field) => field.touched),
1548
+ dirty: fields.some((field) => field.dirty),
1549
+ isValidating: fields.some((field) => field.isValidating),
1550
+ onBlur: () => {
1551
+ fields.forEach((field) => {
1552
+ field.onBlur();
1553
+ });
1554
+ }
1555
+ };
1556
+ }
1557
+
1532
1558
  // src/react/inputProps.ts
1533
1559
  var hasFieldError = (error) => error !== void 0 && error.length > 0;
1534
1560
  var ariaInvalid = (field) => hasFieldError(field.error) ? true : void 0;
@@ -1917,6 +1943,6 @@ var focusField = (path, root) => {
1917
1943
  return path === "" ? (root !== void 0 || document.querySelectorAll("form").length <= 1) && focusFirstOf(namedControls(scope).filter(isFocusCandidate)) : focusFirstOf(pathCandidates(scope, [path]).filter(isFocusCandidate));
1918
1944
  };
1919
1945
 
1920
- export { CheckboxField, DateField, NumberField, SelectField, TextField, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, persistForm, selectProps, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
1946
+ export { CheckboxField, DateField, NumberField, SelectField, TextField, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, persistForm, selectProps, shouldValidateOn, textInputProps, useField, useFieldArray, useFields, useForm, useFormError, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
1921
1947
  //# sourceMappingURL=index.js.map
1922
1948
  //# sourceMappingURL=index.js.map