Skip to content

Commit f4e10fa

Browse files
committed
Convert PF4 multiple choice list to use common
1 parent 552ab49 commit f4e10fa

File tree

1 file changed

+28
-53
lines changed

1 file changed

+28
-53
lines changed
Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,39 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { Checkbox, FormGroup } from '@patternfly/react-core';
4-
import { composeValidators } from '@data-driven-forms/react-form-renderer';
4+
import MultipleChoiceListCommon, { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
55

6-
const propTypes = {
7-
validate: PropTypes.oneOfType([ PropTypes.array, PropTypes.func ]),
8-
FieldProvider: PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]),
9-
name: PropTypes.string.isRequired,
6+
const FinalCheckbox = (props) => (
7+
<Checkbox isChecked={ props.checked } { ...props }/>
8+
);
9+
10+
FinalCheckbox.propTypes = {
11+
checked: PropTypes.bool,
1012
};
1113

12-
const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
13-
<FieldProvider
14+
const Wrapper = ({ showError, isRequired, helperText, label, meta, children, rest }) =>(
15+
<FormGroup
16+
label={ label }
17+
fieldId={ rest.id || rest.key || rest.name }
18+
isValid={ !showError }
19+
isRequired={ isRequired }
20+
helperText={ helperText }
21+
helperTextInvalid={ meta.error }
22+
>
23+
{ children }
24+
</FormGroup>
25+
);
26+
27+
Wrapper.propTypes = {
28+
...wrapperProps,
29+
};
30+
31+
const MultipleChoiceList = (props) => (
32+
<MultipleChoiceListCommon
1433
{ ...props }
15-
validate={ composeValidators(props.validate || []) }
16-
render={ ({
17-
label,
18-
isRequired,
19-
helperText,
20-
meta,
21-
options,
22-
isDisabled,
23-
isReadOnly,
24-
...rest
25-
}) => {
26-
const { error, touched } = meta;
27-
const showError = touched && error;
28-
const groupValues = rest.input.value;
29-
return (
30-
<FormGroup label={ label } fieldId={ rest.id || rest.key || rest.name } isValid={ showError } >
31-
{ options.map(option =>
32-
(<FieldProvider
33-
formOptions={ rest.formOptions }
34-
id={ `${rest.id}-${option.value}` }
35-
key={ option.value }
36-
{ ...option }
37-
name={ props.name }
38-
type="checkbox"
39-
render={ ({ input, meta, formOptions, componentType, ...rest }) => {
40-
const indexValue = groupValues.indexOf(input.value);
41-
return (
42-
<Checkbox
43-
label={ rest.label }
44-
aria-label={ option['aria-label'] || option.label }
45-
isChecked={ input.checked }
46-
{ ...input }
47-
{ ...rest }
48-
isDisabled={ isDisabled || isReadOnly }
49-
onChange={ () => (indexValue === -1
50-
? input.onChange([ ...groupValues, input.value ])
51-
: input.onChange([ ...groupValues.slice(0, indexValue), ...groupValues.slice(indexValue + 1) ])) }
52-
/>
53-
);
54-
} }
55-
/>)) }
56-
</FormGroup>
57-
);
58-
} }
34+
Wrapper={ Wrapper }
35+
Checkbox={ FinalCheckbox }
5936
/>
6037
);
6138

62-
MultipleChoiceList.propTypes = propTypes;
63-
6439
export default MultipleChoiceList;

0 commit comments

Comments
 (0)