formstand 0.12.0 → 0.13.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
@@ -255,10 +255,17 @@ type UseFieldReturn<TValue> = Readonly<{
255
255
  validateAsync: () => Promise<FieldValidationResult>;
256
256
  onBlur: () => void;
257
257
  }>;
258
+ declare const inferredCallsOnly$2: unique symbol;
259
+ interface InferredTypeArg$2 {
260
+ readonly [inferredCallsOnly$2]: typeof inferredCallsOnly$2;
261
+ }
258
262
  declare function useField<TSchema extends z.ZodType>(form: FieldFormApi & Readonly<{
259
263
  schema: TSchema;
260
264
  store: ReadonlyStore$2<FormState<z.input<TSchema>>>;
261
265
  }>, pathSelector: (state: FormState<z.input<TSchema>>) => string, options?: UseFieldOptions): UseFieldReturn<unknown>;
266
+ declare function useField<TValue = InferredTypeArg$2>(form: FieldFormApi & Readonly<{
267
+ schema: z.ZodType;
268
+ }>, path: TValue extends InferredTypeArg$2 ? never : "Remove the explicit type argument: a schema-typed form infers the value type from the path", options?: UseFieldOptions): UseFieldReturn<TValue>;
262
269
  declare function useField<TValue = unknown>(form: FieldFormApi & {
263
270
  readonly schema?: undefined;
264
271
  }, path: FieldPathArg<unknown>, options?: UseFieldOptions): UseFieldReturn<TValue>;
@@ -269,10 +276,17 @@ type CommonKeys<V> = keyof V;
269
276
  type AllKeys<V> = V extends unknown ? keyof V : never;
270
277
  type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
271
278
  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;
272
- declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
279
+ declare const inferredCallsOnly$1: unique symbol;
280
+ interface InferredTypeArg$1 {
281
+ readonly [inferredCallsOnly$1]: typeof inferredCallsOnly$1;
282
+ }
283
+ declare function useVariantField<TValue = InferredTypeArg$1>(form: FieldFormApi & Readonly<{
284
+ schema: z.ZodType;
285
+ }>, unionPath: TValue extends InferredTypeArg$1 ? never : "Remove the explicit type argument: a schema-typed form infers the variant value from the union path and field", field: string): UseFieldReturn<TValue | undefined>;
273
286
  declare function useVariantField<TValue = unknown>(form: FieldFormApi & {
274
287
  readonly schema?: undefined;
275
288
  }, unionPath: string, field: string): UseFieldReturn<TValue | undefined>;
289
+ declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
276
290
 
277
291
  type ReadonlyStore$1<T> = Pick<StoreApi<T>, "getState" | "getInitialState" | "subscribe">;
278
292
  type FieldArrayFormApi = Readonly<{
@@ -300,10 +314,17 @@ type UseFieldArrayReturn<TItem> = Readonly<{
300
314
  swap: (a: number, b: number) => void;
301
315
  }>;
302
316
  type ArrayItemOf<T> = [NonNullable<T>] extends [readonly (infer U)[]] ? U : never;
317
+ declare const inferredCallsOnly: unique symbol;
318
+ interface InferredTypeArg {
319
+ readonly [inferredCallsOnly]: typeof inferredCallsOnly;
320
+ }
303
321
  declare function useFieldArray<TSchema extends z.ZodType>(form: FieldArrayFormApi & Readonly<{
304
322
  schema: TSchema;
305
323
  store: ReadonlyStore$1<FormState<z.input<TSchema>>>;
306
324
  }>, pathSelector: (state: FormState<z.input<TSchema>>) => string): UseFieldArrayReturn<unknown>;
325
+ declare function useFieldArray<TItem = InferredTypeArg>(form: FieldArrayFormApi & Readonly<{
326
+ schema: z.ZodType;
327
+ }>, path: TItem extends InferredTypeArg ? never : "Remove the explicit type argument: a schema-typed form infers the item type from the path"): UseFieldArrayReturn<TItem>;
307
328
  declare function useFieldArray<TItem = unknown>(form: FieldArrayFormApi & {
308
329
  readonly schema?: undefined;
309
330
  }, path: FieldPathArg<unknown>): UseFieldArrayReturn<TItem>;
@@ -356,6 +377,11 @@ declare function useFormSelectorShallow<U>(form: FormStateApi & {
356
377
  readonly schema?: undefined;
357
378
  }, selector: (state: FormState<unknown>) => U): U;
358
379
 
380
+ declare function useFormValues<TSchema extends z.ZodType, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>): z.input<TSchema>;
381
+ declare function useFormValues(form: FormStateApi & {
382
+ readonly schema?: undefined;
383
+ }): unknown;
384
+
359
385
  declare const useFormError: (form: FormStateApi) => readonly string[] | undefined;
