import React, { useEffect } from 'react' import './Input.css' import classNames from 'classnames' import { IInputProps } from '../type' const Input: React.FC = ({ type = 'text', multiline = false, minHeight = 25, maxHeight = 200, autoHeight = true, autofocus = false, ...props }) => { useEffect(() => { if (autofocus === true) props.referance?.current?.focus() if (props.clear instanceof Function) { props.clear(clear) } }, []) const onChangeEvent = (e: any) => { if (multiline === true) { if (autoHeight === true) { if (e.target.style.height !== minHeight + 'px') { e.target.style.height = minHeight + 'px' } let height if (e.target.scrollHeight <= maxHeight) height = e.target.scrollHeight + 'px' else height = maxHeight + 'px' if (e.target.style.height !== height) { e.target.style.height = height } } } if (props.maxlength && (e.target.value || '').length > props.maxlength) { if (props.onMaxLengthExceed instanceof Function) props.onMaxLengthExceed() if (props.referance) { props.referance.current.value = (e.target.value || '').substring(0, props.maxlength) } return } if (props.onChange instanceof Function) props.onChange(e) } const clear = () => { var _event = { FAKE_EVENT: true, target: props.referance?.current, } if (props.referance?.current?.value) { props.referance.current.value = '' } onChangeEvent(_event) } return (
{props.leftButtons &&
{props.leftButtons}
} {multiline === false ? ( ) : ( )} {props.rightButtons &&
{props.rightButtons}
}
) } export default Input