formstand 0.7.0 → 0.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/index.d.cts CHANGED
@@ -67,6 +67,7 @@ type CreateFormOptions<TSchema extends z.ZodType> = Readonly<{
67
67
  mode?: ValidationMode;
68
68
  reValidateMode?: ValidationMode;
69
69
  validateOnMount?: boolean;
70
+ devtools?: boolean | string;
70
71
  }>;
71
72
  type SubmitHandler<TSchema extends z.ZodType> = (data: z.output<TSchema>) => void | Promise<void>;
72
73
  type InvalidSubmitHandler = (errors: ErrorMap) => void;
@@ -149,6 +150,24 @@ declare const parsePath: (path: string) => readonly PathSegment[];
149
150
  declare const getAtPath: (obj: unknown, path: string) => unknown;
150
151
  declare const setAtPath: <T>(obj: T, path: string, value: unknown) => T;
151
152
 
153
+ type PersistStorage = Readonly<{
154
+ getItem: (key: string) => string | null;
155
+ setItem: (key: string, value: string) => void;
156
+ removeItem: (key: string) => void;
157
+ }>;
158
+ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
159
+ key: string;
160
+ storage?: PersistStorage;
161
+ debounceMs?: number;
162
+ apply?: "adopt" | "restore" | "manual";
163
+ }>;
164
+ type PersistHandle = Readonly<{
165
+ restore: () => boolean;
166
+ clear: () => void;
167
+ dispose: () => void;
168
+ }>;
169
+ declare const persistForm: <TSchema extends z.ZodType>(form: Form<TSchema>, options: PersistOptions<TSchema>) => PersistHandle;
170
+
152
171
  declare const useForm: <TSchema extends z.ZodType>(schema: TSchema, options: CreateFormOptions<TSchema>) => Form<TSchema>;
153
172
 
