import React, { Ref, RefObject, forwardRef, useImperativeHandle, useRef, useState, } from 'react'; import IconButton from './IconButton'; import Style from './Input.less'; interface InputProps { value?: string; type?: string; placeholder?: string; className?: string; clearable?: boolean; autoFocus?: boolean; onChange: (value: string) => void; onEnter?: (value: string) => void; onFocus?: () => void; } const Input = forwardRef((props: InputProps, ref) => { const $input = useRef(null); const { value, type = 'text', placeholder = '', className = '', onChange, clearable, autoFocus, onEnter = () => {}, onFocus = () => {}, } = props; useImperativeHandle( ref, () => ({ // @ts-ignore focus: () => $input.current.focus(), }), [], ); function handleInput(e: any) { onChange(e.target.value); } const [lockEnter, setLockEnter] = useState(false); function handleIMEStart() { setLockEnter(true); } function handleIMEEnd() { setLockEnter(false); } function handleKeyDown(e: any) { if (lockEnter) { return; } if (e.key === 'Enter') { onEnter(value as string); } } function handleClickClear() { onChange(''); // @ts-ignore $input.current.focus(); } return (
{clearable !== false && ( )}
); }); export default Input;