import React from 'react' import type { CounterProps } from '../utils/limit-counter' import { getLimit } from '../utils/limit-counter' import type { FieldProps } from '../field' import { IntlContext } from 'react-intl' type UseCounterLogicParam< TElement extends HTMLInputElement | HTMLTextAreaElement, > = CounterProps & { length: number onBeforeInput?: React.InputEventHandler onPaste?: React.ClipboardEventHandler onFocus?: React.FocusEventHandler onBlur?: React.FocusEventHandler } function changeExceedsLimit( el: HTMLInputElement | HTMLTextAreaElement, data: string, limit: number ) { const selectionSize = Math.abs( (el.selectionStart ?? 0) - (el.selectionEnd ?? 0) ) const textLength = el.value.length - selectionSize + data.length return !!limit && textLength > limit } export function useCounterLogic< TElement extends HTMLInputElement | HTMLTextAreaElement, >({ length, maxLength: maxLengthIncoming, showCounter: showCounterIncoming, limit: limitIncoming, onBeforeInput, onPaste, onFocus, onBlur, }: UseCounterLogicParam) { /* Not required to use components. But this gives us nice number formatting when present */ const intl = React.useContext(IntlContext) const { limit, showCounter, truncate, onPasteTruncated, count, overCount, nearCount, } = getLimit( length, { maxLength: maxLengthIncoming, showCounter: showCounterIncoming, limit: limitIncoming, }, intl ) const handleBeforeInput = React.useCallback< React.InputEventHandler >( (e) => { onBeforeInput?.(e) if ( !truncate || !( e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement ) ) { return } if (changeExceedsLimit(e.target, e.data, limit)) { setFlashError((s) => s + 1) } }, [truncate, limit, onBeforeInput] ) const handlePaste = React.useCallback< React.ClipboardEventHandler >( (e) => { onPaste?.(e) if ( !truncate || !( e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement ) || !onPasteTruncated ) { return } const clipboardData = e.clipboardData.getData('text/plain') if (changeExceedsLimit(e.target, clipboardData, limit)) { onPasteTruncated() } }, [limit, truncate, onPaste, onPasteTruncated] ) const [hasFocus, setHasFocus] = React.useState(false) const handleFocus = React.useCallback>( (e) => { onFocus?.(e) setHasFocus(true) }, [onFocus] ) const handleBlur = React.useCallback>( (e) => { onBlur?.(e) setHasFocus(false) }, [onBlur] ) const [flashError, setFlashError] = React.useState(0) React.useEffect(() => { if (flashError) { const timer = setTimeout(() => { setFlashError(0) }, 500) return () => clearTimeout(timer) } }, [flashError]) const fieldError = overCount || !!flashError const countConfig: FieldProps['count'] = React.useMemo( () => count && showCounter ? { text: count, status: fieldError ? 'error' : nearCount && hasFocus ? 'warning' : 'default', } : undefined, [count, showCounter, fieldError, nearCount, hasFocus] ) const maxLength = truncate && limit ? limit : undefined return { maxLength, fieldError, count: countConfig, handleBeforeInput, handlePaste, handleBlur, handleFocus, } }