import * as Nerv from 'nervjs' import Component from '@lib/component' interface InputProps { type?: string value?: string | number name?: string placeholder?: string icon?: string prependButton?: boolean appendButton?: boolean readonly?: boolean disabled?: boolean autofocus?: boolean maxlength?: number minlength?: number max?: number min?: number } interface InputState { currentValue?: string | number } class Input extends Component { static defaultProps = { type: 'text', prependButton: false, appendButton: false, readonly: false, disabled: false, autofocus: false } constructor (...args) { super(...args) this.onBlurHandle = this.onBlurHandle.bind(this) this.onFoucsHandle = this.onFoucsHandle.bind(this) this.onInputHandle = this.onInputHandle.bind(this) this.state = { currentValue: this.props.value } } componentWillReceiveProps (nextProps) { if (nextProps.value === this.state.currentValue) { return } this.setState({ currentValue: nextProps.value }) } onFoucsHandle (evt) { const { onFocus } = this.props if (onFocus) { onFocus(evt) } } onBlurHandle (evt) { const { onBlur } = this.props if (onBlur) { onBlur(evt) } } onInputHandle (evt) { const { onInput } = this.props if (onInput) { if (evt.target instanceof HTMLInputElement) { const val = evt.target.value this.setState({ currentValue: val }, () => { onInput(val, evt) }) } } } render () { const { type, size, status, prepend, prependButton, append, appendButton, icon, disabled, placeholder, min, max, minlength, maxlength, readonly, autofocus, name } = this.props const iconName = icon || status const iconClass = iconName ? `icon-${iconName}` : '' const { currentValue } = this.state return (
{prepend ? (
{prepend}
) : ( '' )} {icon ? : ''} {append ? (
{append}
) : ( '' )}
) } } export default Input