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 run input blur only when value changes #7013

Merged
merged 3 commits into from
Dec 20, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { AutocompleteInput } from './AutocompleteInput';
import { Form } from 'react-final-form';
import { TestTranslationProvider } from 'ra-core';
import { FormDataConsumer, TestTranslationProvider } from 'ra-core';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
import { renderWithRedux } from 'ra-test';
import { SimpleForm } from '../form';

describe('<AutocompleteInput />', () => {
// Fix document.createRange is not a function error on fireEvent usage (Fixed in jsdom v16.0.0)
Expand Down Expand Up @@ -923,4 +926,41 @@ describe('<AutocompleteInput />', () => {

expect(queryByText('New Kid On The Block')).not.toBeNull();
});

it("should allow to edit the input if it's inside a FormDataConsumer", () => {
const { getByLabelText } = renderWithRedux(
<SimpleForm validateOnBlur basePath="/posts" resource="posts">
<FormDataConsumer>
{({ formData, ...rest }) => {
return (
<AutocompleteInput
label="Id"
choices={[
{
name: 'General Practitioner',
id: 'GeneralPractitioner',
},
{
name: 'Physiotherapist',
id: 'Physiotherapist',
},
{
name: 'Clinical Pharmacist',
id: 'ClinicalPharmacist',
},
]}
source="id"
/>
);
}}
</FormDataConsumer>
</SimpleForm>
);
const input = getByLabelText('Id', {
selector: 'input',
}) as HTMLInputElement;
fireEvent.focus(input);
userEvent.type(input, 'Hello World!');
expect(input.value).toEqual('Hello World!');
});
});
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
? inputText(getChoiceText(selectedItem).props.record)
: getChoiceText(selectedItem)
);

inputEl.current.blur();
}, [
input.value,
handleFilterChange,
Expand All @@ -333,6 +331,10 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
inputText,
]);

useEffect(() => {
inputEl.current.blur();
}, [input.value]);

// This function ensures that the suggestion list stay aligned to the
// input element even if it moves (because user scrolled for example)
const updateAnchorEl = () => {
Expand Down