searchHead.js 1.9 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { PureComponent } from "react"
import { SearchBar } from "antd-mobile"
import { withRouter } from "react-router-dom"
import classnames from "classnames"
baiguangyao committed
5

zhanghaozhe committed
6
import "./search_header.scss"
baiguangyao committed
7

zhanghaozhe committed
8
class SearchHead extends PureComponent {
zhanghaozhe committed
9 10 11
  state = {
    isFocus: false,
  }
zhanghaozhe committed
12

zhanghaozhe committed
13 14 15
  returnPage = () => {
    this.props.history.go(-1)
  }
zhanghaozhe committed
16

zhanghaozhe committed
17 18 19
  componentDidMount() {
    this.refs.search.focus()
  }
zhanghaozhe committed
20

zhanghaozhe committed
21 22 23 24
  search = () => {
    this.storeKeyword()
    this.props.handleSearch()
  }
zhanghaozhe committed
25

zhanghaozhe committed
26 27 28 29 30 31 32 33
  storeKeyword = () => {
    const { searchHistory = [], value } = this.props
    const data = searchHistory.some((item) => item === value)
      ? searchHistory
      : searchHistory.concat([value])
    // value && localStorage.setItem('searchHistory', JSON.stringify([...searchHistory, value]))
    localStorage.setItem("searchHistory", JSON.stringify(data))
  }
zhanghaozhe committed
34

zhanghaozhe committed
35 36 37 38 39
  changeFontColor = (isFocus) => {
    this.setState({
      isFocus,
    })
  }
zhanghaozhe committed
40

zhanghaozhe committed
41 42 43 44 45 46 47 48 49 50 51 52
  render() {
    const { isFocus } = this.state
    const cls = classnames("submit-btn", { "submit-btn--active": isFocus })
    return (
      <div
        className="search-head"
        style={this.props.style}
        ref={this.props.forwardedRef}
      >
        <div className="left" onClick={this.returnPage}>
          <i className="iconfont iconiconfront-68" />
        </div>
zhanghaozhe committed
53

zhanghaozhe committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        <div className="center">
          <SearchBar
            value={this.props.value}
            showCancelButton
            cancelText={" "}
            ref="search"
            focus={true}
            onChange={this.props.handleChange}
            placeholder="搜索课程"
            onFocus={() => this.changeFontColor(true)}
            onBlur={() => this.changeFontColor(false)}
            onSubmit={this.search}
          />
        </div>
        <div className="right right-btn" onClick={this.search}>
          <div className={cls}>搜索</div>
        </div>
      </div>
    )
  }
baiguangyao committed
74
}
zhanghaozhe committed
75

zhanghaozhe committed
76
export default withRouter(SearchHead)