-
Notifications
You must be signed in to change notification settings - Fork 12
/
SourceSearchAutocomplete.jsx
127 lines (118 loc) · 3.98 KB
/
SourceSearchAutocomplete.jsx
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
import React from 'react';
import { TextField, CircularProgress, Divider } from '@mui/material';
import Autocomplete from '@mui/material/Autocomplete';
import { get, debounce, map } from 'lodash'
import APIService from '../../services/APIService';
import { styled } from '@mui/system';
import AutocompleteLoading from './AutocompleteLoading';
import SourceListItem from './SourceListItem';
const GroupHeader = styled('div')({
position: 'sticky',
top: '-8px',
padding: '4px 16px',
zIndex: 1000,
backgroundColor: '#f5f5f5',
fontSize: '12px',
color: 'rgba(0, 0, 0, 0.6)',
borderBottom: '1px solid rgba(0, 0, 0, 0.12)'
});
const GroupItems = styled('ul')({
padding: 0,
});
const SourceSearchAutocomplete = ({onChange, label, id, required, minCharactersForSearch, size, suggested}) => {
const minLength = minCharactersForSearch || 2;
const [input, setInput] = React.useState('')
const [open, setOpen] = React.useState(false)
const [sources, setSources] = React.useState(map(suggested || [], instance => ({...instance, resultType: 'Suggested Sources'})))
const [selected, setSelected] = React.useState(undefined)
const [loading, setLoading] = React.useState(false)
const isSearchable = input && input.length >= minLength;
const handleInputChange = debounce((event, value, reason) => {
setInput(value || '')
if(reason !== 'reset' && value && value.length >= minLength)
fetchSources(value)
else
setLoading(false)
}, 300)
const handleChange = (event, id, item) => {
event.preventDefault()
event.stopPropagation()
setSelected(item)
onChange(id, item)
}
const fetchSources = searchStr => {
setLoading(true)
setSources([])
const query = {limit: 25, q: searchStr, includeSummary: true}
APIService.sources().get(null, null, query).then(response => {
const sources = response.data
setSources(map(sources, source => ({...source, resultType: 'Search Results'})))
setLoading(false)
})
}
return (
<Autocomplete
filterOptions={x => x}
openOnFocus
blurOnSelect
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
isOptionEqualToValue={(option, value) => option.url === get(value, 'url')}
value={selected}
id={id || 'source'}
size={size || 'medium'}
options={sources}
loading={loading}
loadingText={
loading ?
<AutocompleteLoading text={input} /> :
`Type atleast ${minLength} characters to search`
}
noOptionsText={(isSearchable && !loading) ? "No results" : 'Start typing...'}
getOptionLabel={option => option ? option.name : ''}
fullWidth
required={required}
onInputChange={handleInputChange}
onChange={(event, item) => handleChange(event, id || 'source', item)}
groupBy={option => option.resultType}
renderGroup={params => (
<li style={{listStyle: 'none'}} key={params.group}>
<GroupHeader>{params.group}</GroupHeader>
<GroupItems>{params.children}</GroupItems>
</li>
)}
renderInput={
params => (
<TextField
{...params}
value={input}
required
label={label || "Source"}
variant="outlined"
fullWidth
size={size || 'medium'}
InputProps={{
...params.InputProps,
endAdornment: (
<React.Fragment>
{loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</React.Fragment>
),
}}
/>
)
}
renderOption={
(props, option) => (
<React.Fragment key={option.url}>
<SourceListItem listItemProps={props} option={option} />
<Divider variant="inset" component="li" style={{listStyle: 'none'}} />
</React.Fragment>
)
}
/>
);
}
export default SourceSearchAutocomplete;