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

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

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

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

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

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

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

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

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

陈帅's avatar
陈帅 committed
76 77 78 79 80 81 82 83 84 85 86 87
  // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次
  @Bind()
  @Debounce(500, {
    leading: true,
    trailing: false,
  })
  debouncePressEnter() {
    const { onPressEnter } = this.props;
    const value = this.state;
    onPressEnter(value);
  }

88 89
  render() {
    const { className, placeholder, ...restProps } = this.props;
陈帅's avatar
陈帅 committed
90
    const { searchMode, value } = this.state;
91
    delete restProps.defaultOpen; // for rc-select not affected
92
    const inputClass = classNames(styles.input, {
陈帅's avatar
陈帅 committed
93
      [styles.show]: searchMode,
94 95
    });
    return (
jim's avatar
jim committed
96
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
97
        <Icon type="search" key="Icon" />
98
        <AutoComplete
陈帅's avatar
陈帅 committed
99
          key="AutoComplete"
100
          {...restProps}
101
          className={inputClass}
陈帅's avatar
陈帅 committed
102
          value={value}
103 104 105
          onChange={this.onChange}
        >
          <Input
jim's avatar
jim committed
106 107 108
            ref={node => {
              this.input = node;
            }}
jim's avatar
jim committed
109
            aria-label={placeholder}
jim's avatar
jim committed
110
            placeholder={placeholder}
111 112 113 114 115 116 117 118
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}