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

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

jim's avatar
jim committed
38
  onKeyDown = e => {
39
    if (e.key === 'Enter') {
40
      this.debouncePressEnter();
41
    }
jim's avatar
jim committed
42
  };
陈帅's avatar
陈帅 committed
43

jim's avatar
jim committed
44
  onChange = value => {
45
    this.setState({ value });
46 47 48
    const { onChange } = this.props;
    if (onChange) {
      onChange();
49
    }
jim's avatar
jim committed
50
  };
陈帅's avatar
陈帅 committed
51

52 53
  enterSearchMode = () => {
    this.setState({ searchMode: true }, () => {
54 55
      const { searchMode } = this.state;
      if (searchMode) {
afc163's avatar
afc163 committed
56
        this.input.focus();
57 58
      }
    });
jim's avatar
jim committed
59
  };
陈帅's avatar
陈帅 committed
60

61 62 63 64 65
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
66
  };
陈帅's avatar
陈帅 committed
67

68 69 70 71 72 73 74 75 76 77 78 79
  debouncePressEnter() {
    const { onPressEnter } = this.props;
    const { value } = this.state;
    onPressEnter(value);
  }

  // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次
  @Bind()
  @Debounce(500, {
    leading: true,
    trailing: false,
  })
80 81
  render() {
    const { className, placeholder, ...restProps } = this.props;
82
    const { searchMode, value } = this.state;
83
    delete restProps.defaultOpen; // for rc-select not affected
84
    const inputClass = classNames(styles.input, {
85
      [styles.show]: searchMode,
86 87
    });
    return (
jim's avatar
jim committed
88
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
89
        <Icon type="search" key="Icon" />
90
        <AutoComplete
陈帅's avatar
陈帅 committed
91
          key="AutoComplete"
92
          {...restProps}
93
          className={inputClass}
94
          value={value}
95 96 97 98
          onChange={this.onChange}
        >
          <Input
            placeholder={placeholder}
jim's avatar
jim committed
99 100 101
            ref={node => {
              this.input = node;
            }}
102 103 104 105 106 107 108 109
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}