{"version":3,"sources":["../src/form/fieldset/fieldset.tsx"],"sourcesContent":["import {\n  Children,\n  cloneElement,\n  isValidElement,\n  useId,\n  forwardRef,\n  createContext,\n  useContext,\n} from \"react\";\nimport type {\n  FieldsetHTMLAttributes,\n  ReactElement,\n  ReactNode,\n  CSSProperties,\n  ComponentProps,\n} from \"react\";\nimport { clsx } from \"@postenbring/hedwig-css/typed-classname\";\nimport { ValidationMessage, type ValidationMessageProps } from \"../validation-message\";\nimport { type ErrorMessageProps } from \"../error-message\";\nimport { getValidationMessageValue } from \"../../utils\";\nimport { FieldsetDescription } from \"./fieldset-description\";\nimport { FieldsetLegend } from \"./fieldset-legend\";\n\ntype FieldsetLegendProps = ComponentProps<typeof FieldsetLegend>;\n\nexport interface FieldsetProps extends FieldsetHTMLAttributes<HTMLFieldSetElement> {\n  className?: string;\n  style?: CSSProperties;\n  \"data-color\"?: \"info\" | \"success\" | \"warning\" | \"error\";\n  validationMessage?: ReactNode | { value: ReactNode };\n  validationMessageProps?: Partial<ValidationMessageProps>;\n  /**\n   * Providing an errorMessage will also give contained Checkboxes or Radio buttons\n   * error styling and aria to indicate invalid state.\n   *\n   * For Radio buttons you are even better off using RadioGroup.\n   */\n\n  /** @deprecated Use `validationMessage` instead */\n  errorMessage?: ReactNode;\n  /**\n   * @deprecated Pass props directly to <Fieldset.Legend> instead.\n   */\n  legendProps?: FieldsetLegendProps;\n  /**\n   * @deprecated Use <Fieldset.Legend>...</Fieldset.Legend> instead.\n   */\n  legend?: ReactNode;\n  size?: \"small\" | \"\";\n  children: ReactNode;\n  /** @deprecated Use `validationMessageProps` instead */\n  errorMessageProps?: Partial<ErrorMessageProps>;\n}\n\n/** @deprecated Will no longer be needed */\nconst FieldsetContext = createContext<{ hasError: boolean; size: \"small\" | \"\" }>({\n  hasError: false,\n  size: \"\",\n});\n\n/** @deprecated Will no longer be needed */\nexport const useFieldsetContext = () => useContext(FieldsetContext);\n\nexport const Fieldset = forwardRef<HTMLFieldSetElement, FieldsetProps>(function Fieldset(\n  {\n    className,\n    style,\n    \"data-color\": dataColor = undefined,\n    validationMessage,\n    validationMessageProps,\n    errorMessage,\n    errorMessageProps,\n    legendProps,\n    legend,\n    size = \"\",\n    children,\n    ...rest\n  },\n  ref,\n) {\n  const validationMessageId = useId();\n  const validationColor = errorMessage ? \"error\" : dataColor;\n  const validationMessageValue = getValidationMessageValue(validationMessage, errorMessage);\n  const childArray = Children.toArray(children);\n  const legendChild = childArray.find(\n    (child): child is ReactElement<FieldsetLegendProps> =>\n      isValidElement<FieldsetLegendProps>(child) && child.type === FieldsetLegend,\n  );\n  const contentChildren = legendChild\n    ? childArray.filter((child) => child !== legendChild)\n    : childArray;\n  let renderedLegend = null;\n\n  if (legendChild) {\n    renderedLegend = cloneElement(legendChild, {\n      ...legendProps,\n      ...legendChild.props,\n      className: clsx(\n        legendProps?.className as undefined,\n        legendChild.props.className as undefined,\n      ),\n    });\n  } else if (legend !== undefined) {\n    renderedLegend = <FieldsetLegend {...legendProps}>{legend}</FieldsetLegend>;\n  }\n\n  return (\n    <fieldset\n      aria-describedby={validationMessageValue ? validationMessageId : undefined}\n      aria-invalid={errorMessage ? true : undefined}\n      className={clsx(\"hds-fieldset\", { [`hds-fieldset--${size}`]: size }, className as undefined)}\n      data-color={validationColor}\n      ref={ref}\n      style={style}\n      {...rest}\n    >\n      {renderedLegend}\n      <FieldsetContext.Provider value={{ hasError: Boolean(errorMessage), size }}>\n        {contentChildren}\n      </FieldsetContext.Provider>\n      <ValidationMessage\n        id={validationMessageId}\n        {...(validationMessageProps ?? errorMessageProps)}\n      >\n        {validationMessageValue}\n      </ValidationMessage>\n    </fieldset>\n  );\n}) as FieldsetType;\n\ntype FieldsetType = ReturnType<typeof forwardRef<HTMLFieldSetElement, FieldsetProps>> & {\n  Description: typeof FieldsetDescription;\n  Legend: typeof FieldsetLegend;\n};\n\nFieldset.Description = FieldsetDescription;\nFieldset.Legend = FieldsetLegend;\n"],"mappings":";;;;;;;;;;;;;;;;;;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