This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/
searchBar.js
311 lines (260 loc) · 8.78 KB
/
searchBar.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import React, {Component} from 'react';
import keycode from 'keycode';
import Downshift from 'downshift';
import uuidv4 from 'uuid/v4';
import decodeUriComponent from 'decode-uri-component';
import {isEqual, noop} from 'lodash';
import styled from '@emotion/styled';
import {css} from 'emotion';
import ClearIcon from '@material-ui/icons/Clear';
import PropTypes from '../../utils/propTypes';
import {IconButton} from '../iconButton';
import SearchInput from './overrides/searchInput';
import {ice, white} from '../../variables/colors';
const InputWrapper = styled('div')`
border: 1px solid ${ice};
background-color: ${white};
display: flex;
align-items: center;
justify-content: space-between;
min-height: 40px;
border-radius: 20px;
padding: 3px;
`;
const searchInputWrapper = css`
flex-grow: 1;
`;
const clearButton = css`
border: none;
z-index: 1;
`;
// use getRootProps https://github.com/paypal/downshift#getrootprops
const SearchInputWrapper = ({innerRef, ...rest}) => (
<div
className={searchInputWrapper}
ref={innerRef}
{...rest}
/>
);
SearchInputWrapper.propTypes = {
innerRef: PropTypes.func.isRequired,
};
class SearchBar extends Component {
constructor(props) {
super(props);
this.input = React.createRef();
}
componentDidMount() {
// init suggestions
const {location, setState, selectedItem} = this.props;
let newSelectedItem = [];
// fill search from state.location
if (location && location.query && location.query.search) {
// get groups separated by -OR-
const groups = location.query.search.split('-OR-');
newSelectedItem = groups.reduce((p, group) => {
// create related chips
const chips = group.split(',').map((o) => {
const el = decodeUriComponent(o).split(':');
return {
parent: el[0],
child: el.splice(1).join(':'),
uuid: uuidv4(),
isLogic: false,
};
});
// do not hoist it, uuid need to be different
const logic = {
parent: '-OR-',
child: '',
uuid: uuidv4(),
isLogic: true,
};
return [
...p,
...(group ? chips : []),
logic, // will add an extra logic el on the last iteration
];
}, []).slice(0, -1); // remove last added `-OR-`
}
if (!isEqual(selectedItem, newSelectedItem)) {
setState({
selectedItem: newSelectedItem,
toUpdate: false,
});
}
}
handleKeyDown = (event) => {
const {setState, inputValue, selectedItem} = this.props;
if (selectedItem.length && !inputValue.length && keycode(event) === 'backspace') {
const l = selectedItem.length;
if (l) {
const newSelectedItems = selectedItem.slice(0, l - 1);
const last = selectedItem[l - 1];
setState({
selectedItem: newSelectedItems,
isParent: true,
item: '',
toUpdate: !(last.isLogic || !last.child),
});
}
}
};
handleInputChange = (event) => {
const {setState} = this.props;
setState({
inputValue: event.target.value,
toUpdate: false,
});
};
handleChange = (item) => {
const {
parentSuggestions, isParent, selectedItem,
setState,
} = this.props;
let newSelectedItem,
toUpdate = false;
if (!isParent) { // remove precedent parent and add child
const prev = selectedItem.pop();
newSelectedItem = [...selectedItem, {...prev, child: item.label}];
toUpdate = true;
}
// if is parent, simply add
else {
newSelectedItem = [
...selectedItem, {
parent: item.label,
child: '',
uuid: uuidv4(),
isLogic: item.isLogic,
}];
}
// calculate if previous in parent menu
const selectedParent = parentSuggestions.map(o => o.label).includes(item.label);
// set item in redux, and launch related sagas for fetching list if needed
setState({
isParent: item.isLogic || !selectedParent,
inputValue: '',
selectedItem: newSelectedItem,
item: selectedParent || item.isLogic ? item.label : '',
toUpdate,
});
};
handleDelete = item => () => {
const {setState, selectedItem} = this.props;
// remove empty parent, the item we want to delete
let newSelectedItems = selectedItem.filter(o => !(
(o.child === '' && !o.isLogic) // remove parent without child
|| o.uuid === item.uuid), // remove item clicked
);
// remove -OR- item if not a chip before (i.e nothing or another -OR-)
newSelectedItems = newSelectedItems.filter((o, i) => !(
o.isLogic && i === 0 // remove first item if isLogic
|| o.isLogic && i > 0 && newSelectedItems[i - 1].isLogic), // remove isLogic if precedent isLogic
);
// need to setItem correctly after deleting
const l = newSelectedItems.length,
last = l ? newSelectedItems[l - 1] : undefined;
setState({
selectedItem: newSelectedItems,
isParent: true,
item: last && last.isLogic ? '-OR-' : '',
toUpdate: true,
});
this.clickInput();
};
handleOuterClick = () => {
const {setState, inputValue} = this.props;
if (inputValue) {
setState({
inputValue: '',
});
}
};
clear = () => {
const {setState} = this.props;
setState({
selectedItem: [],
isParent: true,
item: '',
toUpdate: true,
});
this.clickInput();
};
clickInput = () => {
setTimeout(() => {
// should appear after blur of input (need to call setTimeout as downshift does)
// stay on focus
this.input.current.focus();
}, 0);
};
searchInput = (props) => {
const {suggestions, placeholder} = this.props;
// due to ugly render props philosophy of Downshift, we have to extract the declaration in a function...
return (
<SearchInputWrapper {...props.getRootProps({refKey: 'innerRef'})}>
<SearchInput
handleKeyDown={this.handleKeyDown}
handleInputChange={this.handleInputChange}
handleDelete={this.handleDelete}
suggestions={suggestions}
clickInput={this.clickInput}
input={this.input}
placeholder={placeholder}
{...props}
/>
</SearchInputWrapper>
);
};
itemToString = item => item === null ? '' : item.label;
render() {
const {inputValue, selectedItem} = this.props;
return (
<InputWrapper>
<Downshift
inputValue={inputValue}
onChange={this.handleChange}
onOuterClick={this.handleOuterClick}
selectedItem={selectedItem}
itemToString={this.itemToString}
>
{this.searchInput}
</Downshift>
<IconButton
Icon={ClearIcon}
iconSize={18}
onClick={this.clear}
className={clearButton}
/>
</InputWrapper>
);
}
}
SearchBar.propTypes = {
location: PropTypes.shape({
query: PropTypes.shape({
search: PropTypes.string,
}),
}),
suggestions: PropTypes.arrayOf(PropTypes.shape({})),
parentSuggestions: PropTypes.arrayOf(PropTypes.shape({})),
setState: PropTypes.func,
inputValue: PropTypes.string,
selectedItem: PropTypes.arrayOf(PropTypes.shape({
isLogic: PropTypes.bool,
child: PropTypes.string,
})),
isParent: PropTypes.bool,
placeholder: PropTypes.string,
};
SearchBar.defaultProps = {
location: null,
suggestions: [],
parentSuggestions: [],
setState: noop,
inputValue: '',
selectedItem: [],
isParent: true,
placeholder: '',
};
export default SearchBar;