import { useFormContext } from 'react-hook-form' import { motion, AnimatePresence } from 'framer-motion' import clsx from 'clsx' import React, { useState, type Key } from 'react' import { Icon } from '../../../atoms/Icon' import { inputErrorMotion } from './helpers' import { findInputError } from '../../../../utils/findInputError' import { isFormInvalid } from '../../../../utils/isFormInvalid' import { getGlobalStyle } from '../../../../helpers' interface InputProps { name: string type?: string value?: string width?: string id: string label?: string placeholder?: string classNames?: string autoComplete?: string defaultValue?: string showEye?: boolean icon?: string style?: React.CSSProperties generatePassword?: () => void onChange?: (e: React.ChangeEvent) => void } export const InputText: React.FC = ({ name, type, value, icon = '', id, width, placeholder, defaultValue, autoComplete, showEye, label, classNames, style, onChange, generatePassword }: InputProps & { label?: string }) => { // STATES const [show, setShow] = useState(false) // HOOKS const { register, formState: { errors } } = useFormContext() const inputError = findInputError(errors, name) const isInvalid = isFormInvalid(inputError) const handleShowEye = (): void => { return setShow(!show) } const styles = { zIndex: 10, height: '2.5rem', // 12 * 0.25rem = 3rem width: '100%', borderRadius: '0.5rem', // rounded-lg = 0.5rem borderWidth: '2px', borderColor: '#cbd5e1', // slate-300 paddingLeft: '0.75rem', // px-3 = 0.75rem paddingRight: '0.75rem', transition: 'border-color 0.3s', // For hover effect border: `2px solid ${getGlobalStyle('--color-neutral-gray')}`, outline: 'none', } return (
{icon !== '' && ( )} { e.target.style.borderColor = getGlobalStyle('--color-primary-purply') }} type={show ? 'text' : type} style={{ ...styles, ...style, borderColor: getGlobalStyle('--color-neutral-gray') }} /> {isInvalid && ( )} {showEye && ( )} {typeof generatePassword === 'function' && ( )}
) } function InputError ({ message }: { message: string }): React.ReactNode { return ( {message} ) }