index.js 1.19 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from "react"
import "./input.scss"
zhanghaozhe committed
3

zhanghaozhe committed
4
import classnames from "classnames"
zhanghaozhe committed
5 6

class Input extends Component {
zhanghaozhe committed
7 8 9 10
  constructor(props) {
    super(props)
    this.state = {
      isFocus: false,
zhanghaozhe committed
11
    }
zhanghaozhe committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  }

  clearInput = () => {
    this.props.onChange("")
  }

  render() {
    let { type, placeholder } = this.props
    const { isFocus } = this.state
    return (
      <div className="custom-input-wrapper">
        <input
          type={type}
          className={classnames("custom-input", {
            "custom-input--active": isFocus,
          })}
          placeholder={placeholder}
          onChange={this.props.onChange}
          value={this.props.value}
          onBlur={() => {
            this.setState({
              isFocus: false,
            })
          }}
          onFocus={() => {
            this.setState({
              isFocus: true,
            })
          }}
        />
        <i
          className={classnames("iconfont icondanseshixintubiao-3", {
            hide: this.props.value.length === 0,
          })}
          onClick={this.clearInput}
        />
      </div>
    )
  }
zhanghaozhe committed
51 52
}

zhanghaozhe committed
53
Input.defaultProps = { type: "text", placeholder: "" }
zhanghaozhe committed
54

zhanghaozhe committed
55
export default Input