'use client' import { forwardRef } from 'react' import classNames from 'classnames' import useMergeRefs from '~/src/hooks/useMergeRefs' import { noop } from '~/src/utils/function' import { isEmpty } from '~/src/utils/type' import { useFormControlContext } from '~/src/components/FormControl' import { Text } from '~/src/components/Text' import type { BaseHelperTextProps, FormErrorMessageProps, FormHelperTextProps, } from './FormHelperText.types' import styles from './FormHelperText.module.scss' /** * @deprecated */ const FORM_HELPER_TEXT_TEST_ID = 'bezier-form-helper-text' /** * @deprecated */ const FORM_ERROR_MESSAGE_TEST_ID = 'bezier-form-error-message' const BaseHelperText = forwardRef( function BaseHelperText(props, forwardedRef) { const { type, typo = '13', children, className, ...rest } = props const contextValue = useFormControlContext() const getProps = type === 'info' ? contextValue?.getHelperTextProps : contextValue?.getErrorMessageProps const { visible, ref, className: formControlClassName, ...ownProps } = getProps?.(rest) ?? { visible: true, ref: noop, className: undefined, ...rest, } const mergedRef = useMergeRefs(ref, forwardedRef) if (isEmpty(children) || !visible) { return null } return ( {children} ) } ) /** * `FormHelperText` is a component to show the description of the input element. * `FormControl` component can handle its layout by `position` props. * @example * ```tsx * * * Password * * * * Please use at least 6 characters * * * ``` */ export const FormHelperText = forwardRef( function FormHelperText(props, forwardedRef) { const { color = 'text-neutral-lighter', children, ...rest } = props return ( {children} ) } ) /** * `FormErrorMessage` is a component to show error message when form values are invalid. * It should be used with `FormControl` component. * @example * ```tsx * * * Password * * * * Password should be at least 6 characters * * * ``` */ export const FormErrorMessage = forwardRef< HTMLSpanElement, FormErrorMessageProps >(function FormErrorMessage(props, forwardedRef) { const { color = 'text-accent-orange', children, ...rest } = props return ( {children} ) })