import React, { useEffect, useMemo, useState } from 'react' import useTheme from '../styles/use-theme' import InputLabel from './input-label' import InputBlockLabel from './input-block-label' import InputIcon from './input-icon' import InputClearIcon from './input-icon-clear' import Textarea from '../textarea/textarea' import { getSizes, getColors } from './styles' import { NormalSizes, NormalTypes } from '../utils/prop-types' interface Props { value?: string initialValue?: string placeholder?: string size?: NormalSizes status?: NormalTypes readOnly?: boolean disabled?: boolean label?: string labelRight?: string icon?: React.ReactNode iconRight?: React.ReactNode width?: string className?: string clearable?: boolean onChange?: (e: React.ChangeEvent) => void onClearClick?: (e: React.MouseEvent) => void onFocus?: (e: React.FocusEvent) => void onBlur?: (e: React.FocusEvent) => void autoComplete: string } const defaultProps = { disabled: false, readOnly: false, clearable: false, width: 'initial', size: 'medium', status: 'default', autoComplete: 'off', className: '', placeholder: '', initialValue: '', } type NativeAttrs = Omit, keyof Props> export type InputProps = Props & typeof defaultProps & NativeAttrs const Input: React.FC> = ({ placeholder, label, labelRight, size, status, disabled, icon, iconRight, initialValue, onChange, readOnly, value, onClearClick, clearable, width, className, onBlur, onFocus, autoComplete, children, ...props }) => { const theme = useTheme() const [selfValue, setSelfValue] = useState(initialValue) const [hover, setHover] = useState(false) const { heightRatio, fontSize } = useMemo(() => getSizes(size),[size]) const showClearIcon = useMemo(() => clearable && selfValue !== '', [selfValue, clearable]) const labelClasses = useMemo( () => labelRight ? 'right-label' : (label ? 'left-label' : ''), [label, labelRight], ) const iconClasses = useMemo( () => iconRight ? 'right-icon' : (icon ? 'left-icon' : ''), [icon, iconRight], ) const { color, borderColor, hoverBorder } = useMemo( () => getColors(theme.palette, status), [theme.palette, status], ) const changeHandler = (event: React.ChangeEvent) => { if (disabled || readOnly) return setSelfValue(event.target.value) onChange && onChange(event) } const clearHandler = (event: React.MouseEvent) => { setSelfValue('') onClearClick && onClearClick(event) } const focusHandler = (e: React.FocusEvent) => { setHover(true) onFocus && onFocus(e) } const blurHandler = (e: React.FocusEvent) => { setHover(false) onBlur && onBlur(e) } useEffect(() => { if (value === undefined) return setSelfValue(value) }, [value]) return (
{children && {children}}
{label && {label}}
{icon && } {clearable && } {iconRight && }
{labelRight && {labelRight}}
) } type InputComponent

= React.FC

& { Textarea: typeof Textarea } type ComponentProps = Partial & Omit & NativeAttrs (Input as InputComponent).defaultProps = defaultProps export default Input as InputComponent