Skip to content

Commit a88bdf5

Browse files
committed
Added switch.js
1 parent 8daa397 commit a88bdf5

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Checkbox from './checkbox';
88
import DatePicker from './date-picker';
99
import TimePicker from './time-picker';
1010
import Radio from './radio';
11+
import Switch from './switch';
1112
import Select from './select';
1213
export const components = {};
1314

@@ -22,7 +23,7 @@ const componentMapper = {
2223
[componentTypes.DATE_PICKER]: DatePicker,
2324
[componentTypes.TIME_PICKER]: TimePicker,
2425
[componentTypes.PLAIN_TEXT]: PlainText,
25-
[componentTypes.SWITCH]: () => <div>switch</div>
26+
[componentTypes.SWITCH]: Switch
2627
};
2728

2829
export default componentMapper;

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ import FormTemplate from '@data-driven-forms/common/src/form-template';
66

77
const { Title, Paragraph } = Typography;
88

9-
const AntButton = ({ children, ...props }) => {
10-
return (
11-
<Button {...props} type="primary" htmlType="submit">
12-
{children}
13-
</Button>
14-
);
15-
};
16-
179
const Form = ({ children, ...props }) => <form {...props}>{children}</form>;
1810
const Description = ({ children }) => (
1911
<Typography>
@@ -27,7 +19,11 @@ const TitleComponent = ({ children }) => (
2719
);
2820

2921
const ButtonGroup = ({ children }) => <div style={{ display: 'flex', justifyContent: 'flex-end' }}>{children}</div>;
30-
const ButtonComponent = ({ label, variant, children, ...props }) => <AntButton {...props}>{label || children}</AntButton>;
22+
const ButtonComponent = ({ label, variant, children, buttonType, ...props }) => (
23+
<Button {...props} type="primary" htmlType={props.type}>
24+
{label || children}
25+
</Button>
26+
);
3127

3228
const AntFormTemplate = (props) => (
3329
<FormTemplate FormWrapper={Form} Button={ButtonComponent} ButtonGroup={ButtonGroup} Title={TitleComponent} Description={Description} {...props} />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Switch as AntSwitch } from 'antd';
4+
import { meta, input } from '@data-driven-forms/common/src/prop-types-templates';
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+
export const Switch = (props) => {
10+
const { input, isReadOnly, isDisabled, isRequired, label, helperText, description, validateOnMount, meta, onText, offText, ...rest } = useFieldApi({
11+
...props,
12+
type: 'checkbox'
13+
});
14+
const invalid = validationError(meta, validateOnMount);
15+
const text = invalid || helperText || description;
16+
const { name, value, onChange, onBlur } = input;
17+
return (
18+
<AntForm help={text} invalid={invalid} isRequired={isRequired} error={!!invalid} label={input.checked ? onText || label : offText || label}>
19+
<AntSwitch {...rest} onChange={onChange} value={value} name={name} onClick={onBlur} disabled={isDisabled || isReadOnly} />
20+
</AntForm>
21+
);
22+
};
23+
24+
Switch.propTypes = {
25+
input,
26+
meta,
27+
isReadOnly: PropTypes.bool,
28+
isDisabled: PropTypes.bool,
29+
isRequired: PropTypes.bool,
30+
label: PropTypes.node,
31+
helperText: PropTypes.node,
32+
validateOnMount: PropTypes.bool,
33+
onText: PropTypes.node,
34+
offText: PropTypes.node,
35+
description: PropTypes.node
36+
};
37+
38+
export default Switch;

0 commit comments

Comments
 (0)