-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
fix autocomplete sets previous value using onInputChange #9243
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,13 @@ const defaultFilterOptions = createFilterOptions(); | |
* | ||
* @example | ||
* <AutocompleteInput source="author_id" options={{ color: 'secondary', InputLabelProps: { shrink: true } }} /> | ||
* | ||
* Retrieve the value displayed in the textbox using the `onInputChange` prop: | ||
* | ||
* @example | ||
* const [state, setState] = useState('') | ||
* | ||
* <AutocompleteInput source="gender" choices={choices} onInputChange={(_, newInputValue) => setState(newInputValue)} /> | ||
*/ | ||
export const AutocompleteInput = < | ||
OptionType extends RaRecord = RaRecord, | ||
|
@@ -440,33 +447,14 @@ If you provided a React element for the optionText prop, you must also provide t | |
useEffect(() => { | ||
if (!multiple) { | ||
const optionLabel = getOptionLabel(selectedChoice); | ||
if (typeof optionLabel === 'string') { | ||
setFilterValue(optionLabel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the issue was happening because of this effect. Removing the call to I'm keeping the |
||
} else { | ||
if (typeof optionLabel !== 'string') { | ||
throw new Error( | ||
'When optionText returns a React element, you must also provide the inputText prop' | ||
); | ||
} | ||
} | ||
}, [getOptionLabel, multiple, selectedChoice]); | ||
|
||
const handleInputChange: AutocompleteProps< | ||
OptionType, | ||
Multiple, | ||
DisableClearable, | ||
SupportCreate | ||
>['onInputChange'] = (event, newInputValue, reason) => { | ||
if ( | ||
event?.type === 'change' || | ||
!doesQueryMatchSelection(newInputValue) | ||
) { | ||
setFilterValue(newInputValue); | ||
debouncedSetFilter(newInputValue); | ||
} | ||
|
||
onInputChange?.(event, newInputValue, reason); | ||
}; | ||
|
||
const doesQueryMatchSelection = useCallback( | ||
(filter: string) => { | ||
let selectedItemTexts; | ||
|
@@ -515,13 +503,39 @@ If you provided a React element for the optionText prop, you must also provide t | |
return filteredOptions; | ||
}; | ||
|
||
const handleAutocompleteChange = ( | ||
event: any, | ||
newValue: any, | ||
_reason: string | ||
) => { | ||
handleChangeWithCreateSupport(newValue != null ? newValue : emptyValue); | ||
}; | ||
const handleAutocompleteChange = useCallback< | ||
AutocompleteProps< | ||
OptionType, | ||
Multiple, | ||
DisableClearable, | ||
SupportCreate | ||
>['onChange'] | ||
>( | ||
(_event, newValue, _reason) => { | ||
handleChangeWithCreateSupport( | ||
newValue != null ? newValue : emptyValue | ||
); | ||
}, | ||
[emptyValue, handleChangeWithCreateSupport] | ||
); | ||
|
||
const handleInputChange = useCallback< | ||
AutocompleteProps< | ||
OptionType, | ||
Multiple, | ||
DisableClearable, | ||
SupportCreate | ||
>['onInputChange'] | ||
>( | ||
(event, newInputValue, reason) => { | ||
setFilterValue(newInputValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MUI fires |
||
if (!doesQueryMatchSelection(newInputValue)) { | ||
debouncedSetFilter(newInputValue); | ||
} | ||
onInputChange?.(event, newInputValue, reason); | ||
}, | ||
[debouncedSetFilter, doesQueryMatchSelection, onInputChange] | ||
); | ||
|
||
const oneSecondHasPassed = useTimeout(1000, filterValue); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add this prop to the list of props in the 'Props' section, and create a new dedicated section for this prop with an example (similar to the other props)?