import React, { useRef, useState, useImperativeHandle, InputHTMLAttributes, } from 'react'; import './styles/index.scss'; import { Icon, IconTypes } from '../Icon'; export interface InputProps extends Omit, 'size' | 'prefix' | 'type'> { className?: string, customClassName?: string, placeholder?: string, clearable?: boolean, prefix?: React.ReactNode, suffix?: React.ReactNode, value?: InputHTMLAttributes['value'], border?: '' | 'bottom', disabled?: boolean, maxLength?: number, onKeyDown?: (options?: object) => void } export interface InputRef { focus: (options?: object) => void; blur: () => void; input: HTMLInputElement | null; } export const Input = React.forwardRef( (props:InputProps, ref):React.ReactElement => { const { className = '', customClassName = '', placeholder, clearable = false, prefix, suffix, onChange, onBlur, onFocus, onKeyDown, value: propsValue = '', border = '', disabled = false, maxLength = undefined, } = props; const [focused, setFocused] = useState(false); const enterCodeList = ['Enter', 'NumpadEnter']; const [value, setValue] = useState(propsValue); const inputRef = useRef(null); const handleFocus = (e) => { setFocused(true); onFocus?.(e); }; const handleBlur = (e) => { setFocused(false); onBlur?.(e); }; const handleEnterKeyDown = (e) => { if (enterCodeList.indexOf(e?.key) > -1 && onKeyDown) { e?.preventDefault(); onKeyDown(e); } }; const clearInput = (e: React.MouseEvent) => { setValue(''); focus(); const currentTarget = inputRef.current.cloneNode(true) as HTMLInputElement; const event = Object.create(e, { target: { value: currentTarget }, currentTarget: { value: currentTarget }, }); currentTarget.value = ''; onChange?.(event as React.ChangeEvent); }; const handleChange = (e: React.ChangeEvent) => { setValue(e.target.value); if (inputRef.current) { onChange?.(e); } }; const focus = () => { if (inputRef.current) { inputRef.current.focus(); } }; useImperativeHandle(ref, () => ({ focus, blur: () => { inputRef.current?.blur(); }, input: inputRef.current, })); return (
{prefix} {suffix} {(clearable && value) && }
); }, );