Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prop to preserve search input of multiselect autocomplete #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/components/inputs/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const Autocomplete: React.FC<AutocompleteProps<any, any, any, any>> = ({
typographyContentColor = 'textSecondary',
inputSelectedColor,
isSearchable = true,
preserveSearch = false,
getOptionDisabled,
placeholder,
inputTextFieldProps,
Expand All @@ -77,6 +78,7 @@ const Autocomplete: React.FC<AutocompleteProps<any, any, any, any>> = ({
const [localLoading, setLocalLoading] = useState(false)
const loading = receivedLoading || localLoading

const [multiselectSearchInput, setMultiselectSearchInput] = React.useState('')
const [localInput, setLocalInput] = useState<string>()
const [optionsLoaded, setOptionsLoaded] = useState(false)

Expand Down Expand Up @@ -256,11 +258,19 @@ const Autocomplete: React.FC<AutocompleteProps<any, any, any, any>> = ({

const handleInputChange = useCallback(
(event: React.SyntheticEvent, value: string, reason: AutocompleteInputChangeReason) => {
const eventType = event?.nativeEvent?.type

if (preserveSearch) {
if (reason === 'clear') setMultiselectSearchInput('')
if (!event || eventType === 'click' || eventType === 'keydown') return
if (eventType === 'input') setMultiselectSearchInput(value)
}

setLocalInput(value ? value : '')
if (onInputChange) onInputChange(event, value, reason)

// this prevents the component from calling loadOptions again when the user clicks outside it and the menu closes
if (event?.nativeEvent?.type === 'focusout') {
if (eventType === 'focusout') {
setOptionsLoaded(false)
return
}
Expand All @@ -270,7 +280,7 @@ const Autocomplete: React.FC<AutocompleteProps<any, any, any, any>> = ({
handleLoadOptions(value)
}
},
[handleLoadOptions, loadOptions, onInputChange]
[handleLoadOptions, loadOptions, onInputChange, preserveSearch]
)

useEffect(() => {
Expand Down Expand Up @@ -313,6 +323,7 @@ const Autocomplete: React.FC<AutocompleteProps<any, any, any, any>> = ({
typographyContentColor={typographyContentColor}
forcePopupIcon
label={label}
inputValue={isMultiSelection && preserveSearch ? multiselectSearchInput : undefined}
disabled={disabled || isValueDisabled}
loading={loading}
loadingText={loadingText ?? <LinearProgress />}
Expand Down Expand Up @@ -415,6 +426,11 @@ Autocomplete.propTypes = {
* If false, the user cannot type in Autocomplete, filter options or create new ones.
*/
isSearchable: PropTypes.bool,
/**
* @default false
* If true and multiple value selection is also enabled, then this will prevent the user's search input from being reset after making a selection
*/
preserveSearch: PropTypes.bool,
/**
* @default false
* If true, the Autocomplete is free solo, meaning that the user input is not bound to provided options and can add
Expand Down
5 changes: 5 additions & 0 deletions src/components/inputs/Autocomplete/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export interface AutocompleteProps<
* If false, the user cannot type in Autocomplete, filter options or create new ones.
*/
isSearchable?: boolean
/**
* @default false
* If true and multiple value selection is also enabled, then this will prevent the user's search input from being reset after making a selection
*/
preserveSearch?: boolean
/**
* @default false
* If true, the value set on change will be set to option[valueKey]/
Expand Down