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 RadioButtonGroupInput onChange props #4123

Merged
merged 3 commits into from
Dec 12, 2019
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
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -41,6 +46,30 @@ describe('<RadioButtonGroupInput />', () => {
expect(input2.checked).toBeFalsy();
});

it('should trigger custom onChange when clicking radio button', async () => {
const onChange = jest.fn();
const { getByLabelText, queryByText } = render(
<Form
onSubmit={jest.fn}
render={() => (
<RadioButtonGroupInput
{...defaultProps}
label="Credit card"
onChange={onChange}
/>
)}
/>
);
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(
<Form
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const RadioButtonGroupInput: FunctionComponent<
id,
isRequired,
meta: { error, touched },
input,
} = useInput({
format,
onBlur,
Expand Down Expand Up @@ -140,6 +141,7 @@ export const RadioButtonGroupInput: FunctionComponent<
<RadioGroup id={id} row={row} {...options}>
{choices.map(choice => (
<RadioButtonGroupInputItem
{...input}
key={get(choice, optionValue)}
choice={choice}
optionText={optionText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const RadioButtonGroupInputItem = ({
optionValue,
source,
translateChoice,
onChange,
}) => {
const { getChoiceText, getChoiceValue } = useChoices({
optionText,
Expand All @@ -19,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,
Expand All @@ -35,8 +36,8 @@ const RadioButtonGroupInputItem = ({
<Radio
id={nodeId}
color="primary"
onChange={() => onChange(value)}
{...inputProps}
onChange={(_, isActive) => isActive && onChange(value)}
/>
}
/>
Expand Down