Skip to content

Commit 357ec55

Browse files
committed
Convert MUI multiple choice list to use common
1 parent bc38f99 commit 357ec55

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

packages/common/src/multiple-choice-list.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const MultipleChoiceList = ({ validate, FieldProvider, Wrapper, Checkbox, ...pro
3131
meta={ meta }
3232
description={ description }
3333
rest={ rest }
34+
error={ error }
3435
>
3536
{ options.map(option =>
3637
(<FieldProvider
Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { composeValidators } from '@data-driven-forms/react-form-renderer';
2+
import PropTypes from 'prop-types';
3+
34
import Grid from '@material-ui/core/Grid';
45
import Checkbox from '@material-ui/core/Checkbox';
56
import FormControlLabel from '@material-ui/core/FormControlLabel';
@@ -8,65 +9,47 @@ import FormGroup from '@material-ui/core/FormGroup';
89
import FormControl from '@material-ui/core/FormControl';
910
import FormHelperText from '@material-ui/core/FormHelperText';
1011

11-
const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
12-
<FieldProvider { ...props } validate={ composeValidators(props.validate || []) }>
13-
{ ({
14-
label,
15-
isRequired,
16-
helperText,
17-
meta,
18-
options,
19-
isDisabled,
20-
formOptions,
21-
componentType,
22-
...rest
23-
}) => {
24-
const { error, touched } = meta;
25-
const showError = touched && error;
26-
const groupValues = Array.isArray(rest.input.value) ? rest.input.value : [];
27-
return (
28-
<Grid container >
29-
<FormControl component="fieldset" >
30-
<FormLabel>{ label }</FormLabel>
31-
<FormGroup>
32-
{ options.map(option =>
33-
(<FieldProvider
34-
{ ...rest }
35-
id={ `${rest.id}-${option.value}` }
36-
key={ option.value }
37-
{ ...option }
38-
name={ props.name }
39-
type="checkbox"
40-
render={ ({ input, meta, value, formOptions, ...rest }) => {
41-
const indexValue = groupValues.indexOf(option.value);
42-
return (
43-
<FormControlLabel
44-
control={ <Checkbox
45-
label={ rest.label }
46-
aria-label={ option['aria-label'] || option.label }
47-
{ ...input }
48-
{ ...rest }
49-
checked={ indexValue !== -1 }
50-
disabled={ isDisabled }
51-
onChange={ () => {
52-
return (indexValue === -1
53-
? input.onChange([ ...groupValues, option.value ])
54-
: input.onChange([ ...groupValues.slice(0, indexValue), ...groupValues.slice(indexValue + 1) ]));} }
55-
>
56-
{ option.label }
57-
</Checkbox> }
58-
label={ option.label }
59-
/>
60-
);
61-
} }
62-
/>)) }
63-
</FormGroup>
64-
<FormHelperText>{ showError ? error : null }</FormHelperText>
65-
</FormControl>
66-
</Grid>
67-
);
68-
} }
69-
</FieldProvider>
12+
import MultipleChoiceListCommon, { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
13+
14+
const FinalCheckbox = (props) => (
15+
<FormControlLabel
16+
control={ <Checkbox
17+
{ ...props }
18+
disabled={ props.isDisabled }
19+
>
20+
{ props.label }
21+
</Checkbox> }
22+
label={ props.label }
23+
/>
24+
);
25+
26+
FinalCheckbox.propTypes = {
27+
isDisabled: PropTypes.bool,
28+
label: PropTypes.node,
29+
};
30+
31+
const Wrapper = ({ showError, label, error, children }) =>(
32+
<Grid container >
33+
<FormControl component="fieldset" >
34+
<FormLabel>{ label }</FormLabel>
35+
<FormGroup>
36+
{ children }
37+
</FormGroup>
38+
<FormHelperText>{ showError ? error : null }</FormHelperText>
39+
</FormControl>
40+
</Grid>
41+
);
42+
43+
Wrapper.propTypes = {
44+
...wrapperProps,
45+
};
46+
47+
const MultipleChoiceList = (props) => (
48+
<MultipleChoiceListCommon
49+
{ ...props }
50+
Wrapper={ Wrapper }
51+
Checkbox={ FinalCheckbox }
52+
/>
7053
);
7154

7255
export default MultipleChoiceList;

0 commit comments

Comments
 (0)