import React , { memo , forwardRef } from 'react' ; import Box from '../../primitives/Box' ; import { HStack } from '../../primitives/Stack' ; import { usePropsResolution } from '../../../hooks/useThemeProps' ; import { useFormControlContext } from './useFormControl' ; import type { IFormControlErrorMessageProps } from './types' ; import { useHasResponsiveProps } from '../../../hooks/useHasResponsiveProps' ; import { combineContextAndProps } from '../../../utils' ; const FormControlErrorMessage = ( props , ref ) => { const formControlContext = useFormControlContext ( ) ; const combinedProps = combineContextAndProps ( formControlContext , props ) ; const { leftIcon , rightIcon , children , _text , _stack , ... resolvedProps } = usePropsResolution ( 'FormControlErrorMessage' , combinedProps , { isDisabled : combinedProps . isDisabled , isReadOnly : combinedProps . isReadOnly , isInvalid : combinedProps . isInvalid , } ) ; let { startIcon , endIcon } = resolvedProps ; if ( rightIcon ) { endIcon = rightIcon ; } if ( leftIcon ) { startIcon = leftIcon ; } if ( endIcon && React . isValidElement ( endIcon ) ) { endIcon = React . Children . map ( endIcon , ( child , index ) => { return React . cloneElement ( child , { key : `button-end-icon-${ index }` , ... _text , ... child . props , } ) ; } ) ; } if ( startIcon && React . isValidElement ( startIcon ) ) { startIcon = React . Children . map ( startIcon , ( child , index ) => { return React . cloneElement ( child , { key : `button-start-icon-${ index }` , ... _text , ... child . props , } ) ; } ) ; } React . useEffect ( ( ) => { resolvedProps ?. setHasFeedbackText ( true ) ; return ( ) => { resolvedProps ?. setHasFeedbackText ( false ) ; } ; } ) ; if ( useHasResponsiveProps ( props ) ) { return null ; } return resolvedProps ?. isInvalid && children ? ( < Box nativeID = { resolvedProps ?. helpTextId } { ... resolvedProps } ref = { ref } > < HStack { ... _stack } > { startIcon } < Box _text = { _text } > { children } < /Box> { endIcon } < /HStack> < /Box>) : null; } ; export default memo ( forwardRef ( FormControlErrorMessage ) ) ;