@paragrav/rhf-utils 0.65.0 → 0.67.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.
Files changed (156) hide show
  1. package/README.md +3 -2
  2. package/dist/tsdown/FormRelayOptions-mBqiP41Z.d.ts +29 -0
  3. package/dist/tsdown/FormRelaySetter-BKO0Wodf.js +192 -0
  4. package/dist/tsdown/RhfUtilsClientConfigType-CeOJzNAc.d.ts +212 -0
  5. package/dist/tsdown/RhfUtilsContextType-BFmDtONY.d.ts +81 -0
  6. package/dist/tsdown/index.d.ts +207 -0
  7. package/dist/tsdown/index.js +745 -0
  8. package/dist/tsdown/relay.d.ts +56 -0
  9. package/dist/tsdown/relay.js +71 -0
  10. package/dist/tsdown/trpc.d.ts +36 -0
  11. package/dist/tsdown/trpc.js +47 -0
  12. package/package.json +8 -24
  13. package/dist/dts/Register.d.ts +0 -16
  14. package/dist/dts/_exports/config.d.ts +0 -2
  15. package/dist/dts/_exports/errors.d.ts +0 -3
  16. package/dist/dts/_exports/form.d.ts +0 -7
  17. package/dist/dts/_exports/index.d.ts +0 -14
  18. package/dist/dts/_exports/relay.d.ts +0 -7
  19. package/dist/dts/_exports/submit.d.ts +0 -4
  20. package/dist/dts/_exports/trpc.d.ts +0 -2
  21. package/dist/dts/_exports/types.d.ts +0 -7
  22. package/dist/dts/_exports/zod.d.ts +0 -6
  23. package/dist/dts/client/config/RhfUtilsClientConfigType.d.ts +0 -71
  24. package/dist/dts/client/config/context/RhfUtilsClientConfigContext.d.ts +0 -3
  25. package/dist/dts/client/config/context/RhfUtilsClientConfigProvider.d.ts +0 -7
  26. package/dist/dts/client/config/context/useRhfUtilsClientConfig.d.ts +0 -1
  27. package/dist/dts/devtool/LazyDevTool.d.ts +0 -10
  28. package/dist/dts/devtool/LazyDevToolViaProviders.d.ts +0 -1
  29. package/dist/dts/errors/flat/FlatFieldErrorsList.d.ts +0 -6
  30. package/dist/dts/errors/flat/context/FlatFieldErrorsContextProvider.d.ts +0 -10
  31. package/dist/dts/errors/flat/context/FlatFieldErrorsOutputConfig.d.ts +0 -19
  32. package/dist/dts/errors/flat/context/useFlatFieldErrorsContext.d.ts +0 -11
  33. package/dist/dts/errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans.d.ts +0 -6
  34. package/dist/dts/errors/flat/context/useFlatFieldErrorsContextOutput.d.ts +0 -5
  35. package/dist/dts/errors/flat/filterFlatFieldErrors.d.ts +0 -7
  36. package/dist/dts/errors/flat/flattenFieldErrors.d.ts +0 -14
  37. package/dist/dts/errors/flat/getFlatFieldErrors.d.ts +0 -35
  38. package/dist/dts/errors/flat/types.d.ts +0 -16
  39. package/dist/dts/errors/getRefdFromFlatFieldErrors.d.ts +0 -1
  40. package/dist/dts/errors/isFieldErrorRefd.d.ts +0 -5
  41. package/dist/dts/errors/isFlatFieldErrorEntryRefd.d.ts +0 -5
  42. package/dist/dts/errors/message/FormErrorMessage.d.ts +0 -7
  43. package/dist/dts/errors/message/RhfUtilsFieldErrorMessage.d.ts +0 -10
  44. package/dist/dts/errors/nonfield/RhfUtilsNonFieldErrorMarker.d.ts +0 -13
  45. package/dist/dts/errors/nonfield/RhfUtilsNonFieldErrorMarker.spec.d.ts +0 -1
  46. package/dist/dts/errors/nonfield/RhfUtilsNonFieldErrorMarker.utils.d.ts +0 -18
  47. package/dist/dts/errors/nonfield/RhfUtilsNonFieldErrorMarkerHtmlAttribute.d.ts +0 -4
  48. package/dist/dts/errors/nonfield/isNonFieldError.d.ts +0 -17
  49. package/dist/dts/errors/nonfield/isNonFieldErrorMarkerInDOM.d.ts +0 -5
  50. package/dist/dts/errors/orphan/getIsOrphanFormErrorWithParentElement.d.ts +0 -5
  51. package/dist/dts/errors/orphan/getOrphansFromFlatFieldErrors.d.ts +0 -2
  52. package/dist/dts/errors/orphan/isOrphanFormError.d.ts +0 -2
  53. package/dist/dts/errors/output/consoleErrors.d.ts +0 -5
  54. package/dist/dts/errors/output/types.d.ts +0 -7
  55. package/dist/dts/errors/root/RootErrorsListFromFlatFieldErrorsContext.d.ts +0 -1
  56. package/dist/dts/errors/root/consts.d.ts +0 -1
  57. package/dist/dts/errors/root/getRootsFromFlatFieldErrors.d.ts +0 -1
  58. package/dist/dts/errors/root/isFlatFieldErrorEntryPathRoot.d.ts +0 -5
  59. package/dist/dts/errors/root/isFormErrorPathRoot.d.ts +0 -6
  60. package/dist/dts/errors/trpc/makeOnSubmitTrpcClientErrorHandler.d.ts +0 -9
  61. package/dist/dts/errors/trpc/trpcClientErrorMessageSchema.d.ts +0 -19
  62. package/dist/dts/errors/trpc/trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer.d.ts +0 -23
  63. package/dist/dts/form/RHF_UseFormReturnWithoutProxiesType.d.ts +0 -15
  64. package/dist/dts/form/RhfUtilsFormInjectorProps.d.ts +0 -17
  65. package/dist/dts/form/UseRhfUtilsFormChildrenPropsType.d.ts +0 -25
  66. package/dist/dts/form/_Controller.d.ts +0 -4
  67. package/dist/dts/form/context/utils/LastSubmitStateType.d.ts +0 -1
  68. package/dist/dts/form/context/utils/RhfUtilsContextProvider.d.ts +0 -9
  69. package/dist/dts/form/context/utils/RhfUtilsContextType.d.ts +0 -12
  70. package/dist/dts/form/context/utils/useRhfUtilsContext.d.ts +0 -5
  71. package/dist/dts/form/context/utils/useRhfUtilsContextRequestSubmit.d.ts +0 -4
  72. package/dist/dts/form/defaults/UseRhfUtilsFormGlobalDefaults.d.ts +0 -24
  73. package/dist/dts/form/defaults/UseRhfUtilsFormInstanceDefaults.d.ts +0 -18
  74. package/dist/dts/form/defaults/UseRhfUtilsFormResolvedDefaultsType.d.ts +0 -12
  75. package/dist/dts/form/defaults/form/UseRhfUtilsFormInstanceFormProps.d.ts +0 -6
  76. package/dist/dts/form/defaults/form/getRhfUtilsFormResolvedFormProps.d.ts +0 -2
  77. package/dist/dts/form/defaults/form/useRhfUtilsFormResolvedFormProps.d.ts +0 -293
  78. package/dist/dts/form/options/RhfUtilsFormOptionsType.d.ts +0 -27
  79. package/dist/dts/form/options/getRhfUtilsFormResolvedOptions.d.ts +0 -2
  80. package/dist/dts/form/options/useRhfUtilsFormResolvedOptions.d.ts +0 -8
  81. package/dist/dts/form/providers/RhfUtilsFormProviders.d.ts +0 -6
  82. package/dist/dts/form/providers/RhfUtilsFormProvidersPropsType.d.ts +0 -17
  83. package/dist/dts/form/relay/FormRelayOptions.d.ts +0 -16
  84. package/dist/dts/form/relay/context/FormRelayContextProvider.d.ts +0 -4
  85. package/dist/dts/form/relay/context/useFormRelayContext.d.ts +0 -12
  86. package/dist/dts/form/relay/context/utils.d.ts +0 -4
  87. package/dist/dts/form/relay/get/FormRelayGroupType.d.ts +0 -16
  88. package/dist/dts/form/relay/get/grouper/context/hooks.d.ts +0 -8
  89. package/dist/dts/form/relay/get/grouper/context/index.d.ts +0 -5
  90. package/dist/dts/form/relay/get/grouper/context/provider.d.ts +0 -9
  91. package/dist/dts/form/relay/get/grouper/useFirstFormStateGroup.d.ts +0 -9
  92. package/dist/dts/form/relay/get/useFormRelay.d.ts +0 -13
  93. package/dist/dts/form/relay/set/FormRelaySetter.d.ts +0 -6
  94. package/dist/dts/form/relay/set/useFormRelaySet.d.ts +0 -2
  95. package/dist/dts/form/relay/types.d.ts +0 -12
  96. package/dist/dts/form/rhf/SafeFieldValuesType.d.ts +0 -4
  97. package/dist/dts/form/rhf/UseFormPropsType.d.ts +0 -4
  98. package/dist/dts/form/rhf/getRhfUtilsFormResolvedRhfProps.d.ts +0 -4
  99. package/dist/dts/form/rhf/useRhfFormWithResolvedProps.d.ts +0 -5
  100. package/dist/dts/form/utils/getSubmitterButtonData.d.ts +0 -6
  101. package/dist/dts/form/utils/useFormRequestSubmit.d.ts +0 -9
  102. package/dist/dts/form/utils/useHandleCancelWithCheckIfCanBe.d.ts +0 -4
  103. package/dist/dts/form/utils/useHandleCancelWithCheckIfCanBeViaProviders.d.ts +0 -2
  104. package/dist/dts/form/utils/useShouldFormBlockNavigation.d.ts +0 -24
  105. package/dist/dts/form/with-handlers-and-children/RhfUtilsForm.d.ts +0 -3
  106. package/dist/dts/form/with-handlers-and-children/RhfUtilsFormProps.d.ts +0 -42
  107. package/dist/dts/form/with-handlers-and-children/RhfUtilsFormPropsOnBeforeSubmitInvariants.d.ts +0 -12
  108. package/dist/dts/form/with-handlers-and-children/_ChildrenViaProviders.d.ts +0 -3
  109. package/dist/dts/form/with-handlers-and-children/_FormWithSubmitHandler.d.ts +0 -4
  110. package/dist/dts/form/with-handlers-and-children/_useHandleSubmit.d.ts +0 -11
  111. package/dist/dts/form/with-providers/RhfUtilsFormWithProviders.d.ts +0 -8
  112. package/dist/dts/form/with-providers/RhfUtilsFormWithProvidersProps.d.ts +0 -8
  113. package/dist/dts/main.d.ts +0 -1
  114. package/dist/dts/playwright/index.d.ts +0 -0
  115. package/dist/dts/playwright-ct.config.d.ts +0 -5
  116. package/dist/dts/resolvers/ObjectSchema.d.ts +0 -7
  117. package/dist/dts/resolvers/zod/RhfUtilsUseZodFormChildrenFCType.d.ts +0 -7
  118. package/dist/dts/resolvers/zod/RhfUtilsUseZodFormChildrenPropsType.d.ts +0 -4
  119. package/dist/dts/resolvers/zod/ZodTypeSafeFieldValuesType.d.ts +0 -5
  120. package/dist/dts/resolvers/zod/consts.d.ts +0 -4
  121. package/dist/dts/resolvers/zod/form/RhfUtilsZodForm.d.ts +0 -7
  122. package/dist/dts/resolvers/zod/getZodResolver.d.ts +0 -4
  123. package/dist/dts/resolvers/zod/providers/RhfUtilsZodFormProviders.d.ts +0 -8
  124. package/dist/dts/resolvers/zod/with-providers/RhfUtilsZodFormWithProviders.d.ts +0 -10
  125. package/dist/dts/submit/UseRhfUtilsFormOnSubmitContextType.d.ts +0 -23
  126. package/dist/dts/submit/error/FormSubmitError.d.ts +0 -6
  127. package/dist/dts/submit/error/FormSubmitFieldErrors.d.ts +0 -14
  128. package/dist/dts/submit/error/setCtxErrorsByFormSubmitFieldErrors.d.ts +0 -8
  129. package/dist/dts/submit/last-error/LastSubmitErrorContextProvider.d.ts +0 -4
  130. package/dist/dts/submit/last-error/LastSubmitErrorContextType.d.ts +0 -8
  131. package/dist/dts/submit/last-error/useLastSubmitError.d.ts +0 -4
  132. package/dist/dts/submit/last-error/useLastSubmitErrorContext.d.ts +0 -3
  133. package/dist/dts/submit/last-error/useLastSubmitErrorContextWith.d.ts +0 -2
  134. package/dist/dts/submit/useFormOnSubmitSuccessfulAndReset.d.ts +0 -13
  135. package/dist/dts/submit/useFormOnSubmitted.d.ts +0 -4
  136. package/dist/dts/submit/useResetFormOnSubmitted.d.ts +0 -35
  137. package/dist/dts/submit/useRhfUtilsFormOnSubmitContext.d.ts +0 -3
  138. package/dist/dts/submit/useSubmitFormOnChange.d.ts +0 -15
  139. package/dist/dts/submit/useSubmitFormOnEvent.d.ts +0 -9
  140. package/dist/dts/submit/useSubmitFormOnWatch.d.ts +0 -14
  141. package/dist/dts/utils/PassthroughChildren.d.ts +0 -7
  142. package/dist/dts/utils/createContext.d.ts +0 -10
  143. package/dist/dts/utils/fixedForwardRef.d.ts +0 -5
  144. package/dist/dts/utils/isEmptyObject.d.ts +0 -1
  145. package/dist/dts/utils/timeoutAsync.d.ts +0 -6
  146. package/dist/dts/utils/types.d.ts +0 -3
  147. package/dist/dts/utils/useDebouncedOnChangeValue.d.ts +0 -14
  148. package/dist/dts/utils/useImperativeForwardedRef.d.ts +0 -9
  149. package/dist/dts/utils/useIsFirstRender.d.ts +0 -7
  150. package/dist/dts/utils/useRefIfValueWasTrue.d.ts +0 -2
  151. package/dist/dts/utils/useToggle.d.ts +0 -2
  152. package/dist/dts/vite-env.d.ts +0 -1
  153. package/dist/esm/FormRelaySetter.mjs +0 -89
  154. package/dist/esm/index.mjs +0 -351
  155. package/dist/esm/relay.mjs +0 -49
  156. package/dist/esm/trpc.mjs +0 -24
