import { useEffect, useRef } from 'react'; import DSCharacterCount from '@scottish-government/design-system/dist/scripts/components/character-count/character-count'; import Button from '../Button'; import ConditionalWrapper from '../../common/ConditionalWrapper'; import ErrorMessage from '../ErrorMessage'; import HintText from '../../common/HintText'; import { TextInputProps } from './types'; import clsx from 'clsx'; const TextInput = ({ buttonIcon, buttonText, className, countThreshold, width, isCurrency, currencySymbol, errorMessage, hasButton = false, hasError, hintText, id, label, maxlength, name, onBlur, onChange, placeholder, type = 'text', value, ...props }: TextInputProps) => { const errorMessageId = `error-message-${id}`; const hintTextId = `hint-text-${id}`; const ref = useRef(null); const inputWrapperClasses = clsx( hasButton && 'ds_input__wrapper', hasButton && 'ds_input__wrapper--has-icon', isCurrency && 'ds_currency-wrapper' ); const describedbys: string[] = []; if (hintText) { describedbys.push(hintTextId) }; if (errorMessage) { describedbys.push(errorMessageId) }; useEffect(() => { /* istanbul ignore else */ if (ref.current) { new DSCharacterCount(ref.current).init(); } }, [ref]); function handleBlur(event: React.FocusEvent) { if (typeof onBlur === 'function') { onBlur(event); } } function handleChange(event: React.ChangeEvent) { if (typeof onChange === 'function') { onChange(event); } } return ( 0} wrapper={(children: React.JSX.Element) =>
{children}
} > {hintText && {hintText}} {errorMessage && {errorMessage}}
{children}
} > {hasButton && (buttonText || buttonIcon) && }
); }; TextInput.displayName = 'TextInput'; export default TextInput;