index.js 3.43 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
    onVisibleChange: PropTypes.func,
19
  };
jim's avatar
jim committed
20 21 22 23 24 25 26 27 28

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

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

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

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

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

jim's avatar
jim committed
63
  onChange = value => {
陈帅's avatar
陈帅 committed
64
    const { onChange } = this.props;
65
    this.setState({ value });
陈帅's avatar
陈帅 committed
66
    if (onChange) {
Sean Bao's avatar
Sean Bao committed
67
      onChange(value);
68
    }
jim's avatar
jim committed
69
  };
陈帅's avatar
陈帅 committed
70

71
  enterSearchMode = () => {
陈帅's avatar
陈帅 committed
72 73
    const { onVisibleChange } = this.props;
    onVisibleChange(true);
74
    this.setState({ searchMode: true }, () => {
陈帅's avatar
陈帅 committed
75 76
      const { searchMode } = this.state;
      if (searchMode) {
afc163's avatar
afc163 committed
77
        this.input.focus();
78 79
      }
    });
jim's avatar
jim committed
80
  };
陈帅's avatar
陈帅 committed
81

82 83 84 85 86
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
87
  };
陈帅's avatar
陈帅 committed
88

陈帅's avatar
陈帅 committed
89 90 91 92 93 94 95 96
  // 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
97
    const { value } = this.state;
陈帅's avatar
陈帅 committed
98 99 100
    onPressEnter(value);
  }

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