@@ -0,0 +1,56 @@
1
+ import { a as RhfUtilsFormOptions, i as SafeFieldValues } from "./RhfUtilsContextType-BFmDtONY.js";
2
+ import { n as FormRelay, r as FormRelayStateSelected, t as FormRelayOptions } from "./FormRelayOptions-mBqiP41Z.js";
3
+ import React$1 from "react";
4
+ import { FormState } from "react-hook-form";
5
+
6
+ //#region src/form/relay/context/FormRelayContextProvider.d.ts
7
+ type Props$1 = React$1.PropsWithChildren;
8
+ declare const FormRelayContextProvider: React$1.FC<Props$1>;
9
+ //#endregion
10
+ //#region src/form/relay/get/FormRelayGroupType.d.ts
11
+ type FormRelayGroup = Partial<Omit<FormState<SafeFieldValues>, 'defaultValues' | 'dirtyFields' | 'touchedFields' | 'validatingFields' | 'errors' | 'submitErrors'> & {
12
+ hasErrors: boolean;
13
+ hasDirtyFields: boolean;
14
+ hasTouchedFields: boolean;
15
+ hasValidatingFields: boolean; /** Sum of all submit counts. */
16
+ submitCount: number;
17
+ /**
18
+ * Consider form flagged when mounted.
19
+ *
20
+ * (To use, return via `FormRelayOptions`'s `select`'s return's `FormRelayStateSelected`'s `isMounted` property.)
21
+ */
22
+ isMounted: boolean;
23
+ }>;
24
+ //#endregion
25
+ //#region src/form/relay/get/grouper/useFirstFormStateGroup.d.ts
26
+ /**
27
+ * Use this when you don't (want to) know whether component is wrapped
28
+ * with RHF context, `FormRelayGrouperContext`, or `FormRelayContext`.
29
+ * Will return `FormRelayGroup`, regardless.
30
+ *
31
+ * Allows you to build naive components that are not tied to one specific context.
32
+ */
33
+ declare const useFirstFormStateGroup: () => FormRelayGroup;
34
+ //#endregion
35
+ //#region src/form/relay/get/useFormRelay.d.ts
36
+ declare const useFormRelayId: (formId: string) => FormRelay | undefined;
37
+ type FormRelayGroupCriteria = {
38
+ groups: string[];
39
+ } | {
40
+ predicate: (context: {
41
+ groups: FormRelayOptions['groups'];
42
+ options: RhfUtilsFormOptions;
43
+ }) => boolean;
44
+ };
45
+ declare const useFormRelayGroup: (criteria?: FormRelayGroupCriteria) => FormRelayGroup;
46
+ //#endregion
47
+ //#region src/form/relay/set/FormRelaySetter.d.ts
48
+ type Props = {
49
+ options?: FormRelayOptions;
50
+ };
51
+ declare const FormRelaySetter: React.FC<Props>;
52
+ //#endregion
53
+ //#region src/form/relay/set/useFormRelaySet.d.ts
54
+ declare const useFormRelaySet: (options?: FormRelayOptions) => void;
55
+ //#endregion
56
+ export { type FormRelay, FormRelayContextProvider, type FormRelayGroup, FormRelaySetter, type FormRelayStateSelected, useFirstFormStateGroup, useFormRelayGroup, useFormRelayId, useFormRelaySet };
@@ -0,0 +1,71 @@
1
+ import { c as FormRelayContextProvider, i as isEmptyObject, l as useFormRelayContext, n as useFormRelaySet, s as useRhfUtilsMaybeContext, t as FormRelaySetter, u as createContext } from "./FormRelaySetter-BKO0Wodf.js";
2
+ import React from "react";
3
+ import { useFormState } from "react-hook-form";
4
+ //#region src/form/relay/context/utils.ts
5
+ /** Condense list of form states to singular booleans. */
6
+ const getFormRelayedAggregatedByRelayedItem = (formStates) => formStates.reduce((acc, curr) => ({
7
+ isDirty: acc.isDirty || curr.isDirty,
8
+ isSubmitting: acc.isSubmitting || curr.isSubmitting,
9
+ isLoading: acc.isLoading || curr.isLoading,
10
+ isSubmitted: acc.isSubmitted || curr.isSubmitted,
11
+ isSubmitSuccessful: acc.isSubmitSuccessful || curr.isSubmitSuccessful,
12
+ isValidating: acc.isValidating || curr.isValidating,
13
+ isValid: acc.isValid || curr.isValid,
14
+ isReady: acc.isReady || curr.isReady,
15
+ disabled: acc.disabled || curr.disabled,
16
+ hasErrors: acc.hasErrors || curr.errors && !isEmptyObject(curr.errors),
17
+ hasDirtyFields: acc.hasDirtyFields || curr.dirtyFields && !isEmptyObject(curr.dirtyFields),
18
+ hasTouchedFields: acc.hasTouchedFields || curr.touchedFields && !isEmptyObject(curr.touchedFields),
19
+ hasValidatingFields: acc.hasValidatingFields || curr.validatingFields && !isEmptyObject(curr.validatingFields),
20
+ submitCount: (acc.submitCount ?? 0) + (curr.submitCount ?? 0),
21
+ isMounted: acc.isMounted || curr.isMounted
22
+ }), {
23
+ isDirty: void 0,
24
+ isSubmitting: void 0,
25
+ isLoading: void 0,
26
+ isSubmitted: void 0,
27
+ isSubmitSuccessful: void 0,
28
+ isValidating: void 0,
29
+ isReady: void 0,
30
+ isValid: void 0,
31
+ disabled: void 0,
32
+ hasErrors: void 0,
33
+ hasDirtyFields: void 0,
34
+ hasTouchedFields: void 0,
35
+ hasValidatingFields: void 0,
36
+ submitCount: void 0,
37
+ isMounted: void 0
38
+ });
39
+ //#endregion
40
+ //#region src/form/relay/get/useFormRelay.ts
41
+ const useFormRelayId = (formId) => useFormRelayContext().state[formId];
42
+ const useFormRelayGroup = (criteria) => {
43
+ const relayState = useFormRelayContext().state;
44
+ const state = getFormRelayedAggregatedByRelayedItem(getFormRelaysByCriteria(relayState, criteria).map(({ state }) => state));
45
+ const jsonState = JSON.stringify(state);
46
+ return React.useMemo(() => state, [jsonState]);
47
+ };
48
+ const getFormRelaysByCriteria = (relayState, criteria) => Object.values(relayState).filter(({ options, utils }) => !criteria || "groups" in criteria && criteria.groups.filter((group) => options.groups?.includes(group)).length || "predicate" in criteria && criteria.predicate({
49
+ groups: options.groups,
50
+ options: utils.options
51
+ }));
52
+ //#endregion
53
+ //#region src/form/relay/get/grouper/context/index.ts
54
+ const { Provider: _FormRelayGrouperContextProvider, useRequired: _useFormRelayGrouperContext, useMaybe: _useFormRelayGrouperMaybeContext } = createContext();
55
+ //#endregion
56
+ //#region src/form/relay/get/grouper/useFirstFormStateGroup.ts
57
+ /**
58
+ * Use this when you don't (want to) know whether component is wrapped
59
+ * with RHF context, `FormRelayGrouperContext`, or `FormRelayContext`.
60
+ * Will return `FormRelayGroup`, regardless.
61
+ *
62
+ * Allows you to build naive components that are not tied to one specific context.
63
+ */
64
+ const useFirstFormStateGroup = () => {
65
+ if (useRhfUtilsMaybeContext()) return getFormRelayedAggregatedByRelayedItem([useFormState()]);
66
+ const maybeRelayCtx = _useFormRelayGrouperMaybeContext();
67
+ if (maybeRelayCtx) return maybeRelayCtx.state;
68
+ return useFormRelayGroup();
69
+ };
70
+ //#endregion
71
+ export { FormRelayContextProvider, FormRelaySetter, useFirstFormStateGroup, useFormRelayGroup, useFormRelayId, useFormRelaySet };
@@ -0,0 +1,36 @@
1
+ import { c as FormSubmitFieldErrors, t as RhfUtilsClientConfig } from "./RhfUtilsClientConfigType-CeOJzNAc.js";
2
+ import { TRPCClientErrorLike } from "@trpc/client";
3
+ import { z } from "zod";
4
+ import { AnyTRPCRouter } from "@trpc/server";
5
+
6
+ //#region src/errors/trpc/makeOnSubmitTrpcClientErrorHandler.d.ts
7
+ /**
8
+ * HOF that returns submit error handler for {@link RhfUtilsClientConfig.onSubmitErrorUnknown}.
9
+ *
10
+ * @param onError Fallback for handling non-TRPC errors.
11
+ * @returns FormSubmitError
12
+ */
13
+ declare const makeOnSubmitTrpcClientErrorHandler: (onNotTrpcClientError: NonNullable<RhfUtilsClientConfig["onSubmitErrorUnknown"]>) => (error: unknown) => FormSubmitFieldErrors | undefined;
14
+ //#endregion
15
+ //#region src/errors/trpc/trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer.d.ts
16
+ /**
17
+ * Schema that transforms {@link TRPCClientError} to RHF {@link FormSubmitFieldErrors}.
18
+ */
19
+ declare const trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer: z.ZodEffects<z.ZodEffects<z.ZodPipeline<z.ZodEffects<z.ZodEffects<z.ZodType<TRPCClientErrorLike<AnyTRPCRouter>, z.ZodTypeDef, TRPCClientErrorLike<AnyTRPCRouter>>, string, TRPCClientErrorLike<AnyTRPCRouter>>, unknown, TRPCClientErrorLike<AnyTRPCRouter>>, z.ZodArray<z.ZodObject<{
20
+ code: z.ZodString;
21
+ message: z.ZodString;
22
+ path: z.ZodArray<z.ZodString, "many">;
23
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
24
+ code: z.ZodString;
25
+ message: z.ZodString;
26
+ path: z.ZodArray<z.ZodString, "many">;
27
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
28
+ code: z.ZodString;
29
+ message: z.ZodString;
30
+ path: z.ZodArray<z.ZodString, "many">;
31
+ }, z.ZodTypeAny, "passthrough">>, "many">>, (readonly [string, {
32
+ type: string;
33
+ message: string;
34
+ }])[], TRPCClientErrorLike<AnyTRPCRouter>>, FormSubmitFieldErrors, TRPCClientErrorLike<AnyTRPCRouter>>;
35
+ //#endregion
36
+ export { makeOnSubmitTrpcClientErrorHandler, trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer };
@@ -0,0 +1,47 @@
1
+ import { TRPCClientError } from "@trpc/client";
2
+ import { z } from "zod";
3
+ //#region src/errors/trpc/trpcClientErrorMessageSchema.ts
4
+ /**
5
+ * Schema for {@link TRPCClientError} message.
6
+ *
7
+ * (Sent from backend as JSON string.)
8
+ */
9
+ const trpcClientErrorMessageSchema = z.object({
10
+ code: z.string(),
11
+ message: z.string(),
12
+ path: z.array(z.string())
13
+ }).passthrough();
14
+ //#endregion
15
+ //#region src/errors/trpc/trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer.ts
16
+ /**
17
+ * Schema that transforms {@link TRPCClientError} to RHF {@link FormSubmitFieldErrors}.
18
+ */
19
+ const trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer = z.custom((error) => error instanceof TRPCClientError).transform((error) => error.message).transform((json) => {
20
+ try {
21
+ return JSON.parse(json);
22
+ } catch (error) {
23
+ return [{
24
+ validation: "root",
25
+ code: "server",
26
+ message: json,
27
+ path: ["root"]
28
+ }];
29
+ }
30
+ }).pipe(z.array(trpcClientErrorMessageSchema)).transform((errors) => errors.map((error) => [error.path.join("."), {
31
+ type: error.code,
32
+ message: error.message
33
+ }])).transform((errors) => Object.fromEntries(errors));
34
+ //#endregion
35
+ //#region src/errors/trpc/makeOnSubmitTrpcClientErrorHandler.ts
36
+ /**
37
+ * HOF that returns submit error handler for {@link RhfUtilsClientConfig.onSubmitErrorUnknown}.
38
+ *
39
+ * @param onError Fallback for handling non-TRPC errors.
40
+ * @returns FormSubmitError
41
+ */
42
+ const makeOnSubmitTrpcClientErrorHandler = (onNotTrpcClientError) => (error) => {
43
+ if (error instanceof TRPCClientError) return trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer.parse(error);
44
+ return onNotTrpcClientError(error);
45
+ };
46
+ //#endregion
47
+ export { makeOnSubmitTrpcClientErrorHandler, trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer };
package/package.json CHANGED
@@ -1,35 +1,18 @@
1
1
  {
2
2
  "name": "@paragrav/rhf-utils",
3
3
  "author": "paragrav.dev",
4
- "version": "0.65.0",
4
+ "version": "0.67.0",
5
5
  "description": "Integration utilities for react-hook-form.",
6
6
  "type": "module",
7
7
  "sideEffects": false,
8
8
  "files": [
9
- "dist/esm/*",
10
- "dist/dts/*"
9
+ "dist/tsdown/*"
11
10
  ],
12
- "module": "./dist/esm/index.mjs",
13
- "types": "./dist/dts/_exports/types.d.ts",
14
11
  "exports": {
15
- ".": {
16
- "import": {
17
- "types": "./dist/dts/_exports/index.d.ts",
18
- "default": "./dist/esm/index.mjs"
19
- }
20
- },
21
- "./relay": {
22
- "import": {
23
- "types": "./dist/dts/_exports/relay.d.ts",
24
- "default": "./dist/esm/relay.mjs"
25
- }
26
- },
27
- "./trpc": {
28
- "import": {
29
- "types": "./dist/dts/_exports/trpc.d.ts",
30
- "default": "./dist/esm/trpc.mjs"
31
- }
32
- }
12
+ ".": "./dist/tsdown/index.js",
13
+ "./relay": "./dist/tsdown/relay.js",
14
+ "./trpc": "./dist/tsdown/trpc.js",
15
+ "./package.json": "./package.json"
33
16
  },
34
17
  "publishConfig": {
35
18
  "withoutPublish": true,
@@ -92,6 +75,7 @@
92
75
  "rollup": "^4.61.1",
93
76
  "safe-stable-stringify": "^2.5.0",
94
77
  "terser": "^5.48.0",
78
+ "tsdown": "^0.22.2",
95
79
  "typescript": "^6.0.3",
96
80
  "typescript-eslint": "8.61.0",
97
81
  "unplugin-dts": "^1.0.2",
@@ -127,7 +111,7 @@
127
111
  "zod"
128
112
  ],
129
113
  "scripts": {
130
- "build": "tsc -b && vite build",
114
+ "build": "tsc -b && tsdown",
131
115
  "build:dev": "vite build --config vite.config.dev.ts",
132
116
  "dev": "vite --config vite.config.dev.ts --force",
133
117
  "lint": "eslint . --cache --cache-strategy content",
@@ -1,16 +0,0 @@
1
- /**
2
- * Use this to extend internal types.
3
- *
4
- * @example
5
- * ```ts
6
- * declare module '@paragrav/rhf-utils' {
7
- * export interface Register {
8
- * RhfUtilsFormOptions: {
9
- * myCustomOption?: boolean;
10
- * };
11
- * }
12
- * }
13
- * ```
14
- */
15
- // eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-object-type
16
- export interface Register {}
@@ -1,2 +0,0 @@
1
- export { RhfUtilsClientConfigProvider } from '../client/config/context/RhfUtilsClientConfigProvider';
2
- export type { RhfUtilsClientConfig } from '../client/config/RhfUtilsClientConfigType';
@@ -1,3 +0,0 @@
1
- export { useFlatFieldErrorsContext } from '../errors/flat/context/useFlatFieldErrorsContext';
2
- export { useFlatFieldErrorsContextHasOnlyOrphans } from '../errors/flat/context/useFlatFieldErrorsContextHasOnlyOrphans';
3
- export { RhfUtilsNonFieldErrorMarker } from '../errors/nonfield/RhfUtilsNonFieldErrorMarker';
@@ -1,7 +0,0 @@
1
- export { useRhfUtilsContext } from '../form/context/utils/useRhfUtilsContext';
2
- export type { RhfUtilsFormOptions } from '../form/options/RhfUtilsFormOptionsType';
3
- export type { SafeFieldValues } from '../form/rhf/SafeFieldValuesType';
4
- export type { RHF_UseFormReturnWithoutProxies as UseFormReturnWithoutProxies } from '../form/RHF_UseFormReturnWithoutProxiesType';
5
- export type { RhfUtilsFormInjectorProps } from '../form/RhfUtilsFormInjectorProps';
6
- export type { UseRhfUtilsFormChildrenProps } from '../form/UseRhfUtilsFormChildrenPropsType';
7
- export { useFormRequestSubmit } from '../form/utils/useFormRequestSubmit';
@@ -1,14 +0,0 @@
1
- /**
2
- * @license @paragrav/rhf-utils
3
- *
4
- * Copyright (c) 2024-present paragrav.dev
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE file in the root directory of this source tree.
8
- */
9
- export * from './config';
10
- export * from './errors';
11
- export * from './form';
12
- export * from './submit';
13
- export * from './zod';
14
- export type { Register } from '../Register';
@@ -1,7 +0,0 @@
1
- export { FormRelayContextProvider } from '../form/relay/context/FormRelayContextProvider';
2
- export type { FormRelayGroup } from '../form/relay/get/FormRelayGroupType';
3
- export { useFirstFormStateGroup } from '../form/relay/get/grouper/useFirstFormStateGroup';
4
- export { useFormRelayGroup, useFormRelayId, } from '../form/relay/get/useFormRelay';
5
- export { FormRelaySetter } from '../form/relay/set/FormRelaySetter';
6
- export { useFormRelaySet } from '../form/relay/set/useFormRelaySet';
7
- export type { FormRelay, FormRelayStateSelected } from '../form/relay/types';
@@ -1,4 +0,0 @@
1
- export { FormSubmitError } from '../submit/error/FormSubmitError';
2
- export type { FormSubmitFieldErrors } from '../submit/error/FormSubmitFieldErrors';
3
- export { useLastSubmitError } from '../submit/last-error/useLastSubmitError';
4
- export type { UseRhfUtilsFormOnSubmitContext, UseRhfUtilsFormOnSubmitErrorContext, } from '../submit/UseRhfUtilsFormOnSubmitContextType';
@@ -1,2 +0,0 @@
1
- export { makeOnSubmitTrpcClientErrorHandler } from '../errors/trpc/makeOnSubmitTrpcClientErrorHandler';
2
- export { trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer } from '../errors/trpc/trpcClientErrorToFormSubmitFieldErrorsSchemaTransformer';
@@ -1,7 +0,0 @@
1
- export type * from './errors';
2
- export type * from './form';
3
- export type * from './relay';
4
- export type * from './submit';
5
- export type * from './trpc';
6
- export type * from './zod';
7
- export type { FlatFieldErrors } from '../errors/flat/types';
@@ -1,6 +0,0 @@
1
- export { RhfUtilsZodForm } from '../resolvers/zod/form/RhfUtilsZodForm';
2
- export { RhfUtilsZodFormProviders } from '../resolvers/zod/providers/RhfUtilsZodFormProviders';
3
- export { RhfUtilsZodFormWithProviders } from '../resolvers/zod/with-providers/RhfUtilsZodFormWithProviders';
4
- export type { RhfUtilsUseZodFormChildrenFC } from '../resolvers/zod/RhfUtilsUseZodFormChildrenFCType';
5
- export type { RhfUtilsUseZodFormChildrenProps } from '../resolvers/zod/RhfUtilsUseZodFormChildrenPropsType';
6
- export type { ZodTypeSafeFieldValues } from '../resolvers/zod/ZodTypeSafeFieldValuesType';
@@ -1,71 +0,0 @@
1
- import { FlatFieldErrorsOutputConfig } from '../../errors/flat/context/FlatFieldErrorsOutputConfig';
2
- import { UseRhfUtilsFormGlobalDefaults } from '../../form/defaults/UseRhfUtilsFormGlobalDefaults';
3
- import { RhfUtilsFormInjectorProps } from '../../form/RhfUtilsFormInjectorProps';
4
- import { FormSubmitFieldErrors } from '../../submit/error/FormSubmitFieldErrors';
5
- import { UseRhfUtilsFormOnSubmitContext } from '../../submit/UseRhfUtilsFormOnSubmitContextType';
6
- import { MaybePromise } from '../../utils/types';
7
- /**
8
- * Client configuration object.
9
- *
10
- * Options under `defaults` can be overridden at form level.
11
- */
12
- export type RhfUtilsClientConfig = {
13
- /**
14
- * Default options for all forms. (Are overridden by form-specific options.)
15
- */
16
- defaults?: UseRhfUtilsFormGlobalDefaults;
17
- /**
18
- * Supply your own `<form>` component.
19
- *
20
- * (By default, a native {@link HTMLFormElement} is used.)
21
- */
22
- FormComponent?: React.FC<React.PropsWithChildren<React.HTMLAttributes<HTMLFormElement>>>;
23
- /**
24
- * Inject your own hooks, components, etc., into every form instance.
25
- *
26
- * @description
27
- *
28
- * (NOTE: context params are not schema-typed as not possible at this level.)
29
- *
30
- * @example
31
- *
32
- * See README.
33
- */
34
- FormInjector?: React.FC<RhfUtilsFormInjectorProps>;
35
- /**
36
- * A hook that returns a callback that determines whether form can be cancelled at event-time.
37
- *
38
- * @description
39
- *
40
- * Cancellation of forms often doesn't involve navigation, making it more challenging to centrally handle.
41
- * Your hook's returned callback should internally prompt user and return response as `boolean`
42
- * indicating whether cancellation should be blocked. See `README.md` for example.
43
- *
44
- * @returns Callback that, given {@link RhfUtilsFormContext} at event-time, returns `boolean` value indicating whether form can be cancelled.
45
- *
46
- * Callback should return:
47
- * `true`: if form can be cancelled (e.g., form not dirty or user confirmed via prompt); {@link UseRhfUtilsFormProps.onCancel} is called.
48
- * `false`: form cannot be cancelled (e.g., user denied); {@link UseRhfUtilsFormProps.onCancel} is not called.
49
- */
50
- useCanFormBeCancelled?: () => (props: UseRhfUtilsFormOnSubmitContext) => MaybePromise<boolean>;
51
- /**
52
- * Global submit error handler when/if submit handler throws an error other than {@link FormSubmitError}.
53
- *
54
- * Example use case:
55
- * - transform API errors to {@link FormSubmitFieldErrors}.
56
- *
57
- * @returns
58
- * - if {@link FormSubmitFieldErrors}: merge into context errors.
59
- * - if `undefined`: do nothing.
60
- */
61
- onSubmitErrorUnknown?: (error: unknown) => FormSubmitFieldErrors | undefined;
62
- /**
63
- * Configuration regarding RHF form state errors (`useFormContext().formState.errors`).
64
- */
65
- fieldErrors?: {
66
- /**
67
- * Configure how/if errors should be outputted for debugging/reporting (i.e., console and/or throwing).
68
- */
69
- output?: FlatFieldErrorsOutputConfig;
70
- };
71
- };
@@ -1,3 +0,0 @@
1
- import { default as React } from 'react';
2
- import { RhfUtilsClientConfig } from '../RhfUtilsClientConfigType';
3
- export declare const RhfUtilsClientConfigContext_Internal: React.Context<RhfUtilsClientConfig | undefined>;
@@ -1,7 +0,0 @@
1
- import { default as React } from 'react';
2
- import { RhfUtilsClientConfig } from '../RhfUtilsClientConfigType';
3
- type Props = React.PropsWithChildren<{
4
- config?: RhfUtilsClientConfig;
5
- }>;
6
- export declare const RhfUtilsClientConfigProvider: React.FC<Props>;
7
- export {};
@@ -1 +0,0 @@
1
- export declare const useRhfUtilsClientConfig: () => import('../RhfUtilsClientConfigType').RhfUtilsClientConfig;
@@ -1,10 +0,0 @@
1
- import { DevtoolUIProps } from '@hookform/devtools/dist/devToolUI';
2
- import { default as React } from 'react';
3
- type LazyDevToolProps = {
4
- config?: boolean | Pick<DevtoolUIProps, 'placement' | 'styles'>;
5
- };
6
- /**
7
- * Lazy-loaded RHF DevTool.
8
- */
9
- export declare const LazyDevTool: React.FC<LazyDevToolProps>;
10
- export {};
@@ -1 +0,0 @@
1
- export declare const LazyDevToolViaProviders: React.FC;
@@ -1,6 +0,0 @@
1
- import { FlatFieldErrors } from './types';
2
- export type Props = {
3
- errors: FlatFieldErrors | null | undefined;
4
- className?: string;
5
- };
6
- export declare const FlatFieldErrorsList: React.FC<Props>;
@@ -1,10 +0,0 @@
1
- import { default as React } from 'react';
2
- type Props = React.PropsWithChildren<{
3
- formRef: React.RefObject<HTMLFormElement | null>;
4
- }>;
5
- /**
6
- * Provides a flattened version of the form errors.
7
- * Allows flattening and filtering to happen once per errors change.
8
- */
9
- export declare const FlatFieldErrorsContextProvider: React.FC<Props>;
10
- export {};
@@ -1,19 +0,0 @@
1
- import { RhfUtilsErrorsOutputConsoleConfig } from '../../output/types';
2
- import { FlatFieldErrorsContext } from './useFlatFieldErrorsContext';
3
- export type FlatFieldErrorsOutputConfig = {
4
- /**
5
- * Configure how/if errors should be outputted to console.
6
- *
7
- * Example use cases:
8
- * - console.debug errors in development environment.
9
- * - console.error certain errors in production environment.
10
- */
11
- console?: (context: FlatFieldErrorsContext) => RhfUtilsErrorsOutputConsoleConfig | null | false | undefined;
12
- /**
13
- * Configure how/if errors should be thrown.
14
- *
15
- * Example use case:
16
- * - bring attention to certain errors in development environment.
17
- */
18
- throw?: (context: FlatFieldErrorsContext) => true | string | false | undefined;
19
- };
@@ -1,11 +0,0 @@
1
- import { FlatFieldErrors } from '../types';
2
- export type FlatFieldErrorsContext = {
3
- all: FlatFieldErrors;
4
- fields: FlatFieldErrors;
5
- roots: FlatFieldErrors;
6
- orphans: FlatFieldErrors;
7
- hasErrors: boolean;
8
- hasOrphans: boolean;
9
- };
10
- declare const _FormErrorsFlatContextProvider: import('react').Provider<FlatFieldErrorsContext | undefined>, useFlatFieldErrorsContext: () => FlatFieldErrorsContext;
11
- export { _FormErrorsFlatContextProvider, useFlatFieldErrorsContext };
@@ -1,6 +0,0 @@
1
- /**
2
- * Returns `true` if all errors are orphans.
3
- *
4
- * You may want to treat this case specifically.
5
- */
6
- export declare const useFlatFieldErrorsContextHasOnlyOrphans: () => boolean;
@@ -1,5 +0,0 @@
1
- import { FlatFieldErrorsOutputConfig } from './FlatFieldErrorsOutputConfig';
2
- /**
3
- * Outputs (e.g., console, throw) errors based on config.
4
- */
5
- export declare const useFlatFieldErrorsContextOutput: (config?: FlatFieldErrorsOutputConfig) => void;
@@ -1,7 +0,0 @@
1
- import { FlatFieldErrorEntry, FlatFieldErrors } from './types';
2
- /**
3
- * Filter {@link FlatFieldErrors} by a predicate.
4
- *
5
- * (Encapsulates to and from Object entries.)
6
- */
7
- export declare const makeFlatFieldErrorsFilter: (predicate: (entry: FlatFieldErrorEntry, index: number) => boolean) => (errors: FlatFieldErrors) => FlatFieldErrors;
@@ -1,14 +0,0 @@
1
- import { FieldErrors } from 'react-hook-form';
2
- /**
3
- * Flatten a {@link FieldErrors} object.
4
- *
5
- * @returns e.g.,
6
- * ```ts
7
- * {
8
- * "address.street.type": "too_short",
9
- * "address.street.ref.value": "123",
10
- * "address.street.message": "Required."
11
- * }
12
- * ```
13
- */
14
- export declare const flattenFieldErrors: (errors: FieldErrors) => Record<string, unknown>;
@@ -1,35 +0,0 @@
1
- import { FieldError, FieldErrors } from 'react-hook-form';
2
- import { SafeFieldValues } from '../../form/rhf/SafeFieldValuesType';
3
- import { FlatFieldErrors } from './types';
4
- /**
5
- * Get {@link FlatFieldErrors} from {@link FieldErrors}.
6
- * This makes it easy to work with potentially deeply-nested {@link FieldErrors}.
7
- *
8
- * @param errors {@link FieldErrors} object.
9
- *
10
- * @returns `FlatFieldErrors` object.
11
- *
12
- * @example
13
- * ```
14
- * {
15
- * "address.street": {
16
- * type: "too_short",
17
- * ref: { value: "123" },
18
- * message: "Required." }
19
- * }
20
- * }
21
- * ```
22
- */
23
- export declare const getFlatFieldErrors: (errors: FieldErrors) => FlatFieldErrors;
24
- /**
25
- * Regular expression to maybe match the end of a leaf node path.
26
- *
27
- * i.e., `name.type`, `address.street.message`
28
- *
29
- * iow: dot [?: non-capturing]("type" | "message")[\b word boundary][$ end of string]
30
- */
31
- export declare const regexMaybeFieldErrorLeafNodeSuffix: RegExp;
32
- export type FieldErrorsSansRef<TFieldValues extends SafeFieldValues = SafeFieldValues> = Record<keyof TFieldValues | 'root', Partial<Pick<FieldError, 'message' | 'type'>> & {
33
- hasRef: boolean;
34
- }>;
35
- export declare const getFlatFieldErrorsSansRef: <TFieldValues extends SafeFieldValues>(errors: FieldErrors<TFieldValues>) => FieldErrorsSansRef<TFieldValues>;
@@ -1,16 +0,0 @@
1
- import { FieldError } from 'react-hook-form';
2
- /**
3
- * Flattened field errors object.
4
- *
5
- * @example
6
- * ```ts
7
- * {
8
- * 'address.street': FieldError
9
- * }
10
- * ```
11
- */
12
- export type FlatFieldErrors = Record<string, FieldError>;
13
- /**
14
- * Return from `Object.entries` on {@link FlatFieldErrors}.
15
- */
16
- export type FlatFieldErrorEntry = [path: string, error: FieldError];
@@ -1 +0,0 @@
1
- export declare const getRefdFromFlatFieldErrors: (errors: import('./flat/types').FlatFieldErrors) => import('./flat/types').FlatFieldErrors;
@@ -1,5 +0,0 @@
1
- import { FieldError } from 'react-hook-form';
2
- /**
3
- * Determine if {@link FieldError} is a `ref`'d field error.
4
- */
5
- export declare const isFieldErrorRefd: (error: FieldError) => boolean;
@@ -1,5 +0,0 @@
1
- import { FlatFieldErrorEntry } from './flat/types';
2
- /**
3
- * Determine if {@link FieldError} is a `ref`'d field error.
4
- */
5
- export declare const isFlatFieldErrorEntryRefd: ([, error,]: FlatFieldErrorEntry) => boolean;
@@ -1,7 +0,0 @@
1
- import { default as React } from 'react';
2
- type Props = {
3
- message: string;
4
- className?: string;
5
- };
6
- export declare const FormErrorMessage: React.FC<Props>;
7
- export {};