index.js 2.26 KB
Newer Older
1
import React, { PureComponent } from 'react';
afc163's avatar
afc163 committed
2
import PropTypes from 'prop-types';
3 4 5 6 7 8 9
import { Input, Icon, AutoComplete } from 'antd';
import classNames from 'classnames';
import styles from './index.less';

export default class HeaderSearch extends PureComponent {
  static defaultProps = {
    defaultActiveFirstOption: false,
afc163's avatar
afc163 committed
10 11 12 13 14
    onPressEnter: () => {},
    onSearch: () => {},
    className: '',
    placeholder: '',
    dataSource: [],
15
    defaultOpen: false,
afc163's avatar
afc163 committed
16 17 18 19 20 21 22 23
  };
  static propTypes = {
    className: PropTypes.string,
    placeholder: PropTypes.string,
    onSearch: PropTypes.func,
    onPressEnter: PropTypes.func,
    defaultActiveFirstOption: PropTypes.bool,
    dataSource: PropTypes.array,
24
    defaultOpen: PropTypes.bool,
25 26
  };
  state = {
27
    searchMode: this.props.defaultOpen,
28 29 30 31 32
    value: '',
  };
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
jim's avatar
jim committed
33
  onKeyDown = e => {
34 35
    if (e.key === 'Enter') {
      this.timeout = setTimeout(() => {
afc163's avatar
afc163 committed
36
        this.props.onPressEnter(this.state.value); // Fix duplicate onPressEnter
37 38
      }, 0);
    }
jim's avatar
jim committed
39 40
  };
  onChange = value => {
41
    this.setState({ value });
42 43 44
    if (this.props.onChange) {
      this.props.onChange();
    }
jim's avatar
jim committed
45
  };
46 47 48
  enterSearchMode = () => {
    this.setState({ searchMode: true }, () => {
      if (this.state.searchMode) {
afc163's avatar
afc163 committed
49
        this.input.focus();
50 51
      }
    });
jim's avatar
jim committed
52
  };
53 54 55 56 57
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
58
  };
59 60
  render() {
    const { className, placeholder, ...restProps } = this.props;
61
    delete restProps.defaultOpen; // for rc-select not affected
62 63 64 65
    const inputClass = classNames(styles.input, {
      [styles.show]: this.state.searchMode,
    });
    return (
jim's avatar
jim committed
66
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
67
        <Icon type="search" key="Icon" />
68
        <AutoComplete
陈帅's avatar
陈帅 committed
69
          key="AutoComplete"
70
          {...restProps}
71 72 73 74 75 76
          className={inputClass}
          value={this.state.value}
          onChange={this.onChange}
        >
          <Input
            placeholder={placeholder}
jim's avatar
jim committed
77 78 79
            ref={node => {
              this.input = node;
            }}
80 81 82 83 84 85 86 87
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}