360
386
 
361
387
  declare function useIsDirty(form: FormStateApi & {
@@ -498,4 +524,4 @@ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFiel
498
524
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
499
525
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
500
526
 
501
- export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type DefaultPathDepth, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type FieldsValidationResult, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledFieldsValidationResult, 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, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
527
+ export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type DefaultPathDepth, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type FieldsValidationResult, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledFieldsValidationResult, 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, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
package/dist/index.d.ts CHANGED
@@ -255,10 +255,17 @@ type UseFieldReturn<TValue> = Readonly<{
255
255
  validateAsync: () => Promise<FieldValidationResult>;
256
256
  onBlur: () => void;
257
257
  }>;
258
+ declare const inferredCallsOnly$2: unique symbol;
259
+ interface InferredTypeArg$2 {
260
+ readonly [inferredCallsOnly$2]: typeof inferredCallsOnly$2;
261
+ }
258
262
  declare function useField<TSchema extends z.ZodType>(form: FieldFormApi & Readonly<{
259
263
  schema: TSchema;
260
264
  store: ReadonlyStore$2<FormState<z.input<TSchema>>>;
261
265
  }>, pathSelector: (state: FormState<z.input<TSchema>>) => string, options?: UseFieldOptions): UseFieldReturn<unknown>;
266
+ declare function useField<TValue = InferredTypeArg$2>(form: FieldFormApi & Readonly<{
267
+ schema: z.ZodType;
268
+ }>, path: TValue extends InferredTypeArg$2 ? never : "Remove the explicit type argument: a schema-typed form infers the value type from the path", options?: UseFieldOptions): UseFieldReturn<TValue>;
262
269
  declare function useField<TValue = unknown>(form: FieldFormApi & {
263
270
  readonly schema?: undefined;
264
271
  }, path: FieldPathArg<unknown>, options?: UseFieldOptions): UseFieldReturn<TValue>;
@@ -269,10 +276,17 @@ type CommonKeys<V> = keyof V;
269
276
  type AllKeys<V> = V extends unknown ? keyof V : never;
270
277
  type VariantKeys<V> = Exclude<AllKeys<V>, CommonKeys<V>> & string;
271
278
  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;
272
- declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
279
+ declare const inferredCallsOnly$1: unique symbol;
280
+ interface InferredTypeArg$1 {
281
+ readonly [inferredCallsOnly$1]: typeof inferredCallsOnly$1;
282
+ }
283
+ declare function useVariantField<TValue = InferredTypeArg$1>(form: FieldFormApi & Readonly<{
284
+ schema: z.ZodType;
285
+ }>, unionPath: TValue extends InferredTypeArg$1 ? never : "Remove the explicit type argument: a schema-typed form infers the variant value from the union path and field", field: string): UseFieldReturn<TValue | undefined>;
273
286
  declare function useVariantField<TValue = unknown>(form: FieldFormApi & {
274
287
  readonly schema?: undefined;
275
288
  }, unionPath: string, field: string): UseFieldReturn<TValue | undefined>;
289
+ declare function useVariantField<TSchema extends z.ZodType, P extends string, TField extends VariantKeys<UnionValueAt<z.input<TSchema>, P>>, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>, unionPath: P, field: TField): UseFieldReturn<VariantFieldValue<UnionValueAt<z.input<TSchema>, P>, TField> | undefined>;
276
290
 
277
291
  type ReadonlyStore$1<T> = Pick<StoreApi<T>, "getState" | "getInitialState" | "subscribe">;
278
292
  type FieldArrayFormApi = Readonly<{
@@ -300,10 +314,17 @@ type UseFieldArrayReturn<TItem> = Readonly<{
300
314
  swap: (a: number, b: number) => void;
301
315
  }>;
302
316
  type ArrayItemOf<T> = [NonNullable<T>] extends [readonly (infer U)[]] ? U : never;
317
+ declare const inferredCallsOnly: unique symbol;
318
+ interface InferredTypeArg {
319
+ readonly [inferredCallsOnly]: typeof inferredCallsOnly;
320
+ }
303
321
  declare function useFieldArray<TSchema extends z.ZodType>(form: FieldArrayFormApi & Readonly<{
304
322
  schema: TSchema;
305
323
  store: ReadonlyStore$1<FormState<z.input<TSchema>>>;
306
324
  }>, pathSelector: (state: FormState<z.input<TSchema>>) => string): UseFieldArrayReturn<unknown>;
325
+ declare function useFieldArray<TItem = InferredTypeArg>(form: FieldArrayFormApi & Readonly<{
326
+ schema: z.ZodType;
327
+ }>, path: TItem extends InferredTypeArg ? never : "Remove the explicit type argument: a schema-typed form infers the item type from the path"): UseFieldArrayReturn<TItem>;
307
328
  declare function useFieldArray<TItem = unknown>(form: FieldArrayFormApi & {
308
329
  readonly schema?: undefined;
309
330
  }, path: FieldPathArg<unknown>): UseFieldArrayReturn<TItem>;
@@ -356,6 +377,11 @@ declare function useFormSelectorShallow<U>(form: FormStateApi & {
356
377
  readonly schema?: undefined;
357
378
  }, selector: (state: FormState<unknown>) => U): U;
358
379
 
380
+ declare function useFormValues<TSchema extends z.ZodType, D extends PathDepth = DefaultPathDepth>(form: Form<TSchema, D>): z.input<TSchema>;
381
+ declare function useFormValues(form: FormStateApi & {
382
+ readonly schema?: undefined;
383
+ }): unknown;
384
+
359
385
  declare const useFormError: (form: FormStateApi) => readonly string[] | undefined;
360
386
 
361
387
  declare function useIsDirty(form: FormStateApi & {
@@ -498,4 +524,4 @@ declare const dateInputProps: <T extends Date | null | undefined>(field: UseFiel
498
524
  declare const checkboxProps: <T extends boolean | null | undefined>(field: UseFieldReturn<T>) => CheckboxProps;
499
525
  declare const selectProps: <T extends string | null | undefined>(field: UseFieldReturn<T>) => SelectProps;
500
526
 
501
- export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type DefaultPathDepth, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type FieldsValidationResult, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledFieldsValidationResult, 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, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
527
+ export { type BoolMap, type BoundUseField, type BoundUseFieldArray, type BoundUseFlag, type BoundUseSelector, CheckboxField, type CheckboxFieldProps, type CheckboxProps, type CreateFormOptions, DateField, type DateFieldProps, type DateInputProps, type DefaultPathDepth, type ErrorMap, type FieldArrayEntry, type FieldArrayFormApi, type FieldFormApi, type FieldPath, type FieldPathArg, type FieldRef, type FieldSnapshot, type FieldValidationResult, type FieldValue, type FieldsValidationResult, type Form, type FormContextApi, type FormHooks, type FormProviderProps, type FormState, type FormStateApi, type InvalidSubmitHandler, NumberField, type NumberFieldProps, type NumberInputBinding, type NumberInputProps, type ParsedDateText, type ParsedNumberText, type PathDepth, type PathSegment, type PathsOf, type PersistHandle, type PersistOptions, type PersistStorage, type ReadonlyStoreApi, type ResetOptions, SelectField, type SelectFieldOption, type SelectFieldProps, type SelectProps, type SettledFieldValidationResult, type SettledFieldsValidationResult, 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, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
package/dist/index.js CHANGED
@@ -179,6 +179,14 @@ var slotAtPath = (obj, path) => {
179
179
  );
180
180
  return walked.ok ? { exists: true, value: walked.current } : { exists: false };
181
181
  };
182
+ var hasOwnSlot = (obj, path) => parsePath(path).reduce(
183
+ (acc, segment) => {
184
+ const container = acc.current;
185
+ const owns = Array.isArray(container) ? typeof segment === "number" && segment < container.length : isPlainRecord(container) && Object.hasOwn(container, String(segment));
186
+ return !acc.ok || !owns ? { ok: false, current: void 0 } : { ok: true, current: readSegment(container, segment) };
187
+ },
188
+ { ok: true, current: obj }
189
+ ).ok;
182
190
  var writeIntoArray = (arr, index, rest, value) => {
183
191
  const length = Math.max(arr.length, index + 1);
184
192
  return Array.from(
@@ -881,6 +889,10 @@ var createForm = (schema, options) => {
881
889
  });
882
890
  },
883
891
  setValue: (path, value) => {
892
+ const currentValues = store.getState().values;
893
+ if (Object.is(getAtPath(currentValues, path), value) && (value !== void 0 || hasOwnSlot(currentValues, path))) {
894
+ return;
895
+ }
884
896
  clearArrayOpsUnder(store, path);
885
897
  store.setState((state) => ({
886
898
  ...state,
@@ -1278,8 +1290,10 @@ function useField(form, pathArg, options) {
1278
1290
  }, [form, path, debounceMs]);
1279
1291
  const setValue = useCallback(
1280
1292
  (value) => {
1293
+ const before = form.store.getState();
1281
1294
  form.setValue(path, value);
1282
1295
  const state = form.store.getState();
1296
+ if (state === before) return;
1283
1297
  if (shouldValidateOn(
1284
1298
  "change",
1285
1299
  state.mode,
@@ -1607,6 +1621,9 @@ var createFormHooks = (form, name) => {
1607
1621
  [`use${prefix}SubmitCount`]: useBoundSubmitCount
1608
1622
  };
1609
1623
  };
1624
+ function useFormValues(form) {
1625
+ return useStore(form.store, (state) => state.values);
1626
+ }
1610
1627
 
1611
1628
  // src/react/inputProps.ts
1612
1629
  var hasFieldError = (error) => error !== void 0 && error.length > 0;
@@ -1996,6 +2013,6 @@ var focusField = (path, root) => {
1996
2013
  return path === "" ? (root !== void 0 || document.querySelectorAll("form").length <= 1) && focusFirstOf(namedControls(scope).filter(isFocusCandidate)) : focusFirstOf(pathCandidates(scope, [path]).filter(isFocusCandidate));
1997
2014
  };
1998
2015
 
1999
- export { CheckboxField, DateField, NumberField, SelectField, TextField, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
2016
+ export { CheckboxField, DateField, NumberField, SelectField, TextField, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, getAtPath, hasFieldError, isAsyncRequiredError, numberInputProps, numberToInputText, parseDateText, parseNumberText, parsePath, persistForm, selectProps, setAtPath, shouldValidateOn, textInputProps, useField, useFieldArray, useForm, useFormError, useFormSelector, useFormSelectorShallow, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useNumberInput, useSubmitCount, useVariantField, validateAsync, validateSync };
2000
2017
  //# sourceMappingURL=index.js.map
2001
2018
  //# sourceMappingURL=index.js.map