formstand 0.17.0 → 0.18.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, S as SettledFieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-D0xLZH-Q.cjs';
2
- export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, 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-D0xLZH-Q.cjs';
1
+ import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, S as SettledFieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-B73p1TgL.cjs';
2
+ export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, 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 isClearableSchema, t as shouldValidateOn, v as validateAsync, u as validateSync } from './createForm-B73p1TgL.cjs';
3
3
  import { z } from 'zod';
4
4
  import * as react from 'react';
5
5
  import { ReactNode, ChangeEvent } from 'react';
@@ -23,7 +23,7 @@ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
23
23
  apply?: "adopt" | "restore" | "manual";
24
24
  }>;
25
25
  type PersistHandle = Readonly<{
26
- restore: () => boolean;
26
+ load: () => boolean;
27
27
  clear: () => void;
28
28
  dispose: () => void;
29
29
  }>;
@@ -62,6 +62,7 @@ type UseFieldReturn<TValue> = Readonly<{
62
62
  value: TValue;
63
63
  initialValue: TValue;
64
64
  emptyValue: null | undefined;
65
+ clearable: boolean;
65
66
  error: readonly string[] | undefined;
66
67
  firstError: string | undefined;
67
68
  touched: boolean;
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, S as SettledFieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-D0xLZH-Q.js';
2
- export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, 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-D0xLZH-Q.js';
1
+ import { P as PathDepth, D as DefaultPathDepth, F as Form, C as CreateFormOptions, a as FieldValidationResult, S as SettledFieldValidationResult, b as FormState, c as FieldPath, d as FieldValue, E as ErrorMap } from './createForm-B73p1TgL.js';
2
+ export { B as BoolMap, e as FieldSnapshot, f as FieldsValidationResult, I as InvalidSubmitHandler, R as ReadonlyStoreApi, g as ResetOptions, 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 isClearableSchema, t as shouldValidateOn, v as validateAsync, u as validateSync } from './createForm-B73p1TgL.js';
3
3
  import { z } from 'zod';
4
4
  import * as react from 'react';
5
5
  import { ReactNode, ChangeEvent } from 'react';
@@ -23,7 +23,7 @@ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
23
23
  apply?: "adopt" | "restore" | "manual";
24
24
  }>;
25
25
  type PersistHandle = Readonly<{
26
- restore: () => boolean;
26
+ load: () => boolean;
27
27
  clear: () => void;
28
28
  dispose: () => void;
29
29
  }>;
@@ -62,6 +62,7 @@ type UseFieldReturn<TValue> = Readonly<{
62
62
  value: TValue;
63
63
  initialValue: TValue;
64
64
  emptyValue: null | undefined;
65
+ clearable: boolean;
65
66
  error: readonly string[] | undefined;
66
67
  firstError: string | undefined;
67
68
  touched: boolean;
package/dist/index.js CHANGED
@@ -142,6 +142,7 @@ var emptyValueForSchema = (schema) => {
142
142
  };
143
143
  return walk(schema, false);
144
144
  };
