import { ChangeEvent, ComponentProps, ComponentPropsWithoutRef, forwardRef, useState } from 'react' import { Eye, Search, VisibilityOff } from '@/assets' import { Typography } from '@/components' import { clsx } from 'clsx' import s from './text-field.module.scss' export type TextFieldProps = { containerProps?: ComponentProps<'div'> errorMessage?: string label?: string labelProps?: ComponentProps<'label'> onValueChange?: (value: string) => void search?: boolean } & ComponentPropsWithoutRef<'input'> export const TextField = forwardRef( ( { className, containerProps, errorMessage, label, labelProps, onChange, onValueChange, placeholder, search, type, ...restProps }, ref ) => { const [showPassword, setShowPassword] = useState(false) const isShowPasswordButtonShown = type === 'password' const finalType = getFinalType(type, showPassword) function handleChange(e: ChangeEvent) { onChange?.(e) onValueChange?.(e.target.value) } const classNames = { error: clsx(s.error), field: clsx(s.field, !!errorMessage && s.error, search && s.hasLeadingIcon, className), fieldContainer: clsx(s.fieldContainer), label: clsx(s.label, labelProps?.className), leadingIcon: s.leadingIcon, root: clsx(s.root, containerProps?.className), } return (
{label && ( {label} )}
{search && } {isShowPasswordButtonShown && ( )}
{errorMessage}
) } ) function getFinalType(type: ComponentProps<'input'>['type'], showPassword: boolean) { if (type === 'password' && showPassword) { return 'text' } return type }