-
Notifications
You must be signed in to change notification settings - Fork 12
/
ConceptSearchAutocomplete.jsx
180 lines (170 loc) · 5.71 KB
/
ConceptSearchAutocomplete.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
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
import React from 'react';
import {
LocalOfferOutlined as ConceptIcon,
} from '@mui/icons-material';
import { TextField, CircularProgress, ListItem, ListItemIcon, ListItemText, Divider, Typography } from '@mui/material';
import Autocomplete from '@mui/material/Autocomplete';
import { get, debounce, map } from 'lodash'
import APIService from '../../services/APIService';
import { BLUE } from '../../common/constants';
import AutocompleteLoading from './AutocompleteLoading';
const SubText = ({ label, value, divider }) => (
<span className='flex-vertical-center'>
<Typography
sx={{ display: 'inline', color: 'rgba(0, 0, 0, 0.6)' }}
component="span"
className='flex-vertical-center'>
<span className='flex-vertical-center'>
{
divider &&
<span className='flex-vertical-center' style={{backgroundColor: 'rgba(0, 0, 0, 0.6)', width: '3px', height: '3px', borderRadius: '100px', margin: '0 8px'}} />
}
<span className='flex-vertical-center'>
{
label &&
<span className='flex-vertical-center' style={{fontSize: '14px', marginRight: '5px'}}>
{`${label}:`}
</span>
}
<span className='flex-vertical-center' style={{fontSize: '14px'}}>
{value}
</span>
</span>
</span>
</Typography>
</span>
)
const SubTexts = ({ option }) => {
return (
<span className='flex-vertical-center'>
{
map([{field: 'id', label: 'ID'}, {field: 'concept_class', label: 'Concept Class'}], (field, index) => (
<SubText key={index} label={field.label} value={option[field.field]} divider={index != 0} />
))
}
</span>
)
}
const ConceptSearchAutocomplete = ({onChange, label, id, required, minCharactersForSearch, size, parentURI, disabled, value, freeSolo, onInputChange}) => {
const minLength = minCharactersForSearch || 1;
const [input, setInput] = React.useState('')
const [open, setOpen] = React.useState(false)
const [concepts, setConcepts] = React.useState([])
const [selected, setSelected] = React.useState('')
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)
fetchConcepts(value)
else
setLoading(false)
if(freeSolo && onInputChange)
onInputChange(id, value || '')
}, 300)
const handleChange = (event, id, item) => {
event.preventDefault()
event.stopPropagation()
setSelected(item)
onChange(id, item)
}
const fetchConcepts = searchStr => {
setLoading(true)
const query = {limit: 10, q: searchStr}
let service = parentURI ? APIService.new().overrideURL(parentURI).appendToUrl('concepts/') : APIService.concepts()
setConcepts([])
service.get(null, null, query).then(response => {
setConcepts(response.data)
setLoading(false)
})
}
React.useEffect(() => {
setSelected(value || '')
if(!value) {
setInput('')
setConcepts([])
}
}, [value])
return (
<Autocomplete
freeSolo={freeSolo}
disabled={disabled}
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 || 'concept'}
size={size || 'medium'}
options={concepts}
loading={loading}
loadingText={
loading ?
<AutocompleteLoading text={input} /> :
`Type atleast ${minLength} characters to search`
}
noOptionsText={(isSearchable && !loading) ? "No results" : 'Start typing...'}
getOptionLabel={option => option ? option.id : ''}
fullWidth
required={required}
onInputChange={handleInputChange}
onChange={(event, item) => handleChange(event, id || 'concept', item)}
renderInput={
params => (
<TextField
{...params}
value={input}
required
label={label || "Concept"}
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}>
<ListItem {...props}>
<ListItemIcon>
<ConceptIcon style={{color: BLUE}} fontSize='large' />
</ListItemIcon>
<ListItemText
primary={
<Typography
className='ellipsis-text-2'
sx={{ maxWidth: 'calc(100% - 5px)' }}>
<span>{option.display_name}</span>
{
option.display_locale &&
<span style={{color: 'rgba(0, 0, 0, 0.6)', marginLeft: '5px'}}>
<i>{`[${option.display_locale}]`}</i>
</span>
}
</Typography>
}
secondary={
<SubTexts option={option} />
}
/>
</ListItem>
<Divider variant="inset" component="li" />
</React.Fragment>
)
}
/>
);
}
export default ConceptSearchAutocomplete;