index.tsx
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { AutoComplete, Icon, Input } from 'antd';
import { AutoCompleteProps, DataSourceItemType } from 'antd/es/auto-complete';
import React, { Component } from 'react';
import classNames from 'classnames';
import debounce from 'lodash/debounce';
import styles from './index.less';
export interface HeaderSearchProps {
onPressEnter: (value: string) => void;
onSearch: (value: string) => void;
onChange: (value: string) => void;
onVisibleChange: (b: boolean) => void;
className: string;
placeholder: string;
defaultActiveFirstOption: boolean;
dataSource: DataSourceItemType[];
defaultOpen: boolean;
open?: boolean;
defaultValue?: string;
}
interface HeaderSearchState {
value?: string;
searchMode: boolean;
}
export default class HeaderSearch extends Component<HeaderSearchProps, HeaderSearchState> {
static defaultProps = {
defaultActiveFirstOption: false,
onPressEnter: () => {},
onSearch: () => {},
onChange: () => {},
className: '',
placeholder: '',
dataSource: [],
defaultOpen: false,
onVisibleChange: () => {},
};
static getDerivedStateFromProps(props: HeaderSearchProps) {
if ('open' in props) {
return {
searchMode: props.open,
};
}
return null;
}
private inputRef: Input | null = null;
constructor(props: HeaderSearchProps) {
super(props);
this.state = {
searchMode: props.defaultOpen,
value: props.defaultValue,
};
this.debouncePressEnter = debounce(this.debouncePressEnter, 500, {
leading: true,
trailing: false,
});
}
onKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
this.debouncePressEnter();
}
};
onChange: AutoCompleteProps['onChange'] = value => {
if (typeof value === 'string') {
const { onSearch, onChange } = this.props;
this.setState({ value });
if (onSearch) {
onSearch(value);
}
if (onChange) {
onChange(value);
}
}
};
enterSearchMode = () => {
const { onVisibleChange } = this.props;
onVisibleChange(true);
this.setState({ searchMode: true }, () => {
const { searchMode } = this.state;
if (searchMode && this.inputRef) {
this.inputRef.focus();
}
});
};
leaveSearchMode = () => {
this.setState({
searchMode: false,
});
};
debouncePressEnter = () => {
const { onPressEnter } = this.props;
const { value } = this.state;
onPressEnter(value || '');
};
render() {
const { className, defaultValue, placeholder, open, ...restProps } = this.props;
const { searchMode, value } = this.state;
delete restProps.defaultOpen; // for rc-select not affected
const inputClass = classNames(styles.input, {
[styles.show]: searchMode,
});
return (
<span
className={classNames(className, styles.headerSearch)}
onClick={this.enterSearchMode}
onTransitionEnd={({ propertyName }) => {
if (propertyName === 'width' && !searchMode) {
const { onVisibleChange } = this.props;
onVisibleChange(searchMode);
}
}}
>
<Icon type="search" key="Icon" />
<AutoComplete
key="AutoComplete"
{...restProps}
className={inputClass}
value={value}
onChange={this.onChange}
>
<Input
ref={node => {
this.inputRef = node;
}}
defaultValue={defaultValue}
aria-label={placeholder}
placeholder={placeholder}
onKeyDown={this.onKeyDown}
onBlur={this.leaveSearchMode}
/>
</AutoComplete>
</span>
);
}
}