{"version":3,"file":"useFieldComponent.mjs","names":["formFieldStyles"],"sources":["../../../../../../../src/lib/hooks/useFieldComponent.tsx"],"sourcesContent":["import { type FC, type PropsWithChildren, useId } from \"react\";\nimport { type PropsContext } from \"@/lib/propsContext\";\nimport formFieldStyles from \"@/components/FormField/FormField.module.scss\";\nimport { useFieldError } from \"@/lib/hooks/useFieldError\";\nimport { type ClassValue } from \"clsx\";\nimport type { FlowComponentName } from \"@/components/propTypes\";\n\ninterface FieldComponentProps {\n  className?: ClassValue;\n  isRequired?: boolean;\n  isDisabled?: boolean;\n  \"aria-describedby\"?: string;\n}\n\nexport interface UseFieldComponent {\n  FieldErrorCaptureContext: FC<PropsWithChildren>;\n  FieldErrorView: FC;\n  fieldPropsContext: PropsContext;\n  /** For the field's outer element. */\n  wrapperProps: {\n    className: string;\n  };\n  /**\n   * For what assistive technology lands on — the input, or the group of a\n   * grouped control. Where the react-aria root forwards `aria-describedby` to\n   * its input itself, spread it on the root together with `wrapperProps`.\n   * Spread it after `rest`: it already contains the consumer's\n   * `aria-describedby`, which `rest` would otherwise put back alone.\n   */\n  controlProps: {\n    \"aria-describedby\"?: string;\n  };\n  /** The id of the error while one is rendered, without the consumer's ids. */\n  renderedFieldErrorId?: string;\n}\n\n/** Joins id references, dropping empty ones. @internal */\nexport const joinIds = (...ids: (string | false | null | undefined)[]) =>\n  ids.filter(Boolean).join(\" \") || undefined;\n\nexport const useFieldComponent = (\n  props: FieldComponentProps,\n  component: FlowComponentName,\n): UseFieldComponent => {\n  const fieldErrorId = useId();\n  const { FieldErrorView, FieldErrorCaptureContext, renderedFieldErrorId } =\n    useFieldError({\n      fieldErrorId,\n      component,\n    });\n\n  // setting up the props context for all components that\n  // are part of a form control\n  const fieldPropsContext: PropsContext = {\n    Label: {\n      className: formFieldStyles.label,\n      optional: !props.isRequired,\n      isDisabled: !!props.isDisabled,\n    },\n    FieldDescription: {\n      className: formFieldStyles.fieldDescription,\n    },\n  };\n\n  return {\n    FieldErrorView,\n    FieldErrorCaptureContext,\n    fieldPropsContext,\n    wrapperProps: {\n      className: formFieldStyles.formField,\n    },\n    controlProps: {\n      // Reference the error only while it is rendered – an id that points to\n      // nothing fails HTML validators and a11y checks.\n      \"aria-describedby\": joinIds(\n        renderedFieldErrorId,\n        props[\"aria-describedby\"],\n      ),\n    },\n    renderedFieldErrorId,\n  } as const;\n};\n"],"mappings":";;;;;;;;AAqCA,IAAa,WAAW,GAAG,QACzB,IAAI,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,KAAK,KAAA;AAEnC,IAAa,qBACX,OACA,cACsB;CACtB,MAAM,eAAe,MAAM;CAC3B,MAAM,EAAE,gBAAgB,0BAA0B,yBAChD,cAAc;EACZ;EACA;CACF,CAAC;CAeH,OAAO;EACL;EACA;EACA,mBAAA;GAbA,OAAO;IACL,WAAWA,yBAAgB;IAC3B,UAAU,CAAC,MAAM;IACjB,YAAY,CAAC,CAAC,MAAM;GACtB;GACA,kBAAkB,EAChB,WAAWA,yBAAgB,iBAC7B;EAMA;EACA,cAAc,EACZ,WAAWA,yBAAgB,UAC7B;EACA,cAAc,EAGZ,oBAAoB,QAClB,sBACA,MAAM,mBACR,EACF;EACA;CACF;AACF"}