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

fix AutocompleteInput's onInputChange is never called #9240

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1532,4 +1532,24 @@ describe('<AutocompleteInput />', () => {
await checkInputValue('prefers_zero-number', '0');
await checkInputValue('prefers_valid-value', '1');
});

it('should call the onInputChange callback', async () => {
const onInputChange = jest.fn();

render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()}>
<AutocompleteInput
{...defaultProps}
onInputChange={onInputChange}
/>
</SimpleForm>
</AdminContext>
);
const input = screen.getByLabelText(
'resources.users.fields.role'
) as HTMLInputElement;
fireEvent.change(input, { target: { value: 'newValue' } });
await waitFor(() => expect(onInputChange).toHaveBeenCalled());
});
});
14 changes: 9 additions & 5 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export const AutocompleteInput = <
translateChoice,
validate,
variant,
onInputChange,
...rest
} = props;

Expand Down Expand Up @@ -449,18 +450,21 @@ If you provided a React element for the optionText prop, you must also provide t
}
}, [getOptionLabel, multiple, selectedChoice]);

const handleInputChange = (
event: any,
newInputValue: string,
_reason: string
) => {
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(
Expand Down