index.js 2.48 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
import { Input, Icon, AutoComplete } from 'antd';
import classNames from 'classnames';
import styles from './index.less';

export default class HeaderSearch extends PureComponent {
afc163's avatar
afc163 committed
8 9 10 11 12 13 14
  static propTypes = {
    className: PropTypes.string,
    placeholder: PropTypes.string,
    onSearch: PropTypes.func,
    onPressEnter: PropTypes.func,
    defaultActiveFirstOption: PropTypes.bool,
    dataSource: PropTypes.array,
15
    defaultOpen: PropTypes.bool,
16
  };
jim's avatar
jim committed
17 18 19 20 21 22 23 24 25 26 27

  static defaultProps = {
    defaultActiveFirstOption: false,
    onPressEnter: () => {},
    onSearch: () => {},
    className: '',
    placeholder: '',
    dataSource: [],
    defaultOpen: false,
  };

陈帅's avatar
陈帅 committed
28 29 30 31 32 33 34 35
  constructor(props) {
    super(props);
    this.state = {
      searchMode: props.defaultOpen,
      value: '',
    };
  }

36 37 38
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
陈帅's avatar
陈帅 committed
39

jim's avatar
jim committed
40
  onKeyDown = e => {
41
    if (e.key === 'Enter') {
陈帅's avatar
陈帅 committed
42 43
      const { onPressEnter } = this.props;
      const { value } = this.state;
44
      this.timeout = setTimeout(() => {
陈帅's avatar
陈帅 committed
45
        onPressEnter(value); // Fix duplicate onPressEnter
46 47
      }, 0);
    }
jim's avatar
jim committed
48
  };
陈帅's avatar
陈帅 committed
49

jim's avatar
jim committed
50
  onChange = value => {
陈帅's avatar
陈帅 committed
51
    const { onChange } = this.props;
52
    this.setState({ value });
陈帅's avatar
陈帅 committed
53 54
    if (onChange) {
      onChange();
55
    }
jim's avatar
jim committed
56
  };
陈帅's avatar
陈帅 committed
57

58 59
  enterSearchMode = () => {
    this.setState({ searchMode: true }, () => {
陈帅's avatar
陈帅 committed
60 61
      const { searchMode } = this.state;
      if (searchMode) {
afc163's avatar
afc163 committed
62
        this.input.focus();
63 64
      }
    });
jim's avatar
jim committed
65
  };
陈帅's avatar
陈帅 committed
66

67 68 69 70 71
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
72
  };
陈帅's avatar
陈帅 committed
73

74 75
  render() {
    const { className, placeholder, ...restProps } = this.props;
76
    delete restProps.defaultOpen; // for rc-select not affected
陈帅's avatar
陈帅 committed
77
    const { searchMode, value } = this.state;
78
    const inputClass = classNames(styles.input, {
陈帅's avatar
陈帅 committed
79
      [styles.show]: searchMode,
80 81
    });
    return (
jim's avatar
jim committed
82
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
83
        <Icon type="search" key="Icon" />
84
        <AutoComplete
陈帅's avatar
陈帅 committed
85
          key="AutoComplete"
86
          {...restProps}
87
          className={inputClass}
陈帅's avatar
陈帅 committed
88
          value={value}
89 90 91
          onChange={this.onChange}
        >
          <Input
jim's avatar
jim committed
92 93 94
            ref={node => {
              this.input = node;
            }}
jim's avatar
jim committed
95
            aria-label={placeholder}
jim's avatar
jim committed
96
            placeholder={placeholder}
97 98 99 100 101 102 103 104
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}