import { X } from "lucide-react" import * as React from "react" import { cn } from "@/lib/utils" interface InputProps extends React.ComponentProps<"input"> { clearable?: boolean onClear?: () => void } function Input({ className, type, clearable, onClear, value, onChange, ...props }: InputProps) { const [showClear, setShowClear] = React.useState(false) const inputRef = React.useRef(null) const hasValue = value !== undefined && value !== null && String(value).length > 0 // 监听输入框焦点状态 const handleFocus = () => setShowClear(true) const handleBlur = () => setShowClear(false) const handleClear = () => { if (onClear) { onClear() } else if (onChange) { // 触发 React 的 change 事件 const event = { target: { value: "" }, currentTarget: { value: "" }, } as React.ChangeEvent onChange(event) } // 清空后重新聚焦 inputRef.current?.focus() } return (
{clearable && hasValue && showClear && ( )}
) } export { Input }