import React, { useEffect, useState } from 'react' import { observer } from 'mobx-react' import clsx from 'clsx' import { FormStoreType, FormFields } from '../../../store' interface TextInputProps extends React.InputHTMLAttributes { name: string formStore: FormStoreType placeholder?: string } const TextInput = observer( ({ name, formStore, className, placeholder, ...rest }: TextInputProps) => { const [inputValue, setInputValue] = useState(formStore.fields[name] || '') const [isFocused, setIsFocused] = useState(false) useEffect(() => { setInputValue(formStore.fields[name] || '') }, [formStore.fields[name]]) const handleInputChange = (e: React.ChangeEvent) => { const newValue = e.target.value setInputValue(newValue) formStore.setField(name, newValue as unknown as T[keyof T]) } const handleFocus = () => { setIsFocused(true) } const handleBlur = (e: React.FocusEvent) => { setIsFocused(false) } const inputClassName = clsx('w-full border p-2 text-black', className) const labelClassName = clsx( 'transition-all ease-in-out duration-300', { 'text-sm': isFocused || inputValue, 'text-base': !isFocused && !inputValue, }, 'absolute px-2 left-2', { 'top-0': isFocused || inputValue, 'top-1/2 transform -translate-y-1/2': !isFocused && !inputValue, }, ) return (
{formStore.errors[name] && ( {formStore.errors[name]} )}
) }, ) export default TextInput