index.tsx 3.74 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';
陈帅's avatar
陈帅 committed
5
import debounce from 'lodash/debounce';
6 7
import styles from './index.less';

何乐's avatar
何乐 committed
8
export interface HeaderSearchProps {
陈小聪's avatar
陈小聪 committed
9 10 11 12 13 14 15
  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
16
  dataSource: DataSourceItemType[];
陈小聪's avatar
陈小聪 committed
17 18 19
  defaultOpen: boolean;
  open?: boolean;
}
jim's avatar
jim committed
20

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

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

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

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

  constructor(props: HeaderSearchProps) {
陈帅's avatar
陈帅 committed
52 53 54 55 56
    super(props);
    this.state = {
      searchMode: props.defaultOpen,
      value: '',
    };
陈帅's avatar
陈帅 committed
57 58 59 60
    this.debouncePressEnter = debounce(this.debouncePressEnter, 500, {
      leading: true,
      trailing: false,
    });
陈帅's avatar
陈帅 committed
61 62
  }

63 64 65
  componentWillUnmount() {
    clearTimeout(this.timeout);
  }
陈帅's avatar
陈帅 committed
66

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

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

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

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

陈帅's avatar
陈帅 committed
106
  debouncePressEnter = () => {
陈帅's avatar
陈帅 committed
107
    const { onPressEnter } = this.props;
Sean Bao's avatar
Sean Bao committed
108
    const { value } = this.state;
陈帅's avatar
陈帅 committed
109
    onPressEnter(value);
陈帅's avatar
陈帅 committed
110
  };
陈帅's avatar
陈帅 committed
111

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