Skip to content

Commit eb31dd0

Browse files
authored
Merge pull request #633 from rvsia/antCustomization
fix(ant): refactor and add customization for each component
2 parents d94ed44 + a6be195 commit eb31dd0

25 files changed

+452
-166
lines changed

packages/ant-component-mapper/demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const App = () => (
2020
componentMapper={componentMapper}
2121
FormTemplate={(props) => <FormTemplate layout='vertical' {...props} />}
2222
onSubmit={console.log}
23-
schema={demoSchema}
23+
schema={wizardSchema}
2424
/>
2525
</div>
2626
);

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33

44
import { Form } from 'antd';
5+
import { validationError } from './helpers';
6+
import { childrenPropTypes } from '@data-driven-forms/common/src/prop-types-templates';
7+
8+
const AntForm = ({ label, children, isRequired, FormItemProps, meta, validateOnMount, helperText, description, hideLabel }) => {
9+
const invalid = validationError(meta, validateOnMount);
10+
const help = invalid || helperText || description;
511

6-
const AntForm = ({ label, children, layout, component, invalid, isRequired, help }) => {
712
return (
8-
<Form.Item validateStatus={!invalid ? '' : 'error'} help={help} label={label} name={label}>
13+
<Form.Item validateStatus={!invalid ? '' : 'error'} help={help} label={!hideLabel && label} name={label} required={isRequired} {...FormItemProps}>
914
<div>{children}</div>
1015
</Form.Item>
1116
);
1217
};
1318

1419
AntForm.propTypes = {
1520
label: PropTypes.string,
16-
layout: PropTypes.string,
17-
children: PropTypes.object,
21+
children: childrenPropTypes,
1822
help: PropTypes.string,
1923
isRequired: PropTypes.bool,
20-
component: PropTypes.string,
21-
invalid: PropTypes.oneOfType([PropTypes.string, PropTypes.bool])
24+
invalid: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
25+
FormItemProps: PropTypes.object,
26+
meta: PropTypes.object,
27+
validateOnMount: PropTypes.bool,
28+
helperText: PropTypes.node,
29+
description: PropTypes.node,
30+
hideLabel: PropTypes.bool
2231
};
2332

2433
export default AntForm;

packages/ant-component-mapper/src/common/is-required.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
22
import { childrenPropTypes } from '@data-driven-forms/common/src/prop-types-templates';
3+
34
import './style.scss';
45

56
const IsRequired = ({ children }) => (
67
<React.Fragment>
7-
<span className="styles" aria-hidden="true">
8+
<span className="ddorg__ant-component-mapper_is-required" aria-hidden="true">
89
*
910
</span>
1011
{children}

packages/ant-component-mapper/src/common/multiple-choice-list.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
33

44
import { Checkbox as AntCheckbox } from 'antd';
55
import MultipleChoiceListCommon, { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
6-
import { validationError } from './helpers';
76
import AntForm from './form-wrapper';
87

98
const FinalCheckbox = ({ isDisabled, label, ...props }) => (
@@ -17,15 +16,19 @@ FinalCheckbox.propTypes = {
1716
label: PropTypes.node
1817
};
1918

20-
const Wrapper = ({ label, isRequired, children, meta, validateOnMount, helperText, description }) => {
21-
const invalid = validationError(meta, validateOnMount);
22-
const help = helperText || description || invalid;
23-
return (
24-
<AntForm label={label} help={help} invalid={invalid} component="fieldset" isRequired={isRequired} layout="vertical">
25-
{children}
26-
</AntForm>
27-
);
28-
};
19+
const Wrapper = ({ label, isRequired, children, meta, validateOnMount, helperText, description, FormItemProps }) => (
20+
<AntForm
21+
label={label}
22+
meta={meta}
23+
validateOnMount={validateOnMount}
24+
helperText={helperText}
25+
description={description}
26+
FormItemProps={FormItemProps}
27+
isRequired={isRequired}
28+
>
29+
{children}
30+
</AntForm>
31+
);
2932

3033
Wrapper.propTypes = {
3134
...wrapperProps
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.styles {
2-
color: #ff4d4f;
3-
margin-right: 4px;
4-
}
1+
.ddorg__ant-component-mapper_is-required {
2+
color: #ff4d4f;
3+
margin-right: 4px;
4+
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ import PropTypes from 'prop-types';
33
import { Checkbox as AntCheckbox } from 'antd';
44
import MultipleChoiceList from '../common/multiple-choice-list';
55
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
6-
import { validationError } from '../common/helpers';
76
import IsRequired from '../common/is-required';
87
import AntForm from '../common/form-wrapper';
98

109
export const SingleCheckbox = (props) => {
11-
const { input, isReadOnly, isDisabled, isRequired, label, helperText, description, validateOnMount, meta } = useFieldApi({
10+
const { input, isReadOnly, isDisabled, isRequired, label, helperText, description, validateOnMount, meta, FormItemProps, ...rest } = useFieldApi({
1211
...props,
1312
type: 'checkbox'
1413
});
15-
const invalid = validationError(meta, validateOnMount);
16-
const text = invalid || helperText || description;
14+
1715
return (
18-
<AntForm component="fieldset" invalid={invalid} help={text}>
16+
<AntForm
17+
label={label}
18+
meta={meta}
19+
validateOnMount={validateOnMount}
20+
helperText={helperText}
21+
description={description}
22+
FormItemProps={FormItemProps}
23+
isRequired={isRequired}
24+
hideLabel
25+
>
1926
<AntCheckbox
2027
checked={input.checked}
2128
{...input}
2229
defaultValue={input.value ? input.value : undefined}
2330
disabled={isDisabled || isReadOnly}
2431
value={input.name}
32+
{...rest}
2533
>
2634
{isRequired ? <IsRequired>{label}</IsRequired> : label}
2735
</AntCheckbox>
@@ -36,7 +44,8 @@ SingleCheckbox.propTypes = {
3644
label: PropTypes.node,
3745
helperText: PropTypes.node,
3846
description: PropTypes.node,
39-
validateOnMount: PropTypes.bool
47+
validateOnMount: PropTypes.bool,
48+
FormItemProps: PropTypes.object
4049
};
4150

4251
const Checkbox = ({ options, ...props }) => (options ? <MultipleChoiceList options={options} {...props} /> : <SingleCheckbox {...props} />);
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { componentTypes } from '@data-driven-forms/react-form-renderer';
2+
23
import Tabs from './tabs';
3-
import React from 'react';
44
import PlainText from './plain-text';
55
import TextField from './text-field';
6-
import TextArea from './text-area';
6+
import Textarea from './textarea';
77
import Checkbox from './checkbox';
88
import DatePicker from './date-picker';
99
import TimePicker from './time-picker';
@@ -12,9 +12,13 @@ import Switch from './switch';
1212
import Select from './select';
1313
import Wizard from './wizard';
1414
import SubForm from './sub-form.js';
15+
import DualListSelect from './dual-list-select.js';
16+
import FieldArray from './field-array.js';
17+
import Slider from './slider.js';
18+
1519
export const components = {
1620
TextField,
17-
TextArea,
21+
Textarea,
1822
Select,
1923
Checkbox,
2024
Radio,
@@ -23,12 +27,15 @@ export const components = {
2327
TimePicker,
2428
PlainText,
2529
SubForm,
26-
Wizard
30+
Wizard,
31+
DualListSelect,
32+
FieldArray,
33+
Slider
2734
};
2835

2936
const componentMapper = {
3037
[componentTypes.TEXT_FIELD]: TextField,
31-
[componentTypes.TEXTAREA]: TextArea,
38+
[componentTypes.TEXTAREA]: Textarea,
3239
[componentTypes.SELECT]: Select,
3340
[componentTypes.CHECKBOX]: Checkbox,
3441
[componentTypes.SUB_FORM]: SubForm,
@@ -39,9 +46,9 @@ const componentMapper = {
3946
[componentTypes.PLAIN_TEXT]: PlainText,
4047
[componentTypes.SWITCH]: Switch,
4148
[componentTypes.WIZARD]: Wizard,
42-
[componentTypes.SLIDER]: () => <div>Not Implemented</div>,
43-
[componentTypes.DUAL_LIST_SELECT]: () => <div>Not Implemented</div>,
44-
[componentTypes.FIELD_ARRAY]: () => <div>Not Implemented</div>
49+
[componentTypes.SLIDER]: Slider,
50+
[componentTypes.DUAL_LIST_SELECT]: DualListSelect,
51+
[componentTypes.FIELD_ARRAY]: FieldArray
4552
};
4653

4754
export default componentMapper;

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,42 @@ import { useFieldApi } from '@data-driven-forms/react-form-renderer';
66
import AntForm from '../common/form-wrapper';
77

88
const DatePicker = (props) => {
9-
const { input, isReadOnly, isDisabled, placeholder, isRequired, label, helperText, description, validateOnMount, meta } = useFieldApi(props);
9+
const {
10+
input,
11+
isReadOnly,
12+
isDisabled,
13+
placeholder,
14+
isRequired,
15+
label,
16+
helperText,
17+
description,
18+
validateOnMount,
19+
meta,
20+
FormItemProps,
21+
...rest
22+
} = useFieldApi(props);
1023
const invalid = validationError(meta, validateOnMount);
11-
const placeholderDisplay = placeholder ? placeholder : 'Select date';
12-
const help = invalid || helperText || description;
24+
1325
return (
14-
<AntForm layout="vertical" isRequired={isRequired} label={label} invalid={invalid} help={help}>
26+
<AntForm
27+
label={label}
28+
meta={meta}
29+
validateOnMount={validateOnMount}
30+
helperText={helperText}
31+
description={description}
32+
FormItemProps={FormItemProps}
33+
isRequired={isRequired}
34+
>
1535
<AntDatePicker
1636
disabled={isDisabled || isReadOnly}
1737
defaultValue={input.value ? input.value : undefined}
18-
placeholder={placeholderDisplay}
38+
placeholder={placeholder}
1939
required={isRequired}
2040
error={!!invalid}
2141
readOnly={isReadOnly}
2242
{...input}
2343
value={input.value || null}
44+
{...rest}
2445
/>
2546
</AntForm>
2647
);
@@ -35,7 +56,12 @@ DatePicker.propTypes = {
3556
helperText: PropTypes.node,
3657
validateOnMount: PropTypes.bool,
3758
locale: PropTypes.string,
38-
description: PropTypes.node
59+
description: PropTypes.node,
60+
FormItemProps: PropTypes.object
61+
};
62+
63+
DatePicker.defaultProps = {
64+
placeholder: 'Select date'
3965
};
4066

4167
export default DatePicker;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
export default () => <div>not implemented</div>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
export default () => <div>not implemented</div>;

0 commit comments

Comments
 (0)