import { useMemo } from 'react'; import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; import { polymorphicForwardRef } from '@/types/polymorphic'; import classes from './styles.module.scss'; interface CharacterCounterProps { currentString: string; maxLength: number; recommended?: boolean; } const segmenter = new Intl.Segmenter(); export const CharacterCounter = polymorphicForwardRef< 'span', CharacterCounterProps >( ( { currentString, maxLength, as: Component = 'span', recommended = false, className, ...props }, ref, ) => { const currentLength = useMemo( () => [...segmenter.segment(currentString)].length, [currentString], ); return ( maxLength && !recommended && classes.counterError, )} > {recommended ? ( ) : ( )} ); }, ); CharacterCounter.displayName = 'CharCounter';