import React, { useMemo } from 'react'; import { v4 as uuidv4 } from 'uuid'; interface IFormGroup { hintText?: string | React.ReactNode, labelText: string, errors?: string[] } const FormGroup: React.FC = (props) => { const { children, errors = [], labelText, hintText } = props; const formFieldId: string = useMemo(() => `field-${uuidv4()}`, []); const inputChildren = React.Children.toArray(children); let firstChild; if (React.isValidElement(inputChildren[0])) { firstChild = React.cloneElement( inputChildren[0], { id: formFieldId }, ); } return (
{firstChild} {inputChildren.filter((_, i) => i !== 0)} {errors?.length > 0 && (

{errors.join(', ')}

)} {hintText ? (

{hintText}

) : null}
); }; export default FormGroup;