1- import React from 'react' ;
1+ import React , { createContext , useContext } from 'react' ;
22import PropTypes from 'prop-types' ;
33
44import { Grid , Checkbox , FormControlLabel , FormLabel , FormGroup , FormControl , FormHelperText } from '@material-ui/core' ;
55
66import MultipleChoiceListCommon , { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list' ;
77import { 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 = ( { label, isDisabled : _isDisabled , ...rest } ) => {
12+ const {
13+ FormControlLabelProps,
14+ CheckboxProps,
15+ props : { isRequired, isReadOnly, helperText, validate, isDisabled, ...props }
16+ } = useContext ( CheckboxContext ) ;
17+ return (
18+ < FormControlLabel
19+ { ...FormControlLabelProps }
20+ control = {
21+ < Checkbox { ...rest } { ...props } { ...CheckboxProps } disabled = { isDisabled } >
22+ { label }
23+ </ Checkbox >
24+ }
25+ label = { label }
26+ />
27+ ) ;
28+ } ;
1929
2030FinalCheckbox . propTypes = {
2131 isDisabled : PropTypes . bool ,
@@ -24,13 +34,13 @@ FinalCheckbox.propTypes = {
2434
2535const Wrapper = ( { label, isRequired, children, meta, validateOnMount, helperText, description } ) => {
2636 const invalid = validationError ( meta , validateOnMount ) ;
27-
37+ const { FormFieldGridProps , FormControlProps , FormLabelProps , FormGroupProps , FormHelperTextProps } = useContext ( CheckboxContext ) ;
2838 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 > }
39+ < Grid container { ... FormFieldGridProps } >
40+ < FormControl required = { isRequired } error = { ! ! invalid } component = "fieldset" { ... FormControlProps } >
41+ < FormLabel { ... FormLabelProps } > { label } </ FormLabel >
42+ < FormGroup { ... FormGroupProps } > { children } </ FormGroup >
43+ { ( invalid || helperText || description ) && < FormHelperText { ... FormHelperTextProps } > { invalid || helperText || description } </ FormHelperText > }
3444 </ FormControl >
3545 </ Grid >
3646 ) ;
@@ -40,12 +50,43 @@ Wrapper.propTypes = {
4050 ...wrapperProps
4151} ;
4252
43- const MultipleChoiceList = ( props ) => < MultipleChoiceListCommon { ...props } Wrapper = { Wrapper } Checkbox = { FinalCheckbox } /> ;
53+ const MultipleChoiceList = ( {
54+ FormControlProps,
55+ FormLabelProps,
56+ FormGroupProps,
57+ FormHelperTextProps,
58+ FormFieldGridProps,
59+ FormControlLabelProps,
60+ CheckboxProps,
61+ ...props
62+ } ) => (
63+ < CheckboxContext . Provider
64+ value = { { FormControlProps, FormLabelProps, FormGroupProps, FormHelperTextProps, FormFieldGridProps, FormControlLabelProps, CheckboxProps, props } }
65+ >
66+ < MultipleChoiceListCommon { ...props } Wrapper = { Wrapper } Checkbox = { FinalCheckbox } />
67+ </ CheckboxContext . Provider >
68+ ) ;
4469
4570MultipleChoiceList . propTypes = {
4671 input : PropTypes . shape ( {
4772 name : PropTypes . string . isRequired
48- } )
73+ } ) ,
74+ FormFieldGridProps : PropTypes . object ,
75+ FormControlProps : PropTypes . object ,
76+ FormGroupProps : PropTypes . object ,
77+ FormControlLabelProps : PropTypes . object ,
78+ CheckboxProps : PropTypes . object ,
79+ FormLabelProps : PropTypes . object ,
80+ FormHelperTextProps : PropTypes . object
81+ } ;
82+ MultipleChoiceList . defaultProps = {
83+ FormFieldGridProps : { } ,
84+ FormControlProps : { } ,
85+ FormGroupProps : { } ,
86+ FormControlLabelProps : { } ,
87+ CheckboxProps : { } ,
88+ FormLabelProps : { } ,
89+ FormHelperTextProps : { }
4990} ;
5091
5192export default MultipleChoiceList ;
0 commit comments