Skip to content

Commit 41bcd6e

Browse files
committed
fix(carbon): use common onChange event
1 parent d0c9263 commit 41bcd6e

File tree

2 files changed

+5
-34
lines changed

2 files changed

+5
-34
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import fnToString from '@data-driven-forms/common/utils/fn-to-string';
88
import { Select as CarbonSelect, MultiSelect, SelectItem, ComboBox } from 'carbon-components-react';
99
import prepareProps from '../prepare-props';
1010

11-
export const multiOnChange = (input, simpleValue) => ({ selectedItem, selectedItems }) => {
12-
if (simpleValue) {
13-
return input.onChange(selectedItems?.map(({ value }) => value) || selectedItem.value);
14-
} else {
15-
return input.onChange(selectedItems || selectedItem);
16-
}
17-
};
11+
const onChangeWrapper = (onChange) => ({ selectedItem, selectedItems }) => onChange(selectedItems || selectedItem);
1812

1913
export const getMultiValue = (value, options) =>
2014
(Array.isArray(value) ? value : value ? [value] : []).map((item) =>
@@ -43,7 +37,7 @@ const ClearedMultiSelectFilterable = ({
4337
disabled={isDisabled}
4438
{...rest}
4539
placeholder={carbonLabel || placeholder}
46-
onChange={originalOnChange}
40+
onChange={onChangeWrapper(onChange)}
4741
titleText={rest.labelText}
4842
id={rest.name}
4943
invalid={Boolean(invalidText)}
@@ -94,7 +88,7 @@ const ClearedMultiSelect = ({
9488
disabled={isDisabled}
9589
{...rest}
9690
label={carbonLabel || placeholder}
97-
onChange={originalOnChange}
91+
onChange={onChangeWrapper(onChange)}
9892
titleText={rest.labelText}
9993
id={rest.name}
10094
invalid={Boolean(invalidText)}
@@ -206,6 +200,7 @@ const ClearedSelectSearchable = ({
206200
originalOnChange,
207201
placeholder,
208202
labelText,
203+
onChange,
209204
...rest
210205
}) => (
211206
<ComboBox
@@ -218,7 +213,7 @@ const ClearedSelectSearchable = ({
218213
items={options}
219214
placeholder={placeholder}
220215
titleText={labelText}
221-
onChange={originalOnChange}
216+
onChange={onChangeWrapper(onChange)}
222217
/>
223218
);
224219

@@ -273,7 +268,6 @@ const Select = (props) => {
273268
loadOptions={loadOptions}
274269
invalidText={invalidText}
275270
loadOptionsChangeCounter={loadOptionsChangeCounter}
276-
originalOnChange={multiOnChange(input, rest.simpleValue)}
277271
helperText={text}
278272
/>
279273
);

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-rend
66
import FormTemplate from '../form-template';
77
import componentMapper from '../component-mapper';
88
import { Select, MultiSelect, ComboBox } from 'carbon-components-react';
9-
import { multiOnChange } from '../select';
109
import { getMultiValue } from '../select/select';
1110

1211
describe('<Select />', () => {
@@ -173,28 +172,6 @@ describe('<Select />', () => {
173172
});
174173
});
175174

176-
describe('multichange', () => {
177-
const input = {
178-
onChange: jest.fn()
179-
};
180-
181-
beforeEach(() => {
182-
input.onChange.mockReset();
183-
});
184-
185-
it('simpleValue', () => {
186-
multiOnChange(input, true)({ selectedItems: [{ value: '123' }, { value: '345' }] });
187-
188-
expect(input.onChange).toHaveBeenCalledWith(['123', '345']);
189-
});
190-
191-
it('not simple value', () => {
192-
multiOnChange(input, false)({ selectedItems: [{ value: '123' }, { value: '345' }] });
193-
194-
expect(input.onChange).toHaveBeenCalledWith([{ value: '123' }, { value: '345' }]);
195-
});
196-
});
197-
198175
describe('getMultiValue', () => {
199176
let value;
200177
let options;

0 commit comments

Comments
 (0)