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: '',
  };
jim's avatar
jim committed
34
  onKeyDown = e => {
35
    if (e.key === 'Enter') {
36
      this.debouncePressEnter();
37
    }
jim's avatar
jim committed
38 39
  };
  onChange = value => {
40
    this.setState({ value });
41 42 43
    if (this.props.onChange) {
      this.props.onChange();
    }
jim's avatar
jim committed
44
  };
45 46 47 48 49 50 51 52 53
  // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次
  @Bind()
  @Debounce(500, {
    leading: true,
    trailing: false,
  })
  debouncePressEnter() {
    this.props.onPressEnter(this.state.value);
  }
54 55 56
  enterSearchMode = () => {
    this.setState({ searchMode: true }, () => {
      if (this.state.searchMode) {
afc163's avatar
afc163 committed
57
        this.input.focus();
58 59
      }
    });
jim's avatar
jim committed
60
  };
61 62 63 64 65
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
66
  };
67 68
  render() {
    const { className, placeholder, ...restProps } = this.props;
69
    delete restProps.defaultOpen; // for rc-select not affected
70 71 72 73
    const inputClass = classNames(styles.input, {
      [styles.show]: this.state.searchMode,
    });
    return (
jim's avatar
jim committed
74
      <span className={classNames(className, styles.headerSearch)} onClick={this.enterSearchMode}>
陈帅's avatar
陈帅 committed
75
        <Icon type="search" key="Icon" />
76
        <AutoComplete
陈帅's avatar
陈帅 committed
77
          key="AutoComplete"
78
          {...restProps}
79 80 81 82 83 84
          className={inputClass}
          value={this.state.value}
          onChange={this.onChange}
        >
          <Input
            placeholder={placeholder}
jim's avatar
jim committed
85 86 87
            ref={node => {
              this.input = node;
            }}
88 89 90 91 92 93 94 95
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}