searchHead.js 1.92 KB
Newer Older
zhanghaozhe committed
1
import React, { PureComponent } from 'react'
baiguangyao committed
2
import { SearchBar } from 'antd-mobile'
zhanghaozhe committed
3
import { withRouter } from 'react-router-dom'
FE committed
4
import classnames from 'classnames';
baiguangyao committed
5 6 7

import './search_header.scss'

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

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

baiguangyao committed
18 19 20
    componentDidMount() {
        this.refs.search.focus();
    }
zhanghaozhe committed
21

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

    storeKeyword = () => {
zhanghaozhe committed
28 29
        let {searchHistory = [], value} = this.props
        value && localStorage.setItem('searchHistory', JSON.stringify([...searchHistory, value]))
zhanghaozhe committed
30 31
    }

FE committed
32 33 34 35 36
    changeFontColor = (isFocus) => {
        this.setState({
            isFocus
        });
    }
zhanghaozhe committed
37

baiguangyao committed
38
    render() {
FE committed
39 40 41
        const { isFocus } = this.state;
        const cls = classnames('submit-btn', {'submit-btn--active': isFocus})
        console.log(isFocus);
baiguangyao committed
42 43
        return (
            <div className="search-head">
zhanghaozhe committed
44

zhanghaozhe committed
45
                <div className="left" onClick={this.returnPage}>
zhanghaozhe committed
46
                    <i className="iconfont iconiconfront-68"/>
zhanghaozhe committed
47 48
                </div>

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

zhanghaozhe committed
70
export default withRouter(SearchHead)