import classNames from 'classnames'; import React, { forwardRef, useImperativeHandle, useMemo, useRef, useState } from 'react'; import { IconCheck, IconError, IconPassword, IconPasswordCross } from 'src/components/icons'; import { KEY } from 'src/utils/constanst.utils'; import s from './InputCommon.module.scss'; type Ref = { focus: () => void } | null; interface Props { children?: React.ReactNode, value?: string | number, placeholder?: string, type?: 'text' | 'number' | 'email' | 'password', styleType?: 'default' | 'custom', backgroundTransparent?: boolean, icon?: React.ReactNode, isIconSuffix?: boolean, isShowIconSuccess?: boolean, error?: string, onChange?: (value: string | number) => void, onEnter?: (value: string | number) => void, } const InputCommon = forwardRef(({ value, placeholder, type, styleType = 'default', icon, backgroundTransparent = false, isIconSuffix, isShowIconSuccess, error, onChange, onEnter }: Props, ref) => { const inputElementRef = useRef(null); const iconElement = useMemo(() => { if (error) { return } else if (isShowIconSuccess) { return } else if (icon) { return {icon} } return <> }, [icon, error, isShowIconSuccess]) useImperativeHandle(ref, () => ({ focus: () => { inputElementRef.current?.focus(); }, getValue: () => { const value = inputElementRef.current?.value || '' return value } })); const handleChange = (e: React.ChangeEvent) => { onChange && onChange(e.target.value) } const handleKeyDown = (e: any) => { if (e.key === KEY.ENTER && onEnter) { const value = inputElementRef.current?.value || '' onEnter(value) } } return (
{iconElement}
{ error &&
{error}
}
) }) export default InputCommon