index.js 1.13 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7
import React, { Component } from 'react';
import './clearable-input.scss'
import classnames from 'classnames'

class ClearableInput extends Component {

    render() {
zhanghaozhe committed
8 9 10 11 12 13 14
        let {
            value,
            name,
            wrapperClass,
            inputClass,
            type = 'text',
            icon,
zhanghaozhe committed
15
            setFieldValue,
zhanghaozhe committed
16 17
            ...rest
        } = this.props
zhanghaozhe committed
18
        let clearIconStyle = {
zhanghaozhe committed
19
            display: value && value.length ? 'block' : 'none'
zhanghaozhe committed
20 21 22 23 24
        }
        return (
            <div className={classnames('clearable-input-wrapper', wrapperClass)}>
                <input
                    type={type}
zhanghaozhe committed
25
                    value={value}
zhanghaozhe committed
26 27
                    className={inputClass}
                    {...rest}
zhanghaozhe committed
28
                    name={name}
zhanghaozhe committed
29
                />
30
                {icon}
zhanghaozhe committed
31
                <i
32
                    className={'iconfont icondanseshixintubiao-3 clear'}
zhanghaozhe committed
33
                    onClick={() => {
zhanghaozhe committed
34
                        setFieldValue(name,'')
zhanghaozhe committed
35
                    }}
zhanghaozhe committed
36 37 38 39 40 41 42 43
                    style={clearIconStyle}
                />
            </div>
        );
    }
}

export default ClearableInput;