import React, { useState, useRef } from 'react'; import './Input.css'; /** * Kole UI Input(React) * 对齐 input.json 契约: * - size: large | default | small * - state: default | focus | error | disabled * - feature: prefix | suffix | clearable | number | password * 受控用法: * 非受控用法: */ const sizeMap = { large: 'kole-input-lg', default: 'kole-input-md', small: 'kole-input-sm' }; const EyeIcon = () => ( ); const EyeOffIcon = () => ( ); const CloseIcon = () => ( ); export default function Input({ value, defaultValue = '', size = 'default', type = 'text', placeholder = '', disabled = false, error = false, errorText = '', clearable = false, className = '', prefix, suffix, onChange, onFocus, onBlur, ...rest }) { const isControlled = value !== undefined; const [inner, setInner] = useState(defaultValue); const [showPassword, setShowPassword] = useState(false); const inputRef = useRef(null); const current = isControlled ? value : inner; const handleInput = (e) => { const next = e.target.value; if (!isControlled) setInner(next); onChange && onChange(next, e); }; const handleClear = () => { if (!isControlled) setInner(''); onChange && onChange(''); requestAnimationFrame(() => inputRef.current && inputRef.current.focus()); }; const resolvedType = type === 'password' ? (showPassword ? 'text' : 'password') : type; const classes = [ 'kole-input', sizeMap[size], error ? 'is-error' : '', disabled ? 'is-disabled' : '', className ].filter(Boolean).join(' '); return (