{"version":3,"file":"useFieldError.mjs","names":["formFieldStyles"],"sources":["../../../../../../../src/lib/hooks/useFieldError.tsx"],"sourcesContent":["import React, {\n  type FC,\n  type PropsWithChildren,\n  useMemo,\n  useState,\n} from \"react\";\nimport { type PropsContext, PropsContextProvider } from \"@/lib/propsContext\";\nimport formFieldStyles from \"@/components/FormField/FormField.module.scss\";\nimport ClearPropsContext from \"@/lib/propsContext/components/ClearPropsContext\";\nimport { useProps } from \"@/lib/hooks/useProps\";\nimport { UiComponentTunnelExit } from \"@/components/UiComponentTunnel/UiComponentTunnelExit\";\nimport type { FlowComponentName } from \"@/components/propTypes\";\nimport { FieldErrorRenderedContext } from \"@/lib/hooks/fieldErrorRenderedContext\";\n\nexport interface UseFieldErrorOptions {\n  fieldErrorId: string;\n  tunnelId?: string;\n  component: FlowComponentName;\n}\n\nexport const useFieldError = (options: UseFieldErrorOptions) => {\n  const fieldErrorProps = useProps(\"FieldError\", {});\n  const currentTunnelId = fieldErrorProps.tunnel?.id;\n  const tunnelId =\n    options.tunnelId ?? currentTunnelId ?? `${options.fieldErrorId}.fieldError`;\n\n  const fieldErrorCapturePropsContext: PropsContext = {\n    FieldError: {\n      id: options.fieldErrorId,\n      tunnel: { id: tunnelId, component: options.component },\n      className: formFieldStyles.fieldError,\n    },\n  };\n\n  const [renderedFieldErrorId, setRenderedFieldErrorId] = useState<string>();\n\n  const FieldErrorCaptureContext: FC<PropsWithChildren> = useMemo(\n    () => (props) => {\n      return (\n        <PropsContextProvider\n          props={fieldErrorCapturePropsContext}\n          dependencies={[tunnelId]}\n        >\n          {props.children}\n        </PropsContextProvider>\n      );\n    },\n    [tunnelId],\n  );\n\n  // Keep the component identity stable: a new one per render makes React\n  // remount the error on every render of the field (each keystroke).\n  const FieldErrorView: FC = useMemo(\n    () => () => {\n      if (currentTunnelId) {\n        return null;\n      }\n\n      return (\n        <FieldErrorRenderedContext value={setRenderedFieldErrorId}>\n          <UiComponentTunnelExit id={tunnelId} component={options.component}>\n            {(children) => {\n              const childrenArray = React.Children.toArray(children);\n              return <ClearPropsContext>{childrenArray[0]}</ClearPropsContext>;\n            }}\n          </UiComponentTunnelExit>\n        </FieldErrorRenderedContext>\n      );\n    },\n    [currentTunnelId, tunnelId, options.component],\n  );\n\n  return {\n    FieldErrorCaptureContext,\n    FieldErrorView,\n    /**\n     * The id of the error while one is rendered for this field. A nested field\n     * (a `Checkbox` inside a `CheckboxButton`) renders its error at the outer\n     * one, so only the outer field ever gets an id here.\n     */\n    renderedFieldErrorId,\n  } as const;\n};\n"],"mappings":";;;;;;;;;;;AAoBA,IAAa,iBAAiB,YAAkC;CAE9D,MAAM,kBADkB,SAAS,cAAc,CAAC,CACxB,CAAA,CAAgB,QAAQ;CAChD,MAAM,WACJ,QAAQ,YAAY,mBAAmB,GAAG,QAAQ,aAAa;CAEjE,MAAM,gCAA8C,EAClD,YAAY;EACV,IAAI,QAAQ;EACZ,QAAQ;GAAE,IAAI;GAAU,WAAW,QAAQ;EAAU;EACrD,WAAWA,yBAAgB;CAC7B,EACF;CAEA,MAAM,CAAC,sBAAsB,2BAA2B,SAAiB;CAsCzE,OAAO;EACL,0BArCsD,eAC/C,UAAU;GACf,OACE,oBAAC,sBAAD;IACE,OAAO;IACP,cAAc,CAAC,QAAQ;IAEtB,UAAA,MAAM;GACa,CAAA;EAE1B,GACA,CAAC,QAAQ,CA0BT;EACA,gBAtByB,oBACb;GACV,IAAI,iBACF,OAAO;GAGT,OACE,oBAAC,2BAAD;IAA2B,OAAO;IAChC,UAAA,oBAAC,uBAAD;KAAuB,IAAI;KAAU,WAAW,QAAQ;KACpD,WAAA,aAAa;MACb,MAAM,gBAAgB,MAAM,SAAS,QAAQ,QAAQ;MACrD,OAAO,oBAAC,mBAAD,EAAA,UAAoB,cAAc,GAAsB,CAAA;KACjE;IACqB,CAAA;GACE,CAAA;EAE/B,GACA;GAAC;GAAiB;GAAU,QAAQ;EAAS,CAK7C;;;;;;EAMA;CACF;AACF"}