-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
47 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
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,8 @@ | ||
import { AutocompleteProps as MuiAutocompleteProps } from '@mui/material/Autocomplete'; | ||
|
||
export type AutocompleteProps = MuiAutocompleteProps & { | ||
creatable?: boolean; | ||
}; | ||
|
||
declare const Autocomplete: (props: AutocompleteProps) => JSX.Element; | ||
export default Autocomplete; |
79 changes: 79 additions & 0 deletions
79
packages/react-ui/src/components/molecules/Autocomplete.js
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,79 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Autocomplete as MuiAutocomplete, | ||
createFilterOptions, | ||
TextField | ||
} from '@mui/material'; | ||
|
||
const filter = createFilterOptions(); | ||
|
||
const Autocomplete = ({ options, creatable, ...otherProps }) => { | ||
const [value, setValue] = React.useState(null); | ||
|
||
return ( | ||
<MuiAutocomplete | ||
{...otherProps} | ||
creatable={creatable} | ||
value={value} | ||
onChange={(event, newValue) => { | ||
if (typeof newValue === 'string') { | ||
setValue({ | ||
title: newValue | ||
}); | ||
} else if (newValue && newValue.inputValue) { | ||
// Create a new value from the user input | ||
setValue({ | ||
title: newValue.inputValue | ||
}); | ||
} else { | ||
setValue(newValue); | ||
} | ||
}} | ||
filterOptions={(options, params) => { | ||
const filtered = filter(options, params); | ||
|
||
const { inputValue } = params; | ||
// Suggest the creation of a new value | ||
const isExisting = options.some((option) => inputValue === option.title); | ||
if (inputValue !== '' && !isExisting) { | ||
filtered.push({ | ||
inputValue, | ||
title: `Add "${inputValue}"` | ||
}); | ||
} | ||
|
||
return filtered; | ||
}} | ||
selectOnFocus | ||
clearOnBlur | ||
handleHomeEndKeys | ||
options={options} | ||
getOptionLabel={(option) => { | ||
// Value selected with enter, right from the input | ||
if (typeof option === 'string') { | ||
return option; | ||
} | ||
// Add "xxx" option created dynamically | ||
if (option.inputValue) { | ||
return option.inputValue; | ||
} | ||
// Regular option | ||
return option.title; | ||
}} | ||
renderOption={(props, option) => <li {...props}>{option.title}</li>} | ||
sx={{ width: 300 }} | ||
freeSolo | ||
renderInput={(params) => <TextField {...params} label='Free solo with text demo' />} | ||
/> | ||
); | ||
}; | ||
|
||
Autocomplete.defaultProps = { | ||
size: 'medium' | ||
}; | ||
Autocomplete.propTypes = { | ||
creatable: PropTypes.bool | ||
}; | ||
|
||
export default Autocomplete; |
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
Oops, something went wrong.