searchHead.js 2.17 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 (
zhanghaozhe committed
44
            <div className="search-head" style={this.props.style} ref={this.props.forwardedRef}>
zhanghaozhe committed
45

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

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

baiguangyao committed
69 70 71
        )
    }
}
zhanghaozhe committed
72

zhanghaozhe committed
73
export default withRouter(SearchHead)