import React, { ReactElement } from 'react'; import css from '../../../utils/css'; import { CommonProps } from '../../common'; import Icon from '../../Icon'; import { InputProps } from '../../Input/Input'; import { TextAreaProps } from '../../Input/TextArea'; import { FieldWrapper, ValidateSuffixWrapper, TextAreaWrapper, } from './StyledField'; import ValidateIcon from './ValidateIcon'; import { isInput } from './types'; export interface FieldProps extends CommonProps { /** * Input component to be wrapped. */ children: ReactElement; /** * Validation status of children's value. */ validateStatus?: 'success' | 'warning' | 'error' | 'validating'; } export const getValidateSuffixElement = ( children: ReactElement, validateStatus: 'success' | 'warning' | 'error' | 'validating' ) => { const { suffix, size } = children.props; const suffixElement = typeof suffix === 'string' ? ( ) : ( suffix ); return ( {suffixElement} ); }; export const getValidatedChildren = ( children: ReactElement, validateStatus?: 'success' | 'warning' | 'error' | 'validating' ) => { if (validateStatus === undefined) { return children; } if (isInput(children)) { return React.cloneElement(children, { ...children.props, suffix: getValidateSuffixElement(children, validateStatus), }); } const { size } = children.props; return ( {children} ); }; const Field = ({ children, validateStatus, id, className, style, sx = {}, 'data-test-id': dataTestId, }: FieldProps) => { const validatedChildren = getValidatedChildren(children, validateStatus); return ( {validatedChildren} ); }; export default Field;