From 078c4e47a18a5e755d824dedf35174037ac05da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Mon, 20 Dec 2021 11:23:22 -0300 Subject: [PATCH 1/3] Fix run input blur only when value changes --- packages/ra-ui-materialui/src/input/AutocompleteInput.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx index 58c598011fd..4788269da33 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -323,8 +323,6 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => { ? inputText(getChoiceText(selectedItem).props.record) : getChoiceText(selectedItem) ); - - inputEl.current.blur(); }, [ input.value, handleFilterChange, @@ -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 = () => { From 2a083800b2377fb5f14b51c8e830f6b2b7e63735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Mon, 20 Dec 2021 13:55:58 -0300 Subject: [PATCH 2/3] Added test --- .../src/input/AutocompleteInput.spec.tsx | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx index 1f672157612..3aee2f2cdb9 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx @@ -1,10 +1,14 @@ 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'; +import { Create } from '../detail'; describe('', () => { // Fix document.createRange is not a function error on fireEvent usage (Fixed in jsdom v16.0.0) @@ -923,4 +927,41 @@ describe('', () => { 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( + + + {({ formData, ...rest }) => { + return ( + + ); + }} + + + ); + const input = getByLabelText('Id', { + selector: 'input', + }) as HTMLInputElement; + fireEvent.focus(input); + userEvent.type(input, 'Hello World!'); + expect(input.value).toEqual('Hello World!'); + }); }); From c4f97235a2926692b1116fcf28c81668876490c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20Svarcas?= Date: Mon, 20 Dec 2021 14:08:57 -0300 Subject: [PATCH 3/3] Applied review --- packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx index 3aee2f2cdb9..47e1b85d24b 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx @@ -8,7 +8,6 @@ import { FormDataConsumer, TestTranslationProvider } from 'ra-core'; import { useCreateSuggestionContext } from './useSupportCreateSuggestion'; import { renderWithRedux } from 'ra-test'; import { SimpleForm } from '../form'; -import { Create } from '../detail'; describe('', () => { // Fix document.createRange is not a function error on fireEvent usage (Fixed in jsdom v16.0.0)