Skip to content

Commit 0e5d071

Browse files
committed
fix(carbon): fix empty initial value in multi select
1 parent a0484d4 commit 0e5d071

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export const multiOnChange = (input, simpleValue) => ({ selectedItem, selectedIt
1616
}
1717
};
1818

19-
const getMultiValue = (value, options) =>
20-
(Array.isArray(value) ? value : [value]).map((item) => (typeof item === 'object' ? item : options.find(({ value }) => value === item))) || [];
19+
export const getMultiValue = (value, options) =>
20+
(Array.isArray(value) ? value : value ? [value] : []).map((item) =>
21+
typeof item === 'object' ? item : options.find(({ value }) => value === item)
22+
);
2123

2224
const ClearedMultiSelectFilterable = ({
2325
invalidText,

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import FormTemplate from '../form-template';
77
import componentMapper from '../component-mapper';
88
import { Select, MultiSelect, ComboBox } from 'carbon-components-react';
99
import { multiOnChange } from '../select';
10+
import { getMultiValue } from '../select/select';
1011

1112
describe('<Select />', () => {
1213
it('renders select', () => {
@@ -193,4 +194,38 @@ describe('<Select />', () => {
193194
expect(input.onChange).toHaveBeenCalledWith([{ value: '123' }, { value: '345' }]);
194195
});
195196
});
197+
198+
describe('getMultiValue', () => {
199+
let value;
200+
let options;
201+
202+
beforeEach(() => {
203+
value = undefined;
204+
options = [];
205+
});
206+
207+
it('undefined', () => {
208+
expect(getMultiValue(value, options)).toEqual([]);
209+
});
210+
211+
it('array', () => {
212+
value = ['dogs'];
213+
options = [
214+
{ label: 'cats', value: 'cats' },
215+
{ label: 'dogs', value: 'dogs' }
216+
];
217+
218+
expect(getMultiValue(value, options)).toEqual([{ label: 'dogs', value: 'dogs' }]);
219+
});
220+
221+
it('single', () => {
222+
value = 'dogs';
223+
options = [
224+
{ label: 'cats', value: 'cats' },
225+
{ label: 'dogs', value: 'dogs' }
226+
];
227+
228+
expect(getMultiValue(value, options)).toEqual([{ label: 'dogs', value: 'dogs' }]);
229+
});
230+
});
196231
});

0 commit comments

Comments
 (0)