/* eslint-disable react/display-name */ import type { InputHTMLAttributes } from 'react'; import React, { useRef, useState, useImperativeHandle, } from 'react'; import './index.scss'; import { IconCloseCircleFilled } from '@tencentcloud/uikit-base-component-react'; export interface InputLegacyProps extends Omit, 'size' | 'prefix' | 'type'> { className?: string | undefined; inputClassName?: string | undefined; placeholder?: string | undefined; clearable?: boolean | undefined; prefix?: React.ReactNode | undefined; suffix?: React.ReactNode | undefined; value?: InputHTMLAttributes['value'] | undefined; border?: '' | 'bottom' | undefined; disabled?: boolean | undefined; maxLength?: number | undefined; onChange?: ((event: React.ChangeEvent) => void) | undefined; onBlur?: ((event: React.FocusEvent) => void) | undefined; onFocus?: ((event: React.FocusEvent) => void) | undefined; onKeyDown?: ((event: React.KeyboardEvent) => void) | undefined; } export interface InputLegacyRef { focus: (options?: object) => void; blur: () => void; input: HTMLInputElement | null; } export const InputLegacy = React.forwardRef( (props: InputLegacyProps, ref): React.ReactElement => { const { className = '', inputClassName = '', 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: any) => { setFocused(true); onFocus?.(e); }; const handleBlur = (e: any) => { setFocused(false); onBlur?.(e); }; const handleEnterKeyDown = (e: any) => { if (enterCodeList.indexOf(e?.key) > -1 && onKeyDown) { e?.preventDefault(); onKeyDown(e); } }; const focus = () => { if (inputRef.current) { inputRef.current.focus({ preventScroll: true, }); } }; 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); } }; useImperativeHandle(ref, () => ({ focus, blur: () => { inputRef.current?.blur(); }, input: inputRef.current, })); return (
{prefix} {suffix} {(clearable && value) && (
{ clearInput(e); }} >
)}
); }, );