Skip to content

Commit b56d64e

Browse files
committed
fix(pf3 multiple checkboxes): allow helperText and description
1 parent 6c4aacd commit b56d64e

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

packages/pf3-component-mapper/demo/demo-schemas/sandbox.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ const output = {
1212
description: 'Text boxes and text areas',
1313
name: '553',
1414
fields: [
15+
{
16+
name: 'check_box_1',
17+
label: 'Check Box',
18+
title: 'Check Box',
19+
helperText: 'aaaa',
20+
description: 'description',
21+
component: components.CHECKBOX,
22+
options: [
23+
{
24+
label: 'Dog',
25+
value: '1',
26+
},
27+
{
28+
label: 'Cats',
29+
value: '2',
30+
},
31+
{
32+
label: 'Hamsters',
33+
value: '3',
34+
},
35+
],
36+
},
1537
{
1638
component: 'select-field',
1739
name: 'select-field-1',

packages/pf3-component-mapper/src/form-fields/form-fields.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const selectComponent = ({
7070
[componentTypes.DATE_PICKER]: () => <DateTimePicker pristine={ meta.pristine } onChange={ input.onChange } value={ input.value } isDisabled={ isDisabled } { ...rest } />,
7171
})[componentType];
7272

73-
const renderHelperText = (error, description) => (error // eslint-disable-line no-nested-ternary
73+
export const renderHelperText = (error, description) => (error // eslint-disable-line no-nested-ternary
7474
? <HelpBlock>{ error }</HelpBlock>
7575
: description ? <HelpBlock>{ description }</HelpBlock> : null);
7676

packages/pf3-component-mapper/src/form-fields/multiple-choice-list.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { Checkbox, FormGroup, ControlLabel } from 'patternfly-react';
3+
import { Checkbox, FormGroup, ControlLabel, FieldLevelHelp } from 'patternfly-react';
44
import { composeValidators } from '@data-driven-forms/react-form-renderer';
55

66
import RequiredLabel from './required-label';
7+
import { renderHelperText } from './form-fields';
78

89
const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
910
<FieldProvider { ...props } validate={ composeValidators(props.validate || []) }>
@@ -15,6 +16,7 @@ const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
1516
options,
1617
isDisabled,
1718
isReadOnly,
19+
description,
1820
...rest
1921
}) => {
2022
const { error, touched } = meta;
@@ -24,6 +26,7 @@ const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
2426
<FormGroup validationState={ showError ? 'error' : null }>
2527
<ControlLabel>
2628
{ (isRequired ? <RequiredLabel label={ label } /> : label) }
29+
{ helperText && <FieldLevelHelp content={ helperText } /> }
2730
</ControlLabel>
2831
<div>
2932
{ options.map(option =>
@@ -52,6 +55,7 @@ const MultipleChoiceList = ({ validate, FieldProvider, ...props }) => (
5255
} }
5356
/>)) }
5457
</div>
58+
{ renderHelperText(showError && meta.error, description) }
5559
</FormGroup>
5660
);
5761
} }

packages/pf3-component-mapper/src/tests/__snapshots__/multiple-choice-list.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ exports[`<MultipleChoiceList /> should render in error state 1`] = `
379379
</MockFieldProvider>
380380
</FieldProvider>
381381
</div>
382+
<HelpBlock
383+
bsClass="help-block"
384+
>
385+
<span
386+
className="help-block"
387+
>
388+
Error message
389+
</span>
390+
</HelpBlock>
382391
</div>
383392
</FormGroup>
384393
</MockFieldProvider>

packages/pf3-component-mapper/src/tests/multiple-choice-list.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import toJson from 'enzyme-to-json';
44
import MultipleChoiceList from '../form-fields/multiple-choice-list';
55
import MockFieldProvider from '../../../../__mocks__/mock-field-provider';
66
import RequiredLabel from '../form-fields/required-label';
7+
import { FieldLevelHelp, HelpBlock } from 'patternfly-react';
78

89
describe('<MultipleChoiceList />', () => {
910
let initialProps;
@@ -71,4 +72,19 @@ describe('<MultipleChoiceList />', () => {
7172
const wrapper = mount(<MultipleChoiceList { ...initialProps } isRequired label="Foo" />);
7273
expect(wrapper.find(RequiredLabel)).toHaveLength(1);
7374
});
75+
76+
it('should render helper text variant', () => {
77+
const wrapper = mount(<MultipleChoiceList { ...initialProps } isRequired label="Foo" helperText="Helper text"/>);
78+
expect(wrapper.find(FieldLevelHelp)).toHaveLength(1);
79+
80+
expect(wrapper.find(FieldLevelHelp).first().props().content).toEqual('Helper text');
81+
expect(wrapper.find(HelpBlock)).toHaveLength(0);
82+
});
83+
84+
it('should render description variant', () => {
85+
const wrapper = mount(<MultipleChoiceList { ...initialProps } isRequired label="Foo" description="Description"/>);
86+
expect(wrapper.find(FieldLevelHelp)).toHaveLength(0);
87+
expect(wrapper.find(HelpBlock)).toHaveLength(1);
88+
expect(wrapper.find(HelpBlock).first().text()).toEqual('Description');
89+
});
7490
});

0 commit comments

Comments
 (0)