|
1 | 1 | import React from 'react';
|
2 | 2 | import PropTypes from 'prop-types';
|
3 | 3 | import { Checkbox, FormGroup, ControlLabel, FieldLevelHelp } from 'patternfly-react';
|
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 | 6 | import RequiredLabel from './required-label';
|
7 | 7 | import { renderHelperText } from './form-fields';
|
8 | 8 |
|
9 |
| -const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => ( |
10 |
| - <FieldProvider { ...props } validate={ composeValidators(props.validate || []) }> |
11 |
| - { ({ |
12 |
| - label, |
13 |
| - isRequired, |
14 |
| - helperText, |
15 |
| - meta, |
16 |
| - options, |
17 |
| - isDisabled, |
18 |
| - isReadOnly, |
19 |
| - description, |
20 |
| - ...rest |
21 |
| - }) => { |
22 |
| - const { error, touched } = meta; |
23 |
| - const showError = touched && error; |
24 |
| - const groupValues = rest.input.value; |
25 |
| - return ( |
26 |
| - <FormGroup validationState={ showError ? 'error' : null }> |
27 |
| - <ControlLabel> |
28 |
| - { (isRequired ? <RequiredLabel label={ label } /> : label) } |
29 |
| - { helperText && <FieldLevelHelp content={ helperText } /> } |
30 |
| - </ControlLabel> |
31 |
| - <div> |
32 |
| - { options.map(option => |
33 |
| - (<FieldProvider |
34 |
| - formOptions={ rest.formOptions } |
35 |
| - id={ `${rest.id}-${option.value}` } |
36 |
| - key={ option.value } |
37 |
| - { ...option } |
38 |
| - name={ props.name } |
39 |
| - type="checkbox" |
40 |
| - render={ ({ input, meta, formOptions, componentType, ...rest }) => { |
41 |
| - const indexValue = groupValues.indexOf(input.value); |
42 |
| - return ( |
43 |
| - <Checkbox |
44 |
| - aria-label={ option['aria-label'] || option.label } |
45 |
| - { ...input } |
46 |
| - { ...rest } |
47 |
| - disabled={ isDisabled || isReadOnly } |
48 |
| - onChange={ () => (indexValue === -1 |
49 |
| - ? input.onChange([ ...groupValues, input.value ]) |
50 |
| - : input.onChange([ ...groupValues.slice(0, indexValue), ...groupValues.slice(indexValue + 1) ])) } |
51 |
| - > |
52 |
| - { rest.label } |
53 |
| - </Checkbox> |
54 |
| - ); |
55 |
| - } } |
56 |
| - />)) } |
57 |
| - </div> |
58 |
| - { renderHelperText(showError && meta.error, description) } |
59 |
| - </FormGroup> |
60 |
| - ); |
61 |
| - } } |
62 |
| - </FieldProvider> |
| 9 | +const FinalCheckbox = ({ label, isDisabled, ...props }) => ( |
| 10 | + <Checkbox { ...props } disabled={ isDisabled }>{ label }</Checkbox> |
63 | 11 | );
|
64 | 12 |
|
65 |
| -MultipleChoiceList.propTypes = { |
66 |
| - validate: PropTypes.func, |
67 |
| - FieldProvider: PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]), |
68 |
| - name: PropTypes.string.isRequired, |
| 13 | +FinalCheckbox.propTypes = { |
| 14 | + label: PropTypes.node, |
| 15 | + isDisabled: PropTypes.bool, |
69 | 16 | };
|
70 | 17 |
|
| 18 | +const Wrapper = ({ showError, isRequired, helperText, label, meta, description, children }) => ( |
| 19 | + <FormGroup validationState={ showError ? 'error' : null }> |
| 20 | + <ControlLabel> |
| 21 | + { isRequired ? <RequiredLabel label={ label } /> : label } |
| 22 | + { helperText && <FieldLevelHelp content={ helperText } /> } |
| 23 | + </ControlLabel> |
| 24 | + <div> |
| 25 | + { children } |
| 26 | + </div> |
| 27 | + { renderHelperText(showError && meta.error, description) } |
| 28 | + </FormGroup> |
| 29 | +); |
| 30 | + |
| 31 | +Wrapper.propTypes = { |
| 32 | + ...wrapperProps, |
| 33 | +}; |
| 34 | + |
| 35 | +const MultipleChoiceList = (props) => ( |
| 36 | + <MultipleChoiceListCommon |
| 37 | + { ...props } |
| 38 | + Wrapper={ Wrapper } |
| 39 | + Checkbox={ FinalCheckbox } |
| 40 | + /> |
| 41 | +); |
| 42 | + |
71 | 43 | export default MultipleChoiceList;
|
0 commit comments