searchHead.js 2.19 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 = () => {
FE committed
28 29 30 31
        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
32 33
    }

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

baiguangyao committed
40
    render() {
FE committed
41 42
        const { isFocus } = this.state;
        const cls = classnames('submit-btn', {'submit-btn--active': isFocus})
baiguangyao committed
43
        return (
FE committed
44
            
zhanghaozhe committed
45
            <div className="search-head" style={this.props.style} ref={this.props.forwardedRef}>
zhanghaozhe committed
46

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

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

baiguangyao committed
70 71 72
        )
    }
}
zhanghaozhe committed
73

zhanghaozhe committed
74
export default withRouter(SearchHead)