index.js 2.47 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
  state = {
31
    searchMode: this.props.defaultOpen,
32 33
    value: '',
  };
陈帅's avatar
陈帅 committed
34

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

jim's avatar
jim committed
41
  onChange = value => {
42
    this.setState({ value });
43 44 45
    if (this.props.onChange) {
      this.props.onChange();
    }
jim's avatar
jim committed
46
  };
陈帅's avatar
陈帅 committed
47

48 49 50 51 52 53 54 55 56
  // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次
  @Bind()
  @Debounce(500, {
    leading: true,
    trailing: false,
  })
  debouncePressEnter() {
    this.props.onPressEnter(this.state.value);
  }
陈帅's avatar
陈帅 committed
57

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

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

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