import * as React from "react"; import nextId from "react-id-generator"; import { cx } from "@emotion/css"; import { flush, liReset, padding, textSize, tintContentSecondary, tintContent, display } from "../styles/styleUtils"; import { themeError } from "../../design-tokens/build/js/designTokens"; interface RenderProps { describedByIds: string; getValidationErrors: React.ReactNode; getHintContent: React.ReactNode; isValid: boolean; } interface FormFieldWrapperProps { children: (renderProps: RenderProps) => React.ReactNode; errors?: React.ReactNode[]; hintContent?: React.ReactNode; id?: string; } const FormFieldWrapper = ({ children, errors, hintContent, id = nextId("formFieldWrapper-") }: FormFieldWrapperProps) => { const getHintContentId = () => { return hintContent ? `${id}-hintContent` : ""; }; const getErrorId = (error, id) => { const errorIndex = errors ? errors.indexOf(error) : -1; return `${id}-errorMsg${errorIndex}`; }; const getErrorIds = () => { return errors ? errors.map(error => getErrorId(error, id)).join(" ") : ""; }; const getHintContent = hintContent => { return hintContent ? ( {hintContent} ) : null; }; const getValidationErrors = (errors, id) => { if (!errors || (errors && errors.length === 0)) { return null; } return (