Skip to content

Commit a9f4e83

Browse files
authored
Merge pull request #1089 from rvsia/fixCarbonMultipleCheckbox
fix(carbon): fix multiple checkbox by correctly passing event
2 parents f38df55 + 75f35a1 commit a9f4e83

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

packages/carbon-component-mapper/src/checkbox/checkbox.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ const SingleCheckbox = (props) => {
4343
);
4444
};
4545

46-
const SingleCheckboxInCommon = ({ label, isDisabled, id, ...props }) => <CarbonCheckbox id={id} labelText={label} disabled={isDisabled} />;
46+
const SingleCheckboxInCommon = ({ label, isDisabled, id, meta, option: { value, name, ...rest }, onChange, ...props }) => (
47+
<CarbonCheckbox id={id} labelText={label} disabled={isDisabled} {...props} {...rest} onChange={(_value, _name, event) => onChange(event)} />
48+
);
4749

4850
SingleCheckboxInCommon.propTypes = {
4951
label: PropTypes.node,
@@ -52,7 +54,10 @@ SingleCheckboxInCommon.propTypes = {
5254
isRequired: PropTypes.bool,
5355
name: PropTypes.string,
5456
id: PropTypes.string,
55-
WrapperProps: PropTypes.object
57+
WrapperProps: PropTypes.object,
58+
meta: PropTypes.object,
59+
option: PropTypes.object,
60+
onChange: PropTypes.func
5661
};
5762

5863
const Checkbox = ({ options, ...props }) =>

packages/carbon-component-mapper/src/tests/checkbox.test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { act } from 'react-dom/test-utils';
23
import { mount } from 'enzyme';
34

45
import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer';
@@ -47,4 +48,97 @@ describe('<Checkbox />', () => {
4748
.props().labelText
4849
).toEqual('option 2');
4950
});
51+
52+
it('selects item in multiple checkbox', async () => {
53+
const schema = {
54+
fields: [
55+
{
56+
component: componentTypes.CHECKBOX,
57+
name: 'check',
58+
label: 'Please select on of options',
59+
options: [
60+
{
61+
label: 'option 1',
62+
value: 'option-1'
63+
},
64+
{
65+
label: 'option 2',
66+
value: 'option-2'
67+
}
68+
]
69+
}
70+
]
71+
};
72+
const eventCheck = {
73+
target: {
74+
checked: true,
75+
type: 'checkbox'
76+
}
77+
};
78+
const eventUncheck = {
79+
target: {
80+
checked: false,
81+
type: 'checkbox'
82+
}
83+
};
84+
85+
const submitSpy = jest.fn();
86+
87+
const wrapper = mount(
88+
<FormRenderer
89+
onSubmit={(values) => submitSpy(values)}
90+
FormTemplate={(props) => <FormTemplate {...props} />}
91+
schema={schema}
92+
componentMapper={componentMapper}
93+
/>
94+
);
95+
96+
await act(async () => {
97+
wrapper
98+
.find('input')
99+
.first()
100+
.simulate('change', eventCheck);
101+
});
102+
wrapper.update();
103+
104+
await act(async () => {
105+
wrapper.find('form').simulate('submit');
106+
});
107+
wrapper.update();
108+
109+
expect(submitSpy).toHaveBeenCalledWith({ check: ['option-1'] });
110+
submitSpy.mockClear();
111+
112+
await act(async () => {
113+
wrapper
114+
.find('input')
115+
.last()
116+
.simulate('change', eventCheck);
117+
});
118+
wrapper.update();
119+
120+
await act(async () => {
121+
wrapper.find('form').simulate('submit');
122+
});
123+
wrapper.update();
124+
125+
expect(submitSpy).toHaveBeenCalledWith({ check: ['option-1', 'option-2'] });
126+
submitSpy.mockClear();
127+
128+
await act(async () => {
129+
wrapper
130+
.find('input')
131+
.first()
132+
.simulate('change', eventUncheck);
133+
});
134+
wrapper.update();
135+
136+
await act(async () => {
137+
wrapper.find('form').simulate('submit');
138+
});
139+
wrapper.update();
140+
141+
expect(submitSpy).toHaveBeenCalledWith({ check: ['option-2'] });
142+
submitSpy.mockClear();
143+
});
50144
});

0 commit comments

Comments
 (0)