{"version":3,"file":"hooks.mjs","names":[],"sources":["../../src/react-hook-form/hooks.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport { useController, useFormContext, useFormState } from \"react-hook-form\";\nimport type {\n  UseFormReturn,\n  FieldValues,\n  UseFormRegister,\n} from \"react-hook-form\";\nimport { getPathInObject } from \"./utils\";\nimport type { UseFieldFn } from \"../types\";\n\n/**\n * This custom hook shares the same props and methods as register.\n * @returns Return value of `register`\n */\nexport function useRegister<TFieldValues extends FieldValues = FieldValues>(\n  ...props: Parameters<UseFormRegister<TFieldValues>>\n) {\n  const { register } = useFormContext<TFieldValues>();\n  return register(...props);\n}\n\n/**\n * RHF-backed useField implementation.\n * Normalizes useController return to the shared FieldBinding shape.\n */\nexport const useFieldRHF: UseFieldFn = ({ name }) => {\n  const { field } = useController({ name });\n  return { field };\n};\n\n/**\n * Provides the error message of a field without subscribing to the entire form state.\n * @param path - The path of the field.\n * @returns The error message of the field, or undefined if there is no error.\n */\nexport const useFieldError = (path: string[]) => {\n  const { subscribe } = useFormContext();\n\n  if (typeof subscribe !== \"function\") {\n    return useFieldErrorFallback(path);\n  }\n\n  return useFieldErrorSubscribe(path, subscribe);\n};\n\nconst useFieldErrorSubscribe = (\n  path: string[],\n  subscribe: UseFormReturn<FieldValues>[\"subscribe\"],\n) => {\n  const [error, setError] = useState<string | undefined>(undefined);\n  const fieldPath = path.join(\".\");\n\n  useEffect(() => {\n    const callback = subscribe({\n      formState: {\n        errors: true,\n      },\n      callback: ({ errors }) => {\n        const newError = getPathInObject(errors, path)?.message as\n          | string\n          | undefined;\n        setError((prev) => (prev !== newError ? newError : prev));\n      },\n    });\n\n    return () => callback();\n  }, [fieldPath, subscribe]);\n\n  return error;\n};\n\nconst useFieldErrorFallback = (path: string[]) => {\n  const fieldPath = path.join(\".\");\n  const { errors } = useFormState({\n    name: fieldPath as any,\n    exact: true,\n  });\n\n  return getPathInObject(errors, path)?.message as string | undefined;\n};\n"],"mappings":";;;;;;;;AAcA,SAAgB,YACd,GAAG,OACH;CACA,MAAM,EAAE,aAAa,eAA6B;CAClD,OAAO,SAAS,GAAG,KAAK;AAC1B;;;;;AAMA,MAAa,eAA2B,EAAE,WAAW;CACnD,MAAM,EAAE,UAAU,cAAc,EAAE,KAAK,CAAC;CACxC,OAAO,EAAE,MAAM;AACjB;;;;;;AAOA,MAAa,iBAAiB,SAAmB;CAC/C,MAAM,EAAE,cAAc,eAAe;CAErC,IAAI,OAAO,cAAc,YACvB,OAAO,sBAAsB,IAAI;CAGnC,OAAO,uBAAuB,MAAM,SAAS;AAC/C;AAEA,MAAM,0BACJ,MACA,cACG;CACH,MAAM,CAAC,OAAO,YAAY,SAA6B,KAAA,CAAS;CAGhE,gBAAgB;EACd,MAAM,WAAW,UAAU;GACzB,WAAW,EACT,QAAQ,KACV;GACA,WAAW,EAAE,aAAa;IACxB,MAAM,WAAW,gBAAgB,QAAQ,IAAI,CAAC,EAAE;IAGhD,UAAU,SAAU,SAAS,WAAW,WAAW,IAAK;GAC1D;EACF,CAAC;EAED,aAAa,SAAS;CACxB,GAAG,CAhBe,KAAK,KAAK,GAgBhB,GAAG,SAAS,CAAC;CAEzB,OAAO;AACT;AAEA,MAAM,yBAAyB,SAAmB;CAEhD,MAAM,EAAE,WAAW,aAAa;EAC9B,MAFgB,KAAK,KAAK,GAEZ;EACd,OAAO;CACT,CAAC;CAED,OAAO,gBAAgB,QAAQ,IAAI,CAAC,EAAE;AACxC"}