react-garden
Version:
React + TypeScript + ThreeJS app using Material UI on NextJS, Apollo Client, GraphQL + WordPress REST APIs, for ThreeD web development.. a part of the threed.ai code family.
70 lines (61 loc) • 1.88 kB
JavaScript
// ** React Imports
import { useState } from 'react'
// ** MUI Imports
import TextField from '@mui/material/TextField'
import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete'
// ** Data
import { top100Films } from '~/@fake-db/autocomplete'
const filter = createFilterOptions()
const AutocompleteCreatable = () => {
// ** State
const [value, setValue] = useState(null)
return (
<Autocomplete
freeSolo
clearOnBlur
value={value}
selectOnFocus
handleHomeEndKeys
sx={{ width: 250 }}
options={top100Films}
id='autocomplete-free-solo-with-text'
renderOption={(props, option) => <li {...props}>{option.title}</li>}
renderInput={params => <TextField {...params} label='Free solo with text demo' />}
getOptionLabel={option => {
if (typeof option === 'string') {
return option
}
if (option.inputValue) {
return option.inputValue
}
return option.title
}}
onChange={(event, newValue) => {
if (typeof newValue === 'string') {
setValue({
title: newValue
})
} else if (newValue && newValue.inputValue) {
setValue({
title: newValue.inputValue
})
} else {
setValue(newValue)
}
}}
filterOptions={(options, params) => {
const filtered = filter(options, params)
const { inputValue } = params
const isExisting = options.some(option => inputValue === option.title)
if (inputValue !== '' && !isExisting) {
filtered.push({
inputValue,
title: `Add "${inputValue}"`
})
}
return filtered
}}
/>
)
}
export default AutocompleteCreatable