index.tsx 3.85 KB
Newer Older
1
import { AutoComplete, Icon, Input } from 'antd';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
2
import { AutoCompleteProps, DataSourceItemType } from 'antd/es/auto-complete';
3
import React, { Component } from 'react';
4

5
import classNames from 'classnames';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
6
import debounce from 'lodash/debounce';
7 8
import styles from './index.less';

ไฝ•ไน's avatar
ไฝ•ไน committed
9
export interface HeaderSearchProps {
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[];
18 19 20
  defaultOpen: boolean;
  open?: boolean;
}
jim's avatar
jim committed
21

22 23 24 25
interface HeaderSearchState {
  value: string;
  searchMode: boolean;
}
ไฝ•ไน's avatar
ไฝ•ไน committed
26

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;
  }

49 50
  private timeout: number | undefined = undefined;

ไฝ•ไน's avatar
ไฝ•ไน committed
51 52 53
  private inputRef: Input | null = null;

  constructor(props: HeaderSearchProps) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
54 55 56 57 58
    super(props);
    this.state = {
      searchMode: props.defaultOpen,
      value: '',
    };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
59 60 61 62
    this.debouncePressEnter = debounce(this.debouncePressEnter, 500, {
      leading: true,
      trailing: false,
    });
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
63 64
  }

65 66 67
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
68

ไฝ•ไน's avatar
ไฝ•ไน committed
69
  onKeyDown = (e: React.KeyboardEvent) => {
70
    if (e.key === 'Enter') {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
71 72
      const { onPressEnter } = this.props;
      const { value } = this.state;
73
      this.timeout = window.setTimeout(() => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74
        onPressEnter(value); // Fix duplicate onPressEnter
75 76
      }, 0);
    }
jim's avatar
jim committed
77
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
78

79 80 81 82 83 84 85 86 87 88
  onChange: AutoCompleteProps['onChange'] = value => {
    if (typeof value === 'string') {
      const { onSearch, onChange } = this.props;
      this.setState({ value });
      if (onSearch) {
        onSearch(value);
      }
      if (onChange) {
        onChange(value);
      }
89
    }
jim's avatar
jim committed
90
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
91

92
  enterSearchMode = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
93 94
    const { onVisibleChange } = this.props;
    onVisibleChange(true);
95
    this.setState({ searchMode: true }, () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
96
      const { searchMode } = this.state;
ไฝ•ไน's avatar
ไฝ•ไน committed
97 98
      if (searchMode && this.inputRef) {
        this.inputRef.focus();
99 100
      }
    });
jim's avatar
jim committed
101
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
102

103 104 105 106 107
  leaveSearchMode = () => {
    this.setState({
      searchMode: false,
      value: '',
    });
jim's avatar
jim committed
108
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
109

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
110
  debouncePressEnter = () => {
111
    const { onPressEnter } = this.props;
Sean Bao's avatar
Sean Bao committed
112
    const { value } = this.state;
113
    onPressEnter(value);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
114
  };
115

116
  render() {
117
    const { className, placeholder, open, ...restProps } = this.props;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
118
    const { searchMode, value } = this.state;
119
    delete restProps.defaultOpen; // for rc-select not affected
120
    const inputClass = classNames(styles.input, {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
121
      [styles.show]: searchMode,
122
    });
123

124
    return (
125 126 127 128
      <span
        className={classNames(className, styles.headerSearch)}
        onClick={this.enterSearchMode}
        onTransitionEnd={({ propertyName }) => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
129 130 131
          if (propertyName === 'width' && !searchMode) {
            const { onVisibleChange } = this.props;
            onVisibleChange(searchMode);
132 133 134
          }
        }}
      >
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
135
        <Icon type="search" key="Icon" />
136
        <AutoComplete
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
137
          key="AutoComplete"
138
          {...restProps}
139
          className={inputClass}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
140
          value={value}
141
          onChange={this.onChange}
142 143
        >
          <Input
jim's avatar
jim committed
144
            ref={node => {
ไฝ•ไน's avatar
ไฝ•ไน committed
145
              this.inputRef = node;
jim's avatar
jim committed
146
            }}
jim's avatar
jim committed
147
            aria-label={placeholder}
jim's avatar
jim committed
148
            placeholder={placeholder}
149 150 151 152 153 154 155 156
            onKeyDown={this.onKeyDown}
            onBlur={this.leaveSearchMode}
          />
        </AutoComplete>
      </span>
    );
  }
}