index.tsx 3.86 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import React, { Component } from 'react';
2
import { Input, Icon, AutoComplete } from 'antd';
何乐's avatar
何乐 committed
3
import { DataSourceItemType } from 'antd/es/auto-complete';
4
import classNames from 'classnames';
5 6
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
7 8
import styles from './index.less';

何乐's avatar
何乐 committed
9
export interface HeaderSearchProps {
陈小聪's avatar
陈小聪 committed
10 11 12 13 14 15 16
  onPressEnter: (value: string) => void;
  onSearch: (value: string) => void;
  onChange: (value: string) => void;
  onVisibleChange: (b: boolean) => void;
  className: string;
  placeholder: string;
  defaultActiveFirstOption: boolean;
何乐's avatar
何乐 committed
17
  dataSource: DataSourceItemType[];
陈小聪's avatar
陈小聪 committed
18 19 20
  defaultOpen: boolean;
  open?: boolean;
}
jim's avatar
jim committed
21

陈小聪's avatar
陈小聪 committed
22 23 24 25
interface HeaderSearchState {
  value: string;
  searchMode: boolean;
}
何乐's avatar
何乐 committed
26

陈小聪's avatar
陈小聪 committed
27
export default class HeaderSearch extends Component<HeaderSearchProps, HeaderSearchState> {
jim's avatar
jim committed
28 29 30 31
  static defaultProps = {
    defaultActiveFirstOption: false,
    onPressEnter: () => {},
    onSearch: () => {},
32
    onChange: () => {},
jim's avatar
jim committed
33 34 35 36
    className: '',
    placeholder: '',
    dataSource: [],
    defaultOpen: false,
37
    onVisibleChange: () => {},
jim's avatar
jim committed
38 39
  };

何乐's avatar
何乐 committed
40
  static getDerivedStateFromProps(props: HeaderSearchProps) {
41 42 43 44 45 46 47 48
    if ('open' in props) {
      return {
        searchMode: props.open,
      };
    }
    return null;
  }

何乐's avatar
何乐 committed
49 50 51 52
  private timeout: NodeJS.Timeout = null!;
  private inputRef: Input | null = null;

  constructor(props: HeaderSearchProps) {
陈帅's avatar
陈帅 committed
53 54 55 56 57 58 59
    super(props);
    this.state = {
      searchMode: props.defaultOpen,
      value: '',
    };
  }

60 61 62
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
陈帅's avatar
陈帅 committed
63

何乐's avatar
何乐 committed
64
  onKeyDown = (e: React.KeyboardEvent) => {
65
    if (e.key === 'Enter') {
陈帅's avatar
陈帅 committed
66 67
      const { onPressEnter } = this.props;
      const { value } = this.state;
68
      this.timeout = setTimeout(() => {
陈帅's avatar
陈帅 committed
69
        onPressEnter(value); // Fix duplicate onPressEnter
70 71
      }, 0);
    }
jim's avatar
jim committed
72
  };
陈帅's avatar
陈帅 committed
73

何乐's avatar
何乐 committed
74
  onChange = (value: string) => {
75
    const { onSearch, onChange } = this.props;
76
    this.setState({ value });
陈帅's avatar
陈帅 committed
77 78
    if (onSearch) {
      onSearch(value);
79
    }
陈帅's avatar
陈帅 committed
80 81
    if (onChange) {
      onChange(value);
82
    }
jim's avatar
jim committed
83
  };
陈帅's avatar
陈帅 committed
84

85
  enterSearchMode = () => {
陈帅's avatar
陈帅 committed
86 87
    const { onVisibleChange } = this.props;
    onVisibleChange(true);
88
    this.setState({ searchMode: true }, () => {
陈帅's avatar
陈帅 committed
89
      const { searchMode } = this.state;
何乐's avatar
何乐 committed
90 91
      if (searchMode && this.inputRef) {
        this.inputRef.focus();
92 93
      }
    });
jim's avatar
jim committed
94
  };
陈帅's avatar
陈帅 committed
95

96 97 98 99 100
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
101
  };
陈帅's avatar
陈帅 committed
102

陈帅's avatar
陈帅 committed
103 104 105 106 107 108 109 110
  // 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
111
    const { value } = this.state;
陈帅's avatar
陈帅 committed
112 113 114
    onPressEnter(value);
  }

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