-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathAutoComplete.jsx
More file actions
229 lines (192 loc) · 5.74 KB
/
AutoComplete.jsx
File metadata and controls
229 lines (192 loc) · 5.74 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* @flow */
import * as React from 'react';
import ReactDOM from 'react-dom';
import ClickOutside from 'react-click-outside';
import { Component, PropTypes } from '../../libs';
import Input from '../input';
import Suggestions from './Suggestions';
type State = {
inputValue: string,
isFocus: boolean,
suggestions: Array<any>,
loading: boolean,
highlightedIndex: number,
}
type Props = {
popperClass: string,
placeholder: string,
disabled: boolean,
name: string,
size: string,
value: string,
triggerOnFocus: boolean,
fetchSuggestions: Function,
onSelect: Function,
onChange: Function,
onIconClick: Function,
icon: Element | string,
append: Element,
prepend: Element,
onFocus: Function,
onBlur: Function
}
type AutoCompleteDefaultProps = {
triggerOnFocus: boolean,
};
class AutoComplete extends Component {
props: Props;
state: State;
static defaultProps: AutoCompleteDefaultProps = {
triggerOnFocus: true,
};
constructor(props: Props) {
super(props);
this.state = {
inputValue: props.value,
isFocus: false,
suggestions: [],
loading: false,
highlightedIndex: -1
};
}
getChildContext() {
return {
component: this
};
}
componentWillReceiveProps(props: Props): void {
this.setState({ inputValue: props.value });
}
componentDidUpdate(): void {
const visible = this.suggestionVisible();
const reference = ReactDOM.findDOMNode(this.inputNode);
if (reference instanceof HTMLElement) {
setTimeout(() => {
this.suggestionsNode.onVisibleChange(visible, reference.offsetWidth);
})
}
}
getData(queryString: string): void {
this.setState({ loading: true });
this.props.fetchSuggestions(queryString, (suggestions: Array<any>) => {
this.setState({ loading: false });
if (Array.isArray(suggestions)) {
this.setState({ suggestions });
}
});
}
handleChange(value: string): void {
this.setState({ inputValue: value });
if (!this.props.triggerOnFocus && !value) {
this.setState({ suggestions: [] }); return;
}
if (this.props.onChange) {
this.props.onChange(value);
}
this.getData(value);
}
handleFocus(e): void {
this.setState({ isFocus: true });
if (this.props.onFocus) this.props.onFocus(e);
if (this.props.triggerOnFocus) {
this.getData(this.state.inputValue);
}
}
handleKeyEnter(highlightedIndex: number): void {
if (this.suggestionVisible() && highlightedIndex >= 0 && highlightedIndex < this.state.suggestions.length) {
this.select(this.state.suggestions[highlightedIndex]);
}
}
handleClickOutside(): void {
if (this.state.isFocus) {
this.setState({ isFocus: false });
}
}
select(item: Object): void {
this.setState({ inputValue: item.value }, () => {
this.setState({ suggestions: [] });
});
if (this.props.onSelect) {
this.props.onSelect(item);
}
}
highlight(index: number): void {
if (!this.suggestionVisible() || this.state.loading) return;
if (index < 0) index = 0;
if (index >= this.state.suggestions.length) {
index = this.state.suggestions.length - 1;
}
const reference = ReactDOM.findDOMNode(this.suggestionsNode);
if (reference instanceof HTMLElement) {
const suggestion = document.querySelector('.el-autocomplete-suggestion__wrap');
const suggestionList = document.querySelectorAll('.el-autocomplete-suggestion__list li');
if (suggestion instanceof HTMLElement && suggestionList instanceof NodeList) {
let highlightItem = suggestionList[index];
let scrollTop = suggestion.scrollTop;
let offsetTop = highlightItem.offsetTop;
if (offsetTop + highlightItem.scrollHeight > (scrollTop + suggestion.clientHeight)) {
suggestion.scrollTop += highlightItem.scrollHeight;
}
if (offsetTop < scrollTop) {
suggestion.scrollTop -= highlightItem.scrollHeight;
}
this.setState({ highlightedIndex: index });
}
}
}
/* Computed Methods */
suggestionVisible(): boolean {
const suggestions = this.state.suggestions;
const isValidData = Array.isArray(suggestions) && suggestions.length > 0;
return (isValidData || this.state.loading) && this.state.isFocus;
}
onKeyDown(e: SyntheticKeyboardEvent<HTMLInputElement>): void {
const { highlightedIndex } = this.state;
switch (e.keyCode) {
case 13:
this.handleKeyEnter(highlightedIndex);
break;
case 38:
this.highlight(highlightedIndex - 1)
break;
case 40:
this.highlight(highlightedIndex + 1);
break;
default:
break;
}
}
render(): React.Element<any> {
const { disabled, placeholder, name, size, icon, append, prepend, onIconClick, popperClass } = this.props;
const { inputValue, suggestions } = this.state;
return (
<div style={this.style()} className={this.className('el-autocomplete')}>
<Input
ref={node => this.inputNode = node}
value={inputValue}
disabled={disabled}
placeholder={placeholder}
name={name}
size={size}
icon={icon}
prepend={prepend}
append={append}
onIconClick={onIconClick}
onChange={this.handleChange.bind(this)}
onFocus={this.handleFocus.bind(this)}
onBlur={this.props.onBlur}
onKeyDown={this.onKeyDown.bind(this)}
/>
<Suggestions
ref={node => this.suggestionsNode = node}
className={this.classNames(popperClass)}
suggestions={suggestions}
/>
</div>
)
}
}
AutoComplete.childContextTypes = {
component: PropTypes.any
};
export default ClickOutside(AutoComplete);