index.js 3.54 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
  static propTypes = {
    className: PropTypes.string,
    placeholder: PropTypes.string,
    onSearch: PropTypes.func,
14
    onChange: PropTypes.func,
afc163's avatar
afc163 committed
15 16 17
    onPressEnter: PropTypes.func,
    defaultActiveFirstOption: PropTypes.bool,
    dataSource: PropTypes.array,
18
    defaultOpen: PropTypes.bool,
19
    onVisibleChange: PropTypes.func,
20
  };
jim's avatar
jim committed
21 22 23 24 25

  static defaultProps = {
    defaultActiveFirstOption: false,
    onPressEnter: () => {},
    onSearch: () => {},
26
    onChange: () => {},
jim's avatar
jim committed
27 28 29 30
    className: '',
    placeholder: '',
    dataSource: [],
    defaultOpen: false,
31
    onVisibleChange: () => {},
jim's avatar
jim committed
32 33
  };

34 35 36 37 38 39 40 41 42
  static getDerivedStateFromProps(props) {
    if ('open' in props) {
      return {
        searchMode: props.open,
      };
    }
    return null;
  }

陈帅's avatar
陈帅 committed
43 44 45 46 47 48 49 50
  constructor(props) {
    super(props);
    this.state = {
      searchMode: props.defaultOpen,
      value: '',
    };
  }

51 52 53
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
陈帅's avatar
陈帅 committed
54

jim's avatar
jim committed
55
  onKeyDown = e => {
56
    if (e.key === 'Enter') {
陈帅's avatar
陈帅 committed
57 58
      const { onPressEnter } = this.props;
      const { value } = this.state;
59
      this.timeout = setTimeout(() => {
陈帅's avatar
陈帅 committed
60
        onPressEnter(value); // Fix duplicate onPressEnter
61 62
      }, 0);
    }
jim's avatar
jim committed
63
  };
陈帅's avatar
陈帅 committed
64

jim's avatar
jim committed
65
  onChange = value => {
66
    const { onSearch, onChange } = this.props;
67
    this.setState({ value });
68 69 70 71 72
    if (onSearch){
        onSearch(value);
    }
    if (onChange){
        onChange(value);
73
    }
jim's avatar
jim committed
74
  };
陈帅's avatar
陈帅 committed
75

76
  enterSearchMode = () => {
陈帅's avatar
陈帅 committed
77 78
    const { onVisibleChange } = this.props;
    onVisibleChange(true);
79
    this.setState({ searchMode: true }, () => {
陈帅's avatar
陈帅 committed
80 81
      const { searchMode } = this.state;
      if (searchMode) {
afc163's avatar
afc163 committed
82
        this.input.focus();
83 84
      }
    });
jim's avatar
jim committed
85
  };
陈帅's avatar
陈帅 committed
86

87 88 89 90 91
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
92
  };
陈帅's avatar
陈帅 committed
93

陈帅's avatar
陈帅 committed
94 95 96 97 98 99 100 101
  // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次
  @Bind()
  @Debounce(500, {
    leading: true,
    trailing: false,
  })
  debouncePressEnter() {
    const { onPressEnter } = this.props;
Sean Bao's avatar
Sean Bao committed
102
    const { value } = this.state;
陈帅's avatar
陈帅 committed
103 104 105
    onPressEnter(value);
  }

106
  render() {
107
    const { className, placeholder, open, ...restProps } = this.props;
陈帅's avatar
陈帅 committed
108
    const { searchMode, value } = this.state;
109
    delete restProps.defaultOpen; // for rc-select not affected
110
    const inputClass = classNames(styles.input, {
陈帅's avatar
陈帅 committed
111
      [styles.show]: searchMode,
112 113
    });
    return (
114 115 116 117
      <span
        className={classNames(className, styles.headerSearch)}
        onClick={this.enterSearchMode}
        onTransitionEnd={({ propertyName }) => {
陈帅's avatar
陈帅 committed
118 119 120
          if (propertyName === 'width' && !searchMode) {
            const { onVisibleChange } = this.props;
            onVisibleChange(searchMode);
121 122 123
          }
        }}
      >
陈帅's avatar
陈帅 committed
124
        <Icon type="search" key="Icon" />
125
        <AutoComplete
陈帅's avatar
陈帅 committed
126
          key="AutoComplete"
127
          {...restProps}
128
          className={inputClass}
陈帅's avatar
陈帅 committed
129
          value={value}
130 131 132
          onChange={this.onChange}
        >
          <Input
jim's avatar
jim committed
133 134 135
            ref={node => {
              this.input = node;
            }}
jim's avatar
jim committed
136
            aria-label={placeholder}
jim's avatar
jim committed
137
            placeholder={placeholder}
138 139 140 141 142 143 144 145
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}