Skip to content

Commit a8bbac3

Browse files
committed
Added radio.js
1 parent a2370c5 commit a8bbac3

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

packages/ant-component-mapper/src/common/form-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ AntForm.propTypes = {
3131
help: PropTypes.string,
3232
isRequired: PropTypes.bool,
3333
component: PropTypes.string,
34-
invalid: PropTypes.string
34+
invalid: PropTypes.oneOfType(PropTypes.string, PropTypes.bool)
3535
};
3636

3737
export default AntForm;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TextArea from './text-area';
77
import Checkbox from './checkbox';
88
import DatePicker from './date-picker';
99
import TimePicker from './time-picker';
10+
import Radio from './radio';
1011

1112
export const components = {};
1213

@@ -16,7 +17,7 @@ const componentMapper = {
1617
[componentTypes.SELECT]: () => <div>Select field</div>,
1718
[componentTypes.CHECKBOX]: Checkbox,
1819
[componentTypes.SUB_FORM]: () => <div>sub form</div>,
19-
[componentTypes.RADIO]: () => <div>Radio field</div>,
20+
[componentTypes.RADIO]: Radio,
2021
[componentTypes.TABS]: Tabs,
2122
[componentTypes.DATE_PICKER]: DatePicker,
2223
[componentTypes.TIME_PICKER]: TimePicker,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Radio as AntRadio } from 'antd';
4+
import { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
5+
import AntForm from '../common/form-wrapper';
6+
import { validationError } from '../common/helpers';
7+
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
8+
9+
const RadioOption = ({ name, option }) => {
10+
const { input } = useFieldApi({ name, type: 'radio', value: option.value });
11+
return (
12+
<AntRadio key={`${name}-${option.value}`} {...input} id={`${name}-${option.value}`}>
13+
{option.label}
14+
</AntRadio>
15+
);
16+
};
17+
18+
RadioOption.propTypes = {
19+
name: PropTypes.string.isRequired,
20+
option: PropTypes.shape({ label: PropTypes.string.isRequired, value: PropTypes.any.isRequired }).isRequired
21+
};
22+
23+
const Radio = ({ name, ...props }) => {
24+
const { options, isDisabled, label, isRequired, helperText, description, isReadOnly, meta, validateOnMount } = useFieldApi({
25+
...props,
26+
name,
27+
type: 'radio'
28+
});
29+
const invalid = validationError(meta, validateOnMount);
30+
const text = invalid || helperText || description;
31+
return (
32+
<AntForm layout="vertical" isRequired={isRequired} label={label} invalid={invalid} help={text}>
33+
<AntRadio.Group name={name} disabled={isDisabled || isReadOnly}>
34+
{options.map((option) => (
35+
<RadioOption key={option.value} name={name} option={option} />
36+
))}
37+
</AntRadio.Group>
38+
</AntForm>
39+
);
40+
};
41+
42+
Radio.propTypes = {
43+
...wrapperProps,
44+
options: PropTypes.arrayOf(
45+
PropTypes.shape({
46+
value: PropTypes.any,
47+
label: PropTypes.node
48+
})
49+
),
50+
label: PropTypes.node.isRequired,
51+
isDisabled: PropTypes.bool,
52+
isReadOnly: PropTypes.bool,
53+
children: PropTypes.any,
54+
description: PropTypes.node
55+
};
56+
57+
Radio.defaultProps = {
58+
options: []
59+
};
60+
61+
export default Radio;

0 commit comments

Comments
 (0)