import { forwardRef, SyntheticEvent, useState, useEffect } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import noop from 'utils/noop'; import { nobel } from 'styles/colors'; import { getThemeFont, fontSize } from 'styles/fonts'; import AnimatedBorder from 'components/AnimatedBorder'; import ErrorMessage from 'components/ErrorMessage'; import TextAreaComp from './components/TextAreaComp'; const Wrapper = styled.div` display: inline-block; ${({ theme: { rtl } }) => rtl && css` direction: rtl; `} `; const SCAnimatedBorder = styled(AnimatedBorder)` flex-direction: column; padding: 1em; box-sizing: border-box; > label { top: 1.5em; ${({ focused, dirty, error }) => (focused || dirty || error) && css` top: 0; `} } `; const CountMessage = styled.div` padding-top: 0.5em; color: ${nobel.main}; font-family: ${getThemeFont}; font-size: ${fontSize.small}; `; // TODO: `characterLimitMsgGenerator`, `exceedLimitMsgGenerator` are deprecated const characterLimitMsgFunc = (charactersLeft: number): string => `Characters left: ${charactersLeft}`; const exceedLimitMsgFunc = (excessCharacters: number): string => `Excess characters: ${excessCharacters}`; interface Props { /** @ignore */ style?: Record; /** @ignore */ className?: string; /** Characters limit of textarea */ maxLength?: number; /** Allow user to continue to type when the characters count over `maxLength` */ allowExceed?: boolean; /** * Callback function, to be executed when user type in textarea * * @param {SyntheticEvent} event https://reactjs.org/docs/events.html */ onChange?: (_e: SyntheticEvent) => void; /** * Callback function, to be executed when user focus on textarea * * @param {SyntheticEvent} event https://reactjs.org/docs/events.html */ onFocus?: (_e: SyntheticEvent) => void; /** * Callback function, to be executed when user blur on textarea * * @param {SyntheticEvent} event https://reactjs.org/docs/events.html */ onBlur?: (_e: SyntheticEvent) => void; /** Name of the component */ name: string; /** Label of the component */ label?: string; /** Error message of the component */ error?: string; /** Message to be shown at the bottom of component. Use `{{charactersLeft}}` for the * characters remaining, use `{{count}}` for characters count */ limitMsg?: string; /** Message to be shown at the bottom of component, when the characters count over * `maxLength`. Use `{{charactersExceed}}` for the characters exceed count, use `{{count}}` for * characters count */ exceedLimitMsg?: string; /** Textarea content value */ value?: string; /** Initial textarea content value, use it if you want to leave the component * [uncontrolled](https://reactjs.org/docs/uncontrolled-components.html) */ defaultValue?: string; /** @ignore */ readOnly?: boolean; // TODO: `disableForceLimit`, `characterLimitMsgGenerator`, // `exceedLimitMsgGenerator` are deprecated /** @deprecated Please use `allowExceed` */ disableForceLimit?: boolean; /** @deprecated Please use `limitMsg` */ characterLimitMsgGenerator?: (_charsLeft: number) => string; /** @deprecated Please use `exceedLimitMsg` */ exceedLimitMsgGenerator?: (_excessChars: number) => string; /** Rest Props */ [rest: string]: any; } const TextArea = forwardRef( ( { style = {}, className, maxLength, allowExceed = false, onChange = noop, onFocus = noop, onBlur = noop, name, label, error = '', limitMsg = 'Characters left: {{charactersLeft}}', exceedLimitMsg = 'Exceed characters:{{charactersExceed}}', value, defaultValue = '', readOnly = false, // TODO= `disableForceLimit`, `characterLimitMsgGenerator`, // `exceedLimitMsgGenerator` are deprecated disableForceLimit = false, characterLimitMsgGenerator = characterLimitMsgFunc, exceedLimitMsgGenerator = exceedLimitMsgFunc, ...rest }: Props, ref ) => { const [focused, setFocused] = useState(false); const [_value, setValue] = useState(value || defaultValue || ''); useEffect(() => { if (onChange !== noop) { setValue(value || ''); } }, [onChange, value]); useEffect(() => { if ((value || value === '') && onChange === noop && !readOnly) { // eslint-disable-next-line no-console console.error( '[TextArea] You provided a `value` prop to a form field without an `onChange` handler.' + '\nThis will render a read-only field. If the field should be mutable use' + ' `defaultValue`. Otherwise, set either `onChange` or `readOnly`.' ); } if ( disableForceLimit || characterLimitMsgGenerator !== characterLimitMsgFunc || exceedLimitMsgGenerator !== exceedLimitMsgFunc ) { // eslint-disable-next-line no-console console.warn( '[TextArea] prop `disableForceLimit`, `characterLimitMsgGenerator`,' + ' `exceedLimitMsgGenerator` are deprecated. Please check documentation for better' + ' alternative.' ); } }, []); const handleChange = e => { if ((value || value === '') && !defaultValue && onChange === noop) { return; } setValue(e.target.value); onChange(e); }; const handleFocus = e => { setFocused(true); onFocus(e); }; const handleBlur = e => { setFocused(false); onBlur(e); }; const count = _value.length; const charactersLeft = (maxLength ?? 0) - count; // TODO: `characterLimitMsgGenerator`, `exceedLimitMsgGenerator` are deprecated const shownLimitMsg = characterLimitMsgGenerator !== characterLimitMsgFunc ? characterLimitMsgGenerator(charactersLeft) : limitMsg; const shownExceedLimitMsg = exceedLimitMsgGenerator !== exceedLimitMsgFunc ? exceedLimitMsgGenerator(-charactersLeft) : exceedLimitMsg; const message = charactersLeft >= 0 ? shownLimitMsg : shownExceedLimitMsg; return ( 0} error={error.length > 0} focused={focused} style={style} className={className} > {maxLength && ( {message .replace('{{charactersLeft}}', `${charactersLeft}`) .replace( '{{charactersExceed}}', `${charactersLeft < 0 ? charactersLeft * -1 : 0}` ) .replace('{{count}}', `${count}`)} )} ); } ); export default TextArea;