index.js 2.3 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,
  };

28
  state = {
29
    searchMode: this.props.defaultOpen,
30 31 32 33 34
    value: '',
  };
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
jim's avatar
jim committed
35
  onKeyDown = e => {
36 37
    if (e.key === 'Enter') {
      this.timeout = setTimeout(() => {
afc163's avatar
afc163 committed
38
        this.props.onPressEnter(this.state.value); // Fix duplicate onPressEnter
39 40
      }, 0);
    }
jim's avatar
jim committed
41 42
  };
  onChange = value => {
43
    this.setState({ value });
44 45 46
    if (this.props.onChange) {
      this.props.onChange();
    }
jim's avatar
jim committed
47
  };
48 49 50
  enterSearchMode = () => {
    this.setState({ searchMode: true }, () => {
      if (this.state.searchMode) {
afc163's avatar
afc163 committed
51
        this.input.focus();
52 53
      }
    });
jim's avatar
jim committed
54
  };
55 56 57 58 59
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
60
  };
61 62
  render() {
    const { className, placeholder, ...restProps } = this.props;
63
    delete restProps.defaultOpen; // for rc-select not affected
64 65 66 67
    const inputClass = classNames(styles.input, {
      [styles.show]: this.state.searchMode,
    });
    return (
jim's avatar
jim committed
68
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
69
        <Icon type="search" key="Icon" />
70
        <AutoComplete
陈帅's avatar
陈帅 committed
71
          key="AutoComplete"
72
          {...restProps}
73 74 75 76 77
          className={inputClass}
          value={this.state.value}
          onChange={this.onChange}
        >
          <Input
jim's avatar
jim committed
78 79 80
            ref={node => {
              this.input = node;
            }}
jim's avatar
jim committed
81
            aria-label={placeholder}
jim's avatar
jim committed
82
            placeholder={placeholder}
83 84 85 86 87 88 89 90
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}