-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenConceptLab/ocl_issues#845 | Source/Collection form | async locale…
…s on typing
- Loading branch information
1 parent
f6ab3d7
commit e6f14f6
Showing
2 changed files
with
106 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react'; | ||
import { TextField, CircularProgress } from '@material-ui/core'; | ||
import Autocomplete from '@material-ui/lab/Autocomplete'; | ||
import { get, debounce, map, orderBy, isEmpty } from 'lodash' | ||
import APIService from '../../services/APIService'; | ||
|
||
const LocaleAutoComplete = ({ id, selected, multiple, minCharactersForSearch, required, onChange, label, error, ...rest }) => { | ||
const minLength = minCharactersForSearch || 2; | ||
const [input, setInput] = React.useState('') | ||
const [open, setOpen] = React.useState(false) | ||
const [fetched, setFetched] = React.useState(false) | ||
const [locales, setLocales] = React.useState([]) | ||
|
||
const isSearchable = val => val && val.length >= minLength | ||
const loading = Boolean(open && !fetched && isSearchable(input) && isEmpty(locales)) | ||
const handleInputChange = (event, value) => { | ||
const val = value || '' | ||
setInput(val) | ||
setFetched(false) | ||
if(isSearchable(val)) | ||
fetchLocales(val) | ||
} | ||
|
||
const searchLocales = searchStr => { | ||
APIService | ||
.sources('Locales') | ||
.concepts() | ||
.get(null, null, {limit: 25, q: searchStr, is_latest: true}) | ||
.then(response => { | ||
const _locales = orderBy(map(response.data, l => ({id: l.locale || l.display_locale, name: `${l.display_name} [${l.locale || l.display_locale}]`, uuid: l.uuid})), 'name'); | ||
setLocales(_locales) | ||
setFetched(true) | ||
}) | ||
} | ||
|
||
const fetchLocales = React.useCallback(debounce(searchLocales, 700), []) | ||
|
||
return ( | ||
<Autocomplete | ||
openOnFocus | ||
fullWidth | ||
blurOnSelect={!multiple} | ||
disableCloseOnSelect={Boolean(multiple)} | ||
multiple={Boolean(multiple)} | ||
required={required} | ||
open={open} | ||
onOpen={() => { | ||
setOpen(true); | ||
}} | ||
onClose={() => { | ||
setOpen(false); | ||
}} | ||
getOptionSelected={(option, value) => option.uuid === get(value, 'uuid')} | ||
value={selected || ''} | ||
id={id || 'localesAutoComplete'} | ||
options={locales} | ||
loading={loading} | ||
loadingText={loading ? 'Loading...' : `Type atleast ${minLength} characters to search`} | ||
noOptionsText={(isSearchable(input) && !loading) ? "No results" : 'Start typing...'} | ||
getOptionLabel={option => {return option.name || ''}} | ||
onInputChange={handleInputChange} | ||
onChange={(event, item) => onChange(id || 'localesAutoComplete', item)} | ||
renderInput={ | ||
params => ( | ||
<TextField | ||
{...params} | ||
value={input} | ||
required={required} | ||
label={label} | ||
error={error} | ||
variant="outlined" | ||
fullWidth | ||
InputProps={{ | ||
...params.InputProps, | ||
endAdornment: ( | ||
<React.Fragment> | ||
{loading ? <CircularProgress color="inherit" size={20} /> : null} | ||
{params.InputProps.endAdornment} | ||
</React.Fragment> | ||
), | ||
}} | ||
/> | ||
) | ||
} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
export default LocaleAutoComplete; |