|
1 | 1 | import React from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
3 | 3 | 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'; |
5 | 5 |
|
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, |
10 | 12 | }; |
11 | 13 |
|
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 |
14 | 33 | { ...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 } |
59 | 36 | /> |
60 | 37 | ); |
61 | 38 |
|
62 | | -MultipleChoiceList.propTypes = propTypes; |
63 | | - |
64 | 39 | export default MultipleChoiceList; |
0 commit comments