-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchSelectBox.js
213 lines (190 loc) · 5.93 KB
/
SearchSelectBox.js
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
import React, { PureComponent } from 'react';
import cx from 'classnames';
import _last from 'lodash/last';
import SearchInput from './components/SearchInput';
import SearchOptionsMenu from './components/SearchOptionsMenu';
import getSuggestions from './mockServerApi';
import {
getLastWord,
getNewSearchQuery
} from "./searchSelectBoxHelper";
const EMPTY_ARRAY = [],
KEY_CODES = {
ENTER: 13,
ARROW_UP: 38,
ARROW_DOWN: 40,
BACKSPACE: 8,
},
INIT_SEARCH_RESULTS = {
focusedOptionIndex: 0,
isOpen: false, // to check whether dropdown in open
searchResults: EMPTY_ARRAY,
},
INITIAL_STATE = {
...INIT_SEARCH_RESULTS,
errorMessage: undefined,
hasSavedOption: false, // to check for any previous selected suggestions
isSearchInputFocused: false,
loading: false,
searchQuery: '',
selectedOptions: EMPTY_ARRAY, // storing any suggestions selected from the dropdown
};
class SearchSelectBox extends PureComponent {
state = INITIAL_STATE;
/**
* when user types in the search box
*/
handleSearch = searchQuery => {
this.setState({
searchQuery,
searchResults: EMPTY_ARRAY, // if any previous results exists
isOpen: !!searchQuery, // when we clear the input should close the dropdown
focusedOptionIndex: 0,
loading: true,
}, this.fetchSearchSuggestions);
};
/**
* on input blur we need to reset any suggestions we fetched and also any selectedOptions from dropdown
*/
handBlur = () => {
this.setState({
...INIT_SEARCH_RESULTS,
isSearchInputFocused: false,
selectedOptions: EMPTY_ARRAY,
});
}
/*
Clearing text when clicked on cross button
*/
handleClearText = () => {
this.setState({
...INITIAL_STATE,
isSearchInputFocused: true, // need to keep input box focused
});
}
handleInputFocus = () => {
this.setState({
isSearchInputFocused: true,
});
}
handleRequestError = () => {
this.setState({
loading: false,
errorMessage: 'Sorry, error while fetching suggestions'
});
}
/*
currently typing search query
*/
getCurrentSearchQuery = () => {
const { state: { selectedOptions, searchQuery } } = this;
return selectedOptions.length ? getLastWord(searchQuery) : searchQuery.trim();
}
/**
* fetching suggestions from mockServerApi call
*/
fetchSearchSuggestions = () => {
const targetQuery = this.getCurrentSearchQuery();
if (targetQuery) {
getSuggestions(targetQuery).then(response => {
this.setState({
errorMessage: undefined,
searchResults: response,
loading: false,
})
}, this.handleRequestError);
}
}
/*
Updating focused option index
*/
handleFocusSearchOption = newFocusedOptionIndex => {
this.setState({
focusedOptionIndex: +newFocusedOptionIndex,
});
};
/*
when we select any menu option, we set that suggestion in the input box and reset searchResults
*/
handleSelectSearchOption = () => {
const { searchResults, focusedOptionIndex, selectedOptions } = this.state;
const newQuery = searchResults[focusedOptionIndex];
this.setState({
searchQuery: getNewSearchQuery(newQuery, selectedOptions), // if any previous selected options exists
selectedOptions: selectedOptions.concat(newQuery),
...INIT_SEARCH_RESULTS,
});
}
handleKeyDown = event => {
const { focusedOptionIndex, searchResults, searchQuery, selectedOptions } = this.state,
searchResultsLength = searchResults.length,
lastSearchMenuOptionIndex = searchResultsLength - 1;
if (!searchResultsLength) return;
switch (event.keyCode) {
case KEY_CODES.ENTER: {
if (this.state.isOpen) {
this.handleSelectSearchOption();
}
break;
}
case KEY_CODES.ARROW_UP: {
event.preventDefault(); // to maintain cursor position in input box
const updatedFocusedOptionIndex = focusedOptionIndex === 0 ? lastSearchMenuOptionIndex : focusedOptionIndex - 1; // to maintain cyclic navigation
this.handleFocusSearchOption(updatedFocusedOptionIndex);
break;
}
case KEY_CODES.ARROW_DOWN: {
const updatedFocusedOptionIndex = focusedOptionIndex === lastSearchMenuOptionIndex ? 0 : focusedOptionIndex + 1; // to maintain cyclic navigation
this.handleFocusSearchOption(updatedFocusedOptionIndex);
break;
}
case KEY_CODES.BACKSPACE: {
if (event.target.tagName === 'INPUT' && selectedOptions.length) { // when we start pressing back space we check with last previously selectedOptions
const lastWord = getLastWord(searchQuery);
const lastSelectedOption = _last(selectedOptions);
this.setState({
selectedOptions: lastSelectedOption === lastWord ? selectedOptions.slice(0, -1) : selectedOptions,
});
}
}
}
}
renderSearchResults() {
const { state } = this;
return state.isOpen && !state.loading && (
<SearchOptionsMenu
errorMessage={state.errorMessage}
focusedOptionIndex={state.focusedOptionIndex}
menuOptions={state.searchResults}
onMouseDown={this.handleSelectSearchOption}
onMouseOver={this.handleFocusSearchOption}
searchQuery={this.getCurrentSearchQuery()}
/>
);
}
renderSearchInput() {
const { state } = this;
return (
<SearchInput
clearText={this.handleClearText}
isFocused={state.isSearchInputFocused}
onBlur={this.handBlur}
onChange={this.handleSearch}
onFocus={this.handleInputFocus}
value={state.searchQuery}
/>
);
}
render() {
return (
<div
className={cx('search-box', { 'search-box--expanded': this.state.isSearchInputFocused })}
onKeyDown={this.handleKeyDown}
>
{this.renderSearchInput()}
{this.renderSearchResults()}
</div>
);
}
}
export default SearchSelectBox;