import { useRef, useState, type ReactNode, type ChangeEvent, type FocusEvent, type KeyboardEvent, type MouseEvent, } from "react"; import clsx from 'clsx'; import "./rds-text-area.scss"; export { TextareaState, TextareaStyle } from './rds-text-area-types'; import { TextareaState, TextareaStyle } from './rds-text-area-types'; export interface RdsTextAreaProps { rows?: number; label?: string; placeholder?: string; value?: string; state?: TextareaState; style?: TextareaStyle; showTitle?: boolean; isMandatory?: boolean; id?: string; dataTestId?: string; onChange?: (event: ChangeEvent) => void; onClick?: (event: MouseEvent) => void; onKeyDown?: (event: KeyboardEvent) => void; onFocus?: (event: FocusEvent) => void; onBlur?: (event: FocusEvent) => void; reset?: boolean; validationPattern?: RegExp; validationMsg?: string; isMultiUrl?: boolean; customClasses?: string; } const RdsTextArea = (props: RdsTextAreaProps): ReactNode => { const [isValid, setIsValid] = useState(true); const [isMandatoryValid, setIsMandatoryValid] = useState( !props.isMandatory || (props.value ?? '').trim().length > 0 ); const [currentState, setCurrentState] = useState(props.state || TextareaState.Default); const prevResetRef = useRef(Boolean(props.reset)); const prevStateRef = useRef(props.state); const prevValueRef = useRef(props.value); const prevMandatoryRef = useRef(props.isMandatory); const idRef = useRef(props.id || `rds-textarea-${Math.random().toString(36).slice(2)}`); const assignedId = props.id || idRef.current; const errorId = `${assignedId}-error`; const reset = Boolean(props.reset); if (reset !== prevResetRef.current) { if (reset) { setIsValid(true); setIsMandatoryValid(true); } prevResetRef.current = reset; } const propState = props.state || TextareaState.Default; if (propState !== prevStateRef.current) { prevStateRef.current = propState; setCurrentState(propState); } if (props.value !== prevValueRef.current || props.isMandatory !== prevMandatoryRef.current) { prevValueRef.current = props.value; prevMandatoryRef.current = props.isMandatory; if (props.isMandatory) { setIsMandatoryValid((props.value || '').trim().length > 0); } else { setIsMandatoryValid(true); } } const handleChange = (e: ChangeEvent) => { const inputValue = e.target.value; if (props.isMandatory) { setIsMandatoryValid(inputValue.trim().length > 0); } if (props.validationPattern) { const urlPattern = props.validationPattern; const urlValid = props.isMultiUrl ? inputValue.split("\n").every((url) => urlPattern.test(url)) : urlPattern.test(inputValue); setIsValid(urlValid); } props.onChange?.(e); }; const handleFocus = (e: FocusEvent) => { if (currentState !== TextareaState.Disabled && currentState !== TextareaState.Error) { setCurrentState(TextareaState.Active); } props.onFocus?.(e); }; const handleBlur = (e: FocusEvent) => { const inputValue = e.target.value; if (props.isMandatory) { setIsMandatoryValid(inputValue.trim().length > 0); } if (currentState === TextareaState.Active) { setCurrentState(inputValue ? TextareaState.Selected : TextareaState.Default); } props.onBlur?.(e); }; const getStateClass = () => { switch (currentState) { case TextareaState.Active: return "textarea-active"; case TextareaState.Selected: return "textarea-selected"; case TextareaState.Error: return "textarea-error"; case TextareaState.Disabled: return "textarea-disabled"; default: return "textarea-default"; } }; const getStyleClass = () => { switch (props.style) { case TextareaStyle.Pill: return "textarea-pill"; case TextareaStyle.BottomOutline: return "textarea-bottom-outline"; default: return "textarea-style-default"; } }; const isDisabled = currentState === TextareaState.Disabled; const isError = currentState === TextareaState.Error || !isValid || (props.isMandatory && !isMandatoryValid); const getValidationMessage = () => { if (props.isMandatory && !isMandatoryValid) { return `${props.label || 'Label'} is required`; } return props.validationMsg; }; return (
{props.showTitle !== false && props.label && ( )}