Skip to content

Commit b333c32

Browse files
authored
Merge pull request #1208 from rvsia/fixAntRadio
fix(ant): fix radio, make initial values work
2 parents d1974d2 + 959c506 commit b333c32

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

packages/ant-component-mapper/src/radio/radio.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@ import { wrapperProps } from '@data-driven-forms/common/multiple-choice-list';
55
import FormGroup from '../form-group';
66
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
77

8-
const RadioOption = ({ name, option, ...rest }) => {
9-
const { input } = useFieldApi({ name, type: 'radio', value: option.value });
10-
return (
11-
<AntRadio key={`${name}-${option.value}`} {...input} id={`${name}-${option.value}`} {...rest}>
12-
{option.label}
13-
</AntRadio>
14-
);
15-
};
8+
const RadioOption = ({ name, option: { label, value, ...rest } }) => (
9+
<AntRadio key={`${name}-${value}`} id={`${name}-${value}`} value={value} {...rest}>
10+
{label}
11+
</AntRadio>
12+
);
1613

1714
RadioOption.propTypes = {
1815
name: PropTypes.string.isRequired,
1916
option: PropTypes.shape({ label: PropTypes.string.isRequired, value: PropTypes.any.isRequired }).isRequired,
2017
};
2118

22-
const Radio = ({ name, ...props }) => {
23-
const { options, isDisabled, label, isRequired, helperText, description, isReadOnly, meta, validateOnMount, FormItemProps, ...rest } = useFieldApi({
24-
...props,
25-
name,
26-
type: 'radio',
27-
});
19+
const Radio = ({ name, component, ...props }) => {
20+
const { options, isDisabled, label, isRequired, helperText, description, isReadOnly, meta, validateOnMount, FormItemProps, input, ...rest } =
21+
useFieldApi({
22+
...props,
23+
name,
24+
});
2825

2926
return (
3027
<FormGroup
@@ -36,7 +33,7 @@ const Radio = ({ name, ...props }) => {
3633
FormItemProps={FormItemProps}
3734
isRequired={isRequired}
3835
>
39-
<AntRadio.Group name={name} disabled={isDisabled || isReadOnly} {...rest}>
36+
<AntRadio.Group name={name} disabled={isDisabled || isReadOnly} {...input} {...rest}>
4037
{options.map((option) => (
4138
<RadioOption key={option.value} name={name} option={option} />
4239
))}
@@ -58,6 +55,7 @@ Radio.propTypes = {
5855
isReadOnly: PropTypes.bool,
5956
description: PropTypes.node,
6057
FormItemProps: PropTypes.object,
58+
component: PropTypes.string,
6159
};
6260

6361
Radio.defaultProps = {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React from 'react';
2+
import { FormRenderer } from '@data-driven-forms/react-form-renderer';
3+
import { act, render, screen } from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
6+
import { componentMapper, FormTemplate } from '../index';
7+
8+
describe('<Radio />', () => {
9+
let initialProps;
10+
let onSubmit;
11+
12+
const schema = {
13+
fields: [
14+
{
15+
component: 'radio',
16+
label: 'Radio',
17+
name: 'radio',
18+
initialValue: '2',
19+
options: [
20+
{
21+
label: 'Dogs',
22+
value: '1',
23+
},
24+
{
25+
label: 'Cats',
26+
value: '2',
27+
},
28+
{
29+
label: 'Hamsters',
30+
value: '3',
31+
},
32+
],
33+
},
34+
],
35+
};
36+
37+
beforeEach(() => {
38+
onSubmit = jest.fn();
39+
initialProps = {
40+
onSubmit: (values) => onSubmit(values),
41+
componentMapper,
42+
FormTemplate: (props) => <FormTemplate {...props} />,
43+
schema,
44+
};
45+
});
46+
47+
it('initialValues work', async () => {
48+
render(<FormRenderer {...initialProps} initialValues={{ radio: '2' }} />);
49+
50+
expect([...screen.getAllByRole('radio')].map((r) => [r.name, r.value, r.checked])).toEqual([
51+
['radio', '1', false],
52+
['radio', '2', true],
53+
['radio', '3', false],
54+
]);
55+
56+
await act(async () => {
57+
userEvent.click(screen.getByText('Submit'));
58+
});
59+
60+
expect(onSubmit).toHaveBeenLastCalledWith({ radio: '2' });
61+
62+
userEvent.click(screen.getByText('Hamsters'));
63+
64+
expect([...screen.getAllByRole('radio')].map((r) => [r.name, r.value, r.checked])).toEqual([
65+
['radio', '1', false],
66+
['radio', '2', false],
67+
['radio', '3', true],
68+
]);
69+
70+
await act(async () => {
71+
userEvent.click(screen.getByText('Submit'));
72+
});
73+
74+
expect(onSubmit).toHaveBeenLastCalledWith({ radio: '3' });
75+
});
76+
});

0 commit comments

Comments
 (0)