import { useRef, useState, type InputHTMLAttributes, type ReactNode, type ChangeEvent, type KeyboardEvent, type FormEvent } from 'react'; import { TextField as MuiTextField, type TextFieldProps, InputAdornment } from '@mui/material'; import type { InputProps } from '@mui/material/Input'; import clsx from 'clsx'; import './rds-input.scss'; function getInputPlaceholder(layoutType: RdsInputProps['layout'] | undefined): string { switch (layoutType) { case 'password': return '••••••••'; case 'phone number': return 'Enter Phone Number'; case 'number': return 'Enter Number'; case 'card number': return 'XXXX XXXX XXXX XXXX'; case 'text': default: return 'Placeholder Text'; } } export interface RdsInputProps extends Omit { label?: string; placeholder?: string; hintText?: string; errorMessage?: string; isMandatory?: boolean; variant?: 'outlined' | 'filled' | 'standard'; size?: 'small' | 'medium' | 'large'; layout?: 'text' | 'password' | 'phone number' | 'number' | 'card number'; titlePosition?: string; style?: 'default' | 'pill' | 'bottom outline'; state?: 'default'|'active' | 'selected' | 'error' | 'disabled'; showIcon?: boolean; iconPosition?: 'start' | 'end'; icon?: ReactNode; // Custom icon component provided by the user } const RdsInput = ({ label, placeholder, hintText, errorMessage, isMandatory = false, variant = 'outlined', size = 'small', layout = 'text', titlePosition, style = 'default', state = 'default', showIcon = false, iconPosition = 'end', icon, error, disabled, value, onChange, onFocus, onBlur, InputProps: legacyInputProps, slotProps: consumerSlotProps, ...props }: RdsInputProps) => { const [showPassword, setShowPassword] = useState(false); const [isFocused, setIsFocused] = useState(false); const [internalValue, setInternalValue] = useState(''); const prevLayoutRef = useRef(layout); const isControlled = value !== undefined; if (layout !== prevLayoutRef.current) { prevLayoutRef.current = layout; if (!isControlled) { setInternalValue(''); } } const handleTogglePasswordVisibility = () => { setShowPassword(!showPassword); }; const handleInternalChange = (event: ChangeEvent) => { if (!isControlled) { setInternalValue(event.target.value); } onChange?.(event); }; const currentValue = isControlled ? value : internalValue; const sizeClass = size === 'small' ? 'rds-input--small' : size === 'medium' ? 'rds-input--medium' : 'rds-input--large'; const pillClass = style === 'pill' ? 'rds-input--pill' : style === 'bottom outline' ? 'rds-input--bottom-outline' : ''; const isFocusedDefault = state === 'default' && isFocused; const active = state === 'active' || isFocusedDefault; let stateClass = ''; if (state === 'error' || error) { stateClass = 'rds-input--error'; } else if (state === 'disabled' || disabled) { stateClass = 'rds-input--disabled'; } else if (state === 'selected' || isFocusedDefault) { stateClass = 'rds-input--selected'; } else if (state === 'active') { stateClass = 'rds-input--active'; } let inputType: string = 'text'; switch (layout) { case 'password': inputType = showPassword ? 'text' : 'password'; break; case 'number': case 'card number': inputType = 'tel'; break; case 'phone number': inputType = 'tel'; break; default: inputType = 'text'; } const renderIcon = () => { if (!showIcon) return null; if (icon) { return ( {icon} ); } return ( {icon} ); }; const handleNumericKeyDown = (e: KeyboardEvent) => { const allowedKeys = [ 'Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'Tab', ]; if (e.ctrlKey || e.metaKey) return; const input = e.currentTarget; const selectionStart = input.selectionStart ?? input.value.length; const selectionEnd = input.selectionEnd ?? selectionStart; const hasPlus = input.value.startsWith('+'); const isPlusKey = e.key === '+'; const isDigit = /^[0-9]$/.test(e.key); if (isPlusKey && layout === 'phone number') { const atStart = selectionStart === 0; const replacingPlus = hasPlus && selectionStart === 0 && selectionEnd > 0; if (!atStart || (hasPlus && !replacingPlus)) { e.preventDefault(); } return; } else if (isPlusKey && layout !== 'phone number') { e.preventDefault(); return; } if (!isDigit && !allowedKeys.includes(e.key)) { e.preventDefault(); return; } if (isDigit && layout === 'phone number') { const value = input.value; const selectedSegment = value.slice(selectionStart, selectionEnd); const digitsInValue = value.replace(/\D/g, '').length; const digitsInSelection = selectedSegment.replace(/\D/g, '').length; const maxDigits = hasPlus ? 12 : 10; if (digitsInValue - digitsInSelection >= maxDigits) { e.preventDefault(); } } }; const handlePhoneInput = (e: FormEvent) => { const target = e.target as HTMLInputElement; const orig = target.value; const startsWithPlus = orig.startsWith('+'); let digits = orig.replace(/\D/g, ''); const maxDigits = startsWithPlus ? 12 : 10; if (digits.length > maxDigits) digits = digits.slice(0, maxDigits); const next = (startsWithPlus ? '+' : '') + digits; if (orig !== next) { target.value = next; if (!isControlled) { setInternalValue(next); } if (onChange) { const event = { ...e, target: { ...target, value: next } } as ChangeEvent; onChange(event); } } }; const computedPlaceholder = placeholder ?? getInputPlaceholder(layout); const inlineTitleClass = titlePosition === 'inline-title' ? 'rds-input--inline-title' : ''; const numericInputProps: InputHTMLAttributes = layout === 'phone number' || layout === 'number' || layout === 'card number' ? { inputMode: layout === 'phone number' ? 'tel' : 'numeric', ...(layout === 'phone number' ? { pattern: '^(?:\\+\\d{12}|\\d{10})$' } : {}), onKeyDown: handleNumericKeyDown, ...(layout === 'phone number' ? { onInput: handlePhoneInput } : {}), } : {}; const consumerInputProps = typeof consumerSlotProps?.input === 'object' && consumerSlotProps?.input !== null ? (consumerSlotProps.input as InputProps) : undefined; const inputSlotProps: InputProps = { ...legacyInputProps, ...consumerInputProps, className: clsx( 'rds-input__field', legacyInputProps?.className, consumerInputProps?.className, ), classes: { root: active ? 'Mui-focused' : '', focused: active ? 'Mui-focused' : '', ...legacyInputProps?.classes, ...consumerInputProps?.classes, }, startAdornment: iconPosition === 'start' && showIcon ? renderIcon() : consumerInputProps?.startAdornment ?? legacyInputProps?.startAdornment, endAdornment: iconPosition === 'end' && showIcon ? renderIcon() : consumerInputProps?.endAdornment ?? legacyInputProps?.endAdornment, }; return (
{titlePosition === 'title-above' && label && ( )} { setIsFocused(true); onFocus?.(e); }} onBlur={(e) => { setIsFocused(false); onBlur?.(e); }} slotProps={{ ...consumerSlotProps, input: inputSlotProps, htmlInput: { ...numericInputProps, ...consumerSlotProps?.htmlInput, }, }} {...props} />
); }; RdsInput.displayName = 'RdsInput'; export default RdsInput;