154
173
  type FormProviderProps<TSchema extends z.ZodType> = Readonly<{
@@ -198,6 +217,16 @@ declare function useField<TValue = unknown>(form: FieldFormApi & {
198
217
  }, path: string | ((state: FormState<unknown>) => string), options?: UseFieldOptions): UseFieldReturn<TValue>;
199
218
  declare function useField<TSchema extends z.ZodType, P extends FieldPath<z.input<TSchema>>>(form: Form<TSchema>, path: P, options?: UseFieldOptions): UseFieldReturn<FieldValue<z.input<TSchema>, P>>;
200
219
 
220
+ type VariantFieldValue<V, TField extends string> = V extends unknown ? TField extends keyof V ? V[TField] : never : never;
221
+ type CommonKeys<V> = keyof V;
222
+ type AllKeys<V> = V extends unknown ? keyof V : never;
223
+ type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
224
+ 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;
225
+ declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>>(form: Form<TSchema>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
226
+ declare function useVariantField<TValue = unknown>(form: FieldFormApi & {
227
+ readonly schema?: undefined;
228
+ }, unionPath: string, field: string): UseFieldReturn<TValue | undefined>;
229
+
201
230
  type ReadonlyStore$1<T> = Pick<StoreApi<T>, "getState" | "getInitialState" | "subscribe">;
202
231
  type FieldArrayFormApi = Readonly<{
203
232
  store: ReadonlyStore$1<FormState<unknown>>;
@@ -233,6 +262,7 @@ type BoundUseField<TSchema extends z.ZodType> = {
233
262
  (pathSelector: (state: FormState<z.input<TSchema>>) => string, options?: UseFieldOptions): UseFieldReturn<unknown>;
234
263
  <P extends FieldPath<z.input<TSchema>>>(path: P, options?: UseFieldOptions): UseFieldReturn<FieldValue<z.input<TSchema>, P>>;
235
264
  };
265
+ type BoundUseVariantField<TSchema extends z.ZodType> = <P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>>(unionPath: P, field: TField) => UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
236
266
  type BoundUseFieldArray<TSchema extends z.ZodType> = {
237
267
  (pathSelector: (state: FormState<z.input<TSchema>>) => string): UseFieldArrayReturn<unknown>;
238
268
  <P extends FieldPath<z.input<TSchema>>>(path: P): UseFieldArrayReturn<ArrayItemOf<FieldValue<z.input<TSchema>, P>>>;
@@ -241,6 +271,8 @@ type BoundUseSelector<TSchema extends z.ZodType> = <U>(selector: (state: FormSta
241
271
  type BoundUseFlag<TSchema extends z.ZodType> = (path?: FieldPath<z.input<TSchema>>) => boolean;
242
272
  type FormHooks<TSchema extends z.ZodType, N extends string> = Readonly<{
243
273
  [K in `use${Capitalize<N>}Field`]: BoundUseField<TSchema>;
274
+ } & {
275
+ [K in `use${Capitalize<N>}VariantField`]: BoundUseVariantField<TSchema>;
244
276
  } & {
245
277
  [K in `use${Capitalize<N>}FieldArray`]: BoundUseFieldArray<TSchema>;
246
278
  } & {
@@ -300,6 +332,15 @@ type TextFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
300
332
  ref?: FieldRef<HTMLInputElement>;
301
333
  }>;
302
334
  declare const TextField: <F extends FieldFormApi>({ form, path, label, placeholder, type, autoComplete, ref, }: TextFieldProps<F>) => react.JSX.Element;
335
+ type DateFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
336
+ form: F;
337
+ path: PathsOf<F>;
338
+ label?: ReactNode;
339
+ min?: string;
340
+ max?: string;
341
+ ref?: FieldRef<HTMLInputElement>;
342
+ }>;
343
+ declare const DateField: <F extends FieldFormApi>({ form, path, label, min, max, ref, }: DateFieldProps<F>) => react.JSX.Element;
303
344
  type NumberFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
304
345
  form: F;
305
346
  path: PathsOf<F>;
@@ -347,6 +388,14 @@ type NumberInputProps = Readonly<{
347
388
  onChange: (e: ChangeEvent<HTMLInputElement>) => void;
348
389
  onBlur: () => void;
349
390
  }>;
391
+ type DateInputProps = Readonly<{
392
+ type: "date";
393
+ name: string;
394
+ value: string;
395
+ "aria-invalid": true | undefined;
396
+ onChange: (e: ChangeEvent<HTMLInputElement>) => void;
397
+ onBlur: () => void;
398
+ }>;
350
399
  type CheckboxProps = Readonly<{
351
400
  type: "checkbox";
352
401
  name: string;
@@ -374,7 +423,18 @@ type ParsedNumberText = Readonly<{
374
423
  declare const parseNumberText: (text: string) => ParsedNumberText;
375
424
  declare const textInputProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => TextInputProps;
376
425
  declare const numberInputProps: <T extends number | null | undefined>(field: UseFieldReturn<T>) => NumberInputProps;
426
+ declare const dateToInputText: (value: Date | null | undefined) => string;
427
+ type ParsedDateText = Readonly<{
428
+ kind: "empty";
429
+ }> | Readonly<{
430
+ kind: "date";
431
+ value: Date;
432
+ }> | Readonly<{
433
+ kind: "invalid";
434
+ }>;
435
+ declare const parseDateText: (text: string) => ParsedDateText;
436
+ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFieldReturn<T>) => DateInputProps;
377
437
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
378
438
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
379
439
 
380
- export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputProps, type ParsedNumberText, type PathSegment, type PathsOf, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledValidationResult, type SubmitHandler, type SubmitOptions, type SubmitResult, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type ValidationMode, type ValidationResult, type ValidationTrigger, checkboxProps, createForm, createFormContext, createFormHooks, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, isAsyncRequiredError, numberInputProps, numberToInputText, parseNumberText, parsePath, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useSubmitCount, validateAsync, validateSync };
440
+ export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledValidationResult, type SubmitHandler, type SubmitOptions, type SubmitResult, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type ValidationMode, type ValidationResult, type ValidationTrigger, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useSubmitCount, useVariantField, validateAsync, validateSync };
package/dist/index.d.ts CHANGED
@@ -67,6 +67,7 @@ type CreateFormOptions<TSchema extends z.ZodType> = Readonly<{
67
67
  mode?: ValidationMode;
68
68
  reValidateMode?: ValidationMode;
69
69
  validateOnMount?: boolean;
70
+ devtools?: boolean | string;
70
71
  }>;
71
72
  type SubmitHandler<TSchema extends z.ZodType> = (data: z.output<TSchema>) => void | Promise<void>;
72
73
  type InvalidSubmitHandler = (errors: ErrorMap) => void;
@@ -149,6 +150,24 @@ declare const parsePath: (path: string) => readonly PathSegment[];
149
150
  declare const getAtPath: (obj: unknown, path: string) => unknown;
150
151
  declare const setAtPath: <T>(obj: T, path: string, value: unknown) => T;
151
152
 
153
+ type PersistStorage = Readonly<{
154
+ getItem: (key: string) => string | null;
155
+ setItem: (key: string, value: string) => void;
156
+ removeItem: (key: string) => void;
157
+ }>;
158
+ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
159
+ key: string;
160
+ storage?: PersistStorage;
161
+ debounceMs?: number;
162
+ apply?: "adopt" | "restore" | "manual";
163
+ }>;
164
+ type PersistHandle = Readonly<{
165
+ restore: () => boolean;
166
+ clear: () => void;
167
+ dispose: () => void;
168
+ }>;
169
+ declare const persistForm: <TSchema extends z.ZodType>(form: Form<TSchema>, options: PersistOptions<TSchema>) => PersistHandle;
170
+
152
171
  declare const useForm: <TSchema extends z.ZodType>(schema: TSchema, options: CreateFormOptions<TSchema>) => Form<TSchema>;
153
172
 
154
173
  type FormProviderProps<TSchema extends z.ZodType> = Readonly<{
@@ -198,6 +217,16 @@ declare function useField<TValue = unknown>(form: FieldFormApi & {
198
217
  }, path: string | ((state: FormState<unknown>) => string), options?: UseFieldOptions): UseFieldReturn<TValue>;
199
218
  declare function useField<TSchema extends z.ZodType, P extends FieldPath<z.input<TSchema>>>(form: Form<TSchema>, path: P, options?: UseFieldOptions): UseFieldReturn<FieldValue<z.input<TSchema>, P>>;
200
219
 
220
+ type VariantFieldValue<V, TField extends string> = V extends unknown ? TField extends keyof V ? V[TField] : never : never;
221
+ type CommonKeys<V> = keyof V;
222
+ type AllKeys<V> = V extends unknown ? keyof V : never;
223
+ type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
224
+ 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;
225
+ declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>>(form: Form<TSchema>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
226
+ declare function useVariantField<TValue = unknown>(form: FieldFormApi & {
227
+ readonly schema?: undefined;
228
+ }, unionPath: string, field: string): UseFieldReturn<TValue | undefined>;
229
+
201
230
  type ReadonlyStore$1<T> = Pick<StoreApi<T>, "getState" | "getInitialState" | "subscribe">;
202
231
  type FieldArrayFormApi = Readonly<{
203
232
  store: ReadonlyStore$1<FormState<unknown>>;
@@ -233,6 +262,7 @@ type BoundUseField<TSchema extends z.ZodType> = {
233
262
  (pathSelector: (state: FormState<z.input<TSchema>>) => string, options?: UseFieldOptions): UseFieldReturn<unknown>;
234
263
  <P extends FieldPath<z.input<TSchema>>>(path: P, options?: UseFieldOptions): UseFieldReturn<FieldValue<z.input<TSchema>, P>>;
235
264
  };
265
+ type BoundUseVariantField<TSchema extends z.ZodType> = <P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>>(unionPath: P, field: TField) => UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
236
266
  type BoundUseFieldArray<TSchema extends z.ZodType> = {
237
267
  (pathSelector: (state: FormState<z.input<TSchema>>) => string): UseFieldArrayReturn<unknown>;
238
268
  <P extends FieldPath<z.input<TSchema>>>(path: P): UseFieldArrayReturn<ArrayItemOf<FieldValue<z.input<TSchema>, P>>>;
@@ -241,6 +271,8 @@ type BoundUseSelector<TSchema extends z.ZodType> = <U>(selector: (state: FormSta
241
271
  type BoundUseFlag<TSchema extends z.ZodType> = (path?: FieldPath<z.input<TSchema>>) => boolean;
242
272
  type FormHooks<TSchema extends z.ZodType, N extends string> = Readonly<{
243
273
  [K in `use${Capitalize<N>}Field`]: BoundUseField<TSchema>;
274
+ } & {
275
+ [K in `use${Capitalize<N>}VariantField`]: BoundUseVariantField<TSchema>;
244
276
  } & {
245
277
  [K in `use${Capitalize<N>}FieldArray`]: BoundUseFieldArray<TSchema>;
246
278
  } & {
@@ -300,6 +332,15 @@ type TextFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
300
332
  ref?: FieldRef<HTMLInputElement>;
301
333
  }>;
302
334
  declare const TextField: <F extends FieldFormApi>({ form, path, label, placeholder, type, autoComplete, ref, }: TextFieldProps<F>) => react.JSX.Element;
335
+ type DateFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
336
+ form: F;
337
+ path: PathsOf<F>;
338
+ label?: ReactNode;
339
+ min?: string;
340
+ max?: string;
341
+ ref?: FieldRef<HTMLInputElement>;
342
+ }>;
343
+ declare const DateField: <F extends FieldFormApi>({ form, path, label, min, max, ref, }: DateFieldProps<F>) => react.JSX.Element;
303
344
  type NumberFieldProps<F extends FieldFormApi = FieldFormApi> = Readonly<{
304
345
  form: F;
305
346
  path: PathsOf<F>;
@@ -347,6 +388,14 @@ type NumberInputProps = Readonly<{
347
388
  onChange: (e: ChangeEvent<HTMLInputElement>) => void;
348
389
  onBlur: () => void;
349
390
  }>;
391
+ type DateInputProps = Readonly<{
392
+ type: "date";
393
+ name: string;
394
+ value: string;
395
+ "aria-invalid": true | undefined;
396
+ onChange: (e: ChangeEvent<HTMLInputElement>) => void;
397
+ onBlur: () => void;
398
+ }>;
350
399
  type CheckboxProps = Readonly<{
351
400
  type: "checkbox";
352
401
  name: string;
@@ -374,7 +423,18 @@ type ParsedNumberText = Readonly<{
374
423
  declare const parseNumberText: (text: string) => ParsedNumberText;
375
424
  declare const textInputProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => TextInputProps;
376
425
  declare const numberInputProps: <T extends number | null | undefined>(field: UseFieldReturn<T>) => NumberInputProps;
426
+ declare const dateToInputText: (value: Date | null | undefined) => string;
427
+ type ParsedDateText = Readonly<{
428
+ kind: "empty";
429
+ }> | Readonly<{
430
+ kind: "date";
431
+ value: Date;
432
+ }> | Readonly<{
433
+ kind: "invalid";
434
+ }>;
435
+ declare const parseDateText: (text: string) => ParsedDateText;
436
+ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFieldReturn<T>) => DateInputProps;
377
437
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
378
438
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
379
439
 
380
- export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputProps, type ParsedNumberText, type PathSegment, type PathsOf, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledValidationResult, type SubmitHandler, type SubmitOptions, type SubmitResult, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type ValidationMode, type ValidationResult, type ValidationTrigger, checkboxProps, createForm, createFormContext, createFormHooks, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, isAsyncRequiredError, numberInputProps, numberToInputText, parseNumberText, parsePath, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useSubmitCount, validateAsync, validateSync };
440
+ export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledValidationResult, type SubmitHandler, type SubmitOptions, type SubmitResult, TextField, type TextFieldProps, type TextInputProps, type UseFieldArrayReturn, type UseFieldOptions, type UseFieldReturn, type ValidationMode, type ValidationResult, type ValidationTrigger, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useSubmitCount, useVariantField, validateAsync, validateSync };