Skip to content

Commit b22924c

Browse files
committed
feat(mui): allow passing props to composite components.
1 parent 56e04e0 commit b22924c

File tree

13 files changed

+306
-80
lines changed

13 files changed

+306
-80
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
import React from "react";
33
import ReactDOM from "react-dom";
4-
import FormRenderer from '@data-driven-forms/react-form-renderer';
4+
import FormRenderer, { componentTypes } from '@data-driven-forms/react-form-renderer';
55

66
import Grid from '@material-ui/core/Grid';
77
import { componentMapper, FormTemplate } from '../src'
@@ -15,6 +15,16 @@ const theme = createMuiTheme({
1515
useNextVariants: true,
1616
},
1717
});
18+
19+
const compositeMapper = {
20+
...componentMapper,
21+
[componentTypes.SWITCH]: {
22+
component: componentMapper[componentTypes.SWITCH],
23+
FormControlLabelProps: {
24+
labelPlacement: 'left'
25+
}
26+
}
27+
}
1828

1929

2030
const App = () => (
@@ -31,7 +41,7 @@ const App = () => (
3141
<Grid item xs={12}>
3242
<FormRenderer
3343
onSubmit={console.log}
34-
componentMapper={componentMapper}
44+
componentMapper={compositeMapper}
3545
FormTemplate={props => <FormTemplate {...props} />}
3646
schema={demoSchema}
3747
onCancel={() => console.log('canceling')}

packages/mui-component-mapper/src/common/form-field-grid.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import PropTypes from 'prop-types';
33

44
import { Grid } from '@material-ui/core';
55
import { makeStyles } from '@material-ui/core/styles';
6+
import clsx from 'clsx';
67

78
const useFinalFormFieldStyles = makeStyles({
89
grid: {
910
position: 'relative'
1011
}
1112
});
1213

13-
const FormFieldGrid = ({ children, ...props }) => {
14+
const FormFieldGrid = ({ children, className, ...props }) => {
1415
const classes = useFinalFormFieldStyles();
1516

1617
return (
17-
<Grid xs={12} item style={{ marginBottom: 16, padding: 0 }} className={classes.grid} {...props}>
18+
<Grid xs={12} item className={clsx(classes.grid, className)} {...props}>
1819
{children}
1920
</Grid>
2021
);
2122
};
2223

2324
FormFieldGrid.propTypes = {
24-
children: PropTypes.node
25+
children: PropTypes.node,
26+
className: PropTypes.string
2527
};
2628

2729
export default FormFieldGrid;
Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import React from 'react';
1+
import React, { createContext, useContext } from 'react';
22
import PropTypes from 'prop-types';
33

44
import { Grid, Checkbox, FormControlLabel, FormLabel, FormGroup, FormControl, FormHelperText } from '@material-ui/core';
55

66
import MultipleChoiceListCommon, { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
77
import { validationError } from './helpers';
88

9-
const FinalCheckbox = ({ isDisabled, label, ...props }) => (
10-
<FormControlLabel
11-
control={
12-
<Checkbox {...props} disabled={isDisabled}>
13-
{label}
14-
</Checkbox>
15-
}
16-
label={label}
17-
/>
18-
);
9+
const CheckboxContext = createContext({});
10+
11+
const FinalCheckbox = ({ isDisabled, label, ...props }) => {
12+
const { FormControlLabelProps, CheckboxProps } = useContext(CheckboxContext);
13+
return (
14+
<FormControlLabel
15+
{...FormControlLabelProps}
16+
control={
17+
<Checkbox {...props} {...CheckboxProps} disabled={isDisabled}>
18+
{label}
19+
</Checkbox>
20+
}
21+
label={label}
22+
/>
23+
);
24+
};
1925

2026
FinalCheckbox.propTypes = {
2127
isDisabled: PropTypes.bool,
@@ -24,13 +30,13 @@ FinalCheckbox.propTypes = {
2430

2531
const Wrapper = ({ label, isRequired, children, meta, validateOnMount, helperText, description }) => {
2632
const invalid = validationError(meta, validateOnMount);
27-
33+
const { FormFieldGridProps, FormControlProps, FormLabelProps, FormGroupProps, FormHelperTextProps } = useContext(CheckboxContext);
2834
return (
29-
<Grid container>
30-
<FormControl required={isRequired} error={!!invalid} component="fieldset">
31-
<FormLabel>{label}</FormLabel>
32-
<FormGroup>{children}</FormGroup>
33-
{(invalid || helperText || description) && <FormHelperText>{invalid || helperText || description}</FormHelperText>}
35+
<Grid container {...FormFieldGridProps}>
36+
<FormControl required={isRequired} error={!!invalid} component="fieldset" {...FormControlProps}>
37+
<FormLabel {...FormLabelProps}>{label}</FormLabel>
38+
<FormGroup {...FormGroupProps}>{children}</FormGroup>
39+
{(invalid || helperText || description) && <FormHelperText {...FormHelperTextProps}>{invalid || helperText || description}</FormHelperText>}
3440
</FormControl>
3541
</Grid>
3642
);
@@ -40,12 +46,43 @@ Wrapper.propTypes = {
4046
...wrapperProps
4147
};
4248

43-
const MultipleChoiceList = (props) => <MultipleChoiceListCommon {...props} Wrapper={Wrapper} Checkbox={FinalCheckbox} />;
49+
const MultipleChoiceList = ({
50+
FormControlProps,
51+
FormLabelProps,
52+
FormGroupProps,
53+
FormHelperTextProps,
54+
FormFieldGridProps,
55+
FormControlLabelProps,
56+
CheckboxProps,
57+
...props
58+
}) => (
59+
<CheckboxContext.Provider
60+
value={{ FormControlProps, FormLabelProps, FormGroupProps, FormHelperTextProps, FormFieldGridProps, FormControlLabelProps, CheckboxProps }}
61+
>
62+
<MultipleChoiceListCommon {...props} Wrapper={Wrapper} Checkbox={FinalCheckbox} />
63+
</CheckboxContext.Provider>
64+
);
4465

4566
MultipleChoiceList.propTypes = {
4667
input: PropTypes.shape({
4768
name: PropTypes.string.isRequired
48-
})
69+
}),
70+
FormFieldGridProps: PropTypes.object,
71+
FormControlProps: PropTypes.object,
72+
FormGroupProps: PropTypes.object,
73+
FormControlLabelProps: PropTypes.object,
74+
CheckboxProps: PropTypes.object,
75+
FormLabelProps: PropTypes.object,
76+
FormHelperTextProps: PropTypes.object
77+
};
78+
MultipleChoiceList.defaultProps = {
79+
FormFieldGridProps: {},
80+
FormControlProps: {},
81+
FormGroupProps: {},
82+
FormControlLabelProps: {},
83+
CheckboxProps: {},
84+
FormLabelProps: {},
85+
FormHelperTextProps: {}
4986
};
5087

5188
export default MultipleChoiceList;

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,40 @@ import MultipleChoiceList from '../common/multiple-choice-list';
99
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
1010

1111
export const SingleCheckbox = (props) => {
12-
const { input, isReadOnly, isDisabled, isRequired, label, helperText, description, validateOnMount, meta } = useFieldApi({
12+
const {
13+
input,
14+
isReadOnly,
15+
isDisabled,
16+
isRequired,
17+
label,
18+
helperText,
19+
description,
20+
validateOnMount,
21+
meta,
22+
FormFieldGridProps,
23+
FormControlProps,
24+
FormGroupProps,
25+
FormControlLabelProps,
26+
CheckboxProps,
27+
FormLabelProps,
28+
FormHelperTextProps
29+
} = useFieldApi({
1330
...props,
1431
type: 'checkbox'
1532
});
1633
const invalid = validationError(meta, validateOnMount);
1734
const text = invalid || helperText || description;
1835

1936
return (
20-
<FormFieldGrid>
21-
<FormControl required={isRequired} error={!!invalid} component="fieldset">
22-
<FormGroup>
37+
<FormFieldGrid {...FormFieldGridProps}>
38+
<FormControl required={isRequired} error={!!invalid} component="fieldset" {...FormControlProps}>
39+
<FormGroup {...FormGroupProps}>
2340
<FormControlLabel
41+
{...FormControlLabelProps}
2442
control={
2543
<MUICheckbox
2644
{...input}
45+
{...CheckboxProps}
2746
disabled={isDisabled || isReadOnly}
2847
value={input.name}
2948
inputProps={{
@@ -32,9 +51,9 @@ export const SingleCheckbox = (props) => {
3251
/>
3352
}
3453
disabled={isDisabled || isReadOnly}
35-
label={<FormLabel>{label}</FormLabel>}
54+
label={<FormLabel {...FormLabelProps}>{label}</FormLabel>}
3655
/>
37-
{(invalid || text) && <FormHelperText>{invalid || text}</FormHelperText>}
56+
{(invalid || text) && <FormHelperText {...FormHelperTextProps}>{invalid || text}</FormHelperText>}
3857
</FormGroup>
3958
</FormControl>
4059
</FormFieldGrid>
@@ -50,7 +69,24 @@ SingleCheckbox.propTypes = {
5069
label: PropTypes.node,
5170
helperText: PropTypes.node,
5271
description: PropTypes.node,
53-
validateOnMount: PropTypes.bool
72+
validateOnMount: PropTypes.bool,
73+
FormFieldGridProps: PropTypes.object,
74+
FormControlProps: PropTypes.object,
75+
FormGroupProps: PropTypes.object,
76+
FormControlLabelProps: PropTypes.object,
77+
CheckboxProps: PropTypes.object,
78+
FormLabelProps: PropTypes.object,
79+
FormHelperTextProps: PropTypes.object
80+
};
81+
82+
SingleCheckbox.defaultProps = {
83+
FormFieldGridProps: {},
84+
FormControlProps: {},
85+
FormGroupProps: {},
86+
FormControlLabelProps: {},
87+
CheckboxProps: {},
88+
FormLabelProps: {},
89+
FormHelperTextProps: {}
5490
};
5591

5692
const Checkbox = ({ options, ...props }) => (options ? <MultipleChoiceList options={options} {...props} /> : <SingleCheckbox {...props} />);

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ const DatePicker = (props) => {
2020
description,
2121
validateOnMount,
2222
meta,
23-
locale = 'en'
23+
locale = 'en',
24+
FormFieldGridProps,
25+
MuiPickersUtilsProviderProps,
26+
DatePickerProps
2427
} = useFieldApi(props);
2528
const invalid = validationError(meta, validateOnMount);
2629

2730
return (
28-
<FormFieldGrid>
29-
<MuiPickersUtilsProvider locale={locale} utils={MomentUtils}>
31+
<FormFieldGrid {...FormFieldGridProps}>
32+
<MuiPickersUtilsProvider locale={locale} utils={MomentUtils} {...MuiPickersUtilsProviderProps}>
3033
<MUIDatePicker
3134
fullWidth
3235
margin="normal"
@@ -39,6 +42,7 @@ const DatePicker = (props) => {
3942
readOnly={isReadOnly}
4043
{...input}
4144
value={input.value || null}
45+
{...DatePickerProps}
4246
/>
4347
</MuiPickersUtilsProvider>
4448
</FormFieldGrid>
@@ -56,7 +60,16 @@ DatePicker.propTypes = {
5660
helperText: PropTypes.node,
5761
validateOnMount: PropTypes.bool,
5862
locale: PropTypes.string,
59-
description: PropTypes.node
63+
description: PropTypes.node,
64+
FormFieldGridProps: PropTypes.object,
65+
MuiPickersUtilsProviderProps: PropTypes.object,
66+
DatePickerProps: PropTypes.object
67+
};
68+
69+
DatePicker.defaultProps = {
70+
FormFieldGridProps: {},
71+
MuiPickersUtilsProviderProps: {},
72+
DatePickerProps: {}
6073
};
6174

6275
export default DatePicker;

0 commit comments

Comments
 (0)