145
+ var isClearableSchema = (schema) => schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault || schema instanceof z.ZodPrefault;
145
146
  var unwrapForWalk = (s) => {
146
147
  if (s instanceof z.ZodOptional || s instanceof z.ZodNullable) {
147
148
  return s.unwrap();
@@ -1063,7 +1064,7 @@ var persistForm = (form, options) => {
1063
1064
  return void 0;
1064
1065
  }
1065
1066
  };
1066
- const restore = () => {
1067
+ const load = () => {
1067
1068
  try {
1068
1069
  const raw = storage === null ? null : storage.getItem(options.key);
1069
1070
  if (raw === null) return false;
@@ -1108,7 +1109,7 @@ var persistForm = (form, options) => {
1108
1109
  }
1109
1110
  };
1110
1111
  if (apply !== "manual") {
1111
- restore();
1112
+ load();
1112
1113
  }
1113
1114
  const unsubscribe = form.watchValues((values) => {
1114
1115
  cancelPending();
@@ -1122,7 +1123,7 @@ var persistForm = (form, options) => {
1122
1123
  }
1123
1124
  });
1124
1125
  return Object.freeze({
1125
- restore,
1126
+ load,
1126
1127
  clear: () => {
1127
1128
  cancelPending();
1128
1129
  try {
@@ -1153,7 +1154,7 @@ var usePersistForm = (form, options) => {
1153
1154
  }, [form, key]);
1154
1155
  return useMemo(
1155
1156
  () => ({
1156
- restore: () => inner.current?.restore() ?? false,
1157
+ load: () => inner.current?.load() ?? false,
1157
1158
  clear: () => inner.current?.clear(),
1158
1159
  // dispose stops THIS component's watching early; the effect cleanup
1159
1160
  // still runs its own dispose harmlessly (persistForm's dispose is
@@ -1217,12 +1218,17 @@ function useField(form, pathArg, options) {
1217
1218
  const path = slice.path;
1218
1219
  const formSchema = form.schema;
1219
1220
  const initialValue = slice.initialValue;
1220
- const emptyValue = useMemo(() => {
1221
+ const { emptyValue, clearable } = useMemo(() => {
1221
1222
  if (formSchema !== void 0) {
1222
1223
  const sub = fieldSchemaAtPath(formSchema, path);
1223
- if (sub !== null) return emptyValueForSchema(sub);
1224
+ if (sub !== null) {
1225
+ return {
1226
+ emptyValue: emptyValueForSchema(sub),
1227
+ clearable: isClearableSchema(sub)
1228
+ };
1229
+ }
1224
1230
  }
1225
- return initialValue === null ? null : void 0;
1231
+ return initialValue === null ? { emptyValue: null, clearable: true } : { emptyValue: void 0, clearable: false };
1226
1232
  }, [formSchema, path, initialValue]);
1227
1233
  const debounceMs = options?.debounceMs;
1228
1234
  const debounceTimerRef = useRef(null);
@@ -1308,6 +1314,7 @@ function useField(form, pathArg, options) {
1308
1314
  value: slice.value,
1309
1315
  initialValue: slice.initialValue,
1310
1316
  emptyValue,
1317
+ clearable,
1311
1318
  error: slice.error,
1312
1319
  // `?.[0]` covers both the undefined channel and an empty array.
1313
1320
  firstError: slice.error?.[0],
@@ -1322,7 +1329,7 @@ function useField(form, pathArg, options) {
1322
1329
  validateAsync: validateAsync2,
1323
1330
  onBlur
1324
1331
  }),
1325
- [slice, emptyValue, setValue, setTouched, setError, clearError, validate, validateAsync2, onBlur]
1332
+ [slice, emptyValue, clearable, setValue, setTouched, setError, clearError, validate, validateAsync2, onBlur]
1326
1333
  );
1327
1334
  }
1328
1335
 
@@ -1657,9 +1664,7 @@ var textInputProps = (field) => ({
1657
1664
  "aria-invalid": ariaInvalid(field),
1658
1665
  onChange: (e) => {
1659
1666
  const text = e.target.value;
1660
- field.setValue(
1661
- text === "" && field.emptyValue === null ? null : text
1662
- );
1667
+ field.setValue(text === "" && field.clearable ? field.emptyValue : text);
1663
1668
  },
1664
1669
  onBlur: field.onBlur
1665
1670
  });
@@ -1733,9 +1738,7 @@ var selectProps = (field) => ({
1733
1738
  "aria-invalid": ariaInvalid(field),
1734
1739
  onChange: (e) => {
1735
1740
  const v = e.target.value;
1736
- field.setValue(
1737
- v === "" && field.emptyValue === null ? null : v
1738
- );
1741
+ field.setValue(v === "" && field.clearable ? field.emptyValue : v);
1739
1742
  },
1740
1743
  onBlur: field.onBlur
1741
1744
  });
@@ -1942,7 +1945,7 @@ var SelectField = ({
1942
1945
  const field = useField(form, path);
1943
1946
  const a11y = useFieldA11y(field.error);
1944
1947
  const select = selectProps(field);
1945
- const clearable = field.emptyValue === null;
1948
+ const clearable = field.clearable;
1946
1949
  const hasExplicitEmptyOption = options.some((opt) => opt.value === "");
1947
1950
  const showEmptyOption = !hasExplicitEmptyOption && (clearable || select.value === "" || placeholder !== void 0);
1948
1951
  return /* @__PURE__ */ jsx(
@@ -2031,6 +2034,6 @@ var focusField = (path, root) => {
2031
2034
  return path === "" ? (root !== void 0 || document.querySelectorAll("form").length <= 1) && focusFirstOf(namedControls(scope).filter(isFocusCandidate)) : focusFirstOf(pathCandidates(scope, [path]).filter(isFocusCandidate));
2032
2035
  };
2033
2036
 
2034
- 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, useIsValidating, useNumberInput, usePersistForm, useSubmitCount, useVariantField, validateAsync, validateSync };
2037
+ export { CheckboxField, DateField, NumberField, SelectField, TextField, checkboxProps, createForm, createFormContext, createFormHooks, dateInputProps, dateToInputText, emptyValueForSchema, flattenIssues, focusField, focusFirstError, hasFieldError, isAsyncRequiredError, isClearableSchema, numberInputProps, numberToInputText, parseDateText, parseNumberText, persistForm, selectProps, shouldValidateOn, textInputProps, useField, useFieldArray, useFields, useForm, useFormError, useFormValues, useIsDirty, useIsSubmitting, useIsValid, useIsValidating, useNumberInput, usePersistForm, useSubmitCount, useVariantField, validateAsync, validateSync };
2035
2038
  //# sourceMappingURL=index.js.map
2036
2039
  //# sourceMappingURL=index.js.map