From 2e535ff60a492642761f6e08a2fa6ec4051142dd Mon Sep 17 00:00:00 2001 From: thiery Date: Fri, 6 Dec 2019 17:05:03 +0100 Subject: [PATCH 1/3] fix RadioButtonGroupInput onChange props --- packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx | 2 ++ .../ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx index e2ec097f990..c61ef2fa2b2 100644 --- a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx +++ b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx @@ -109,6 +109,7 @@ export const RadioButtonGroupInput: FunctionComponent< id, isRequired, meta: { error, touched }, + input, } = useInput({ format, onBlur, @@ -140,6 +141,7 @@ export const RadioButtonGroupInput: FunctionComponent< {choices.map(choice => ( { const { getChoiceText, getChoiceValue } = useChoices({ optionText, @@ -35,8 +36,8 @@ const RadioButtonGroupInputItem = ({ onChange(value)} {...inputProps} + onChange={() => onChange(value)} /> } /> From cbfbd577663077aef3bd9dbe816b53f9e8f30094 Mon Sep 17 00:00:00 2001 From: thiery Date: Wed, 11 Dec 2019 15:18:08 +0100 Subject: [PATCH 2/3] add unit test --- .../src/input/RadioButtonGroupInput.spec.tsx | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.spec.tsx b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.spec.tsx index 83bf5d8ded4..11fd1a3a536 100644 --- a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.spec.tsx @@ -1,6 +1,11 @@ import React from 'react'; import expect from 'expect'; -import { render, cleanup, fireEvent } from '@testing-library/react'; +import { + render, + cleanup, + fireEvent, + waitForDomChange, +} from '@testing-library/react'; import { Form } from 'react-final-form'; import { TestTranslationProvider } from 'ra-core'; @@ -41,6 +46,30 @@ describe('', () => { expect(input2.checked).toBeFalsy(); }); + it('should trigger custom onChange when clicking radio button', async () => { + const onChange = jest.fn(); + const { getByLabelText, queryByText } = render( +
( + + )} + /> + ); + expect(queryByText('Credit card')).not.toBeNull(); + const input1 = getByLabelText('VISA') as HTMLInputElement; + fireEvent.click(input1); + expect(onChange).toBeCalledWith('visa'); + + const input2 = getByLabelText('Mastercard') as HTMLInputElement; + fireEvent.click(input2); + expect(onChange).toBeCalledWith('mastercard'); + }); + it('should use the value provided by final-form as the initial input value', () => { const { getByLabelText } = render( Date: Wed, 11 Dec 2019 15:25:23 +0100 Subject: [PATCH 3/3] fix after rebase --- .../ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx b/packages/ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx index a5bce10d425..12acba7b925 100644 --- a/packages/ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx +++ b/packages/ra-ui-materialui/src/input/RadioButtonGroupInputItem.tsx @@ -20,7 +20,7 @@ const RadioButtonGroupInputItem = ({ const label = getChoiceText(choice); const value = getChoiceValue(choice); const { - input: { type, onChange, ...inputProps }, + input: { type, ...inputProps }, } = useField(source, { type: 'radio', value, @@ -37,7 +37,7 @@ const RadioButtonGroupInputItem = ({ id={nodeId} color="primary" {...inputProps} - onChange={() => onChange(value)} + onChange={(_, isActive) => isActive && onChange(value)} /> } />