Skip to content

Commit 5f05031

Browse files
committed
fix(carbon): combobox initial value label
1 parent ed278b9 commit 5f05031

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import prepareProps from '../prepare-props';
1010

1111
export const multiOnChange = (input, simpleValue) => ({ selectedItem, selectedItems }) => {
1212
if (simpleValue) {
13-
return input.onChange(selectedItems.map(({ value }) => value) || selectedItem.value);
13+
return input.onChange(selectedItems?.map(({ value }) => value) || selectedItem.value);
1414
} else {
1515
return input.onChange(selectedItems || selectedItem);
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))) || [];
21+
1922
const ClearedMultiSelectFilterable = ({
2023
invalidText,
2124
hideSelectedOptions,
@@ -44,7 +47,7 @@ const ClearedMultiSelectFilterable = ({
4447
invalid={Boolean(invalidText)}
4548
invalidText={invalidText}
4649
items={options}
47-
initialSelectedItems={Array.isArray(rest.value) ? rest.value : []}
50+
initialSelectedItems={getMultiValue(rest.value, options)}
4851
/>
4952
);
5053

@@ -95,7 +98,7 @@ const ClearedMultiSelect = ({
9598
invalid={Boolean(invalidText)}
9699
invalidText={invalidText}
97100
items={options}
98-
initialSelectedItems={Array.isArray(rest.value) ? rest.value : []}
101+
initialSelectedItems={getMultiValue(rest.value, options)}
99102
/>
100103
);
101104

@@ -118,6 +121,8 @@ ClearedMultiSelect.propTypes = {
118121
isDisabled: PropTypes.bool
119122
};
120123

124+
const getSelectValue = (value, isMulti) => (isMulti ? value : Array.isArray(value) ? value[0] : value);
125+
121126
const ClearedSelect = ({
122127
isSearchable,
123128
isClearable,
@@ -134,9 +139,17 @@ const ClearedSelect = ({
134139
closeMenuOnSelect,
135140
originalOnChange,
136141
placeholder,
142+
value,
137143
...rest
138144
}) => (
139-
<CarbonSelect disabled={isFetching} {...rest} id={rest.name} invalid={Boolean(invalidText)} invalidText={invalidText}>
145+
<CarbonSelect
146+
value={getSelectValue(value, isMulti)}
147+
disabled={isFetching}
148+
{...rest}
149+
id={rest.name}
150+
invalid={Boolean(invalidText)}
151+
invalidText={invalidText}
152+
>
140153
{isFetching && <SelectItem text={placeholder} value={''} />}
141154
{options.map((option, index) => (
142155
<SelectItem key={option.value || index} text={option.label} {...option} />
@@ -162,9 +175,18 @@ ClearedSelect.propTypes = {
162175
isDisabled: PropTypes.bool,
163176
isRequired: PropTypes.bool,
164177
isSearchable: PropTypes.bool,
165-
isClearable: PropTypes.bool
178+
isClearable: PropTypes.bool,
179+
value: PropTypes.any
166180
};
167181

182+
const getComboInitialValue = (value, options = []) => {
183+
const result = Array.isArray(value) ? value[0] : value;
184+
return typeof result === 'object' ? result : options.find(({ value }) => value === result) || result;
185+
};
186+
187+
/**
188+
* Combobox cannot be multi value
189+
*/
168190
const ClearedSelectSearchable = ({
169191
isSearchable,
170192
isClearable,
@@ -174,7 +196,7 @@ const ClearedSelectSearchable = ({
174196
hideSelectedOptions,
175197
noOptionsMessage,
176198
onInputChange,
177-
options,
199+
options = [],
178200
isFetching,
179201
invalid,
180202
classNamePrefix,
@@ -190,7 +212,7 @@ const ClearedSelectSearchable = ({
190212
id={rest.name}
191213
invalid={Boolean(invalidText)}
192214
invalidText={invalidText}
193-
initialSelectedItem={rest.value}
215+
initialSelectedItem={getComboInitialValue(rest.value, options)}
194216
items={options}
195217
placeholder={placeholder}
196218
titleText={labelText}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,71 @@ describe('<Select />', () => {
5656
});
5757
});
5858

59+
it('should render initial value label text when only value is passed as initial value', () => {
60+
const schema = {
61+
fields: [
62+
{
63+
component: componentTypes.SELECT,
64+
name: 'select',
65+
label: 'select',
66+
isSearchable: true,
67+
isClearable: true,
68+
options: [
69+
{ label: 'option 1', value: 1 },
70+
{ label: 'option 2', value: 2 }
71+
]
72+
}
73+
]
74+
};
75+
76+
const wrapper = mount(
77+
<FormRenderer
78+
initialValues={{ select: 1 }}
79+
onSubmit={jest.fn()}
80+
FormTemplate={(props) => <FormTemplate {...props} />}
81+
schema={schema}
82+
componentMapper={componentMapper}
83+
/>
84+
);
85+
86+
const valueInput = wrapper.find('input[name="select"]');
87+
expect(valueInput).toHaveLength(1);
88+
expect(valueInput.props().value).toEqual('option 1');
89+
});
90+
91+
it('should render initial value label text when only value is passed as initial value with simpleValue option', () => {
92+
const schema = {
93+
fields: [
94+
{
95+
component: componentTypes.SELECT,
96+
name: 'select',
97+
label: 'select',
98+
isSearchable: true,
99+
isClearable: true,
100+
simpleValue: true,
101+
options: [
102+
{ label: 'option 1', value: 1 },
103+
{ label: 'option 2', value: 2 }
104+
]
105+
}
106+
]
107+
};
108+
109+
const wrapper = mount(
110+
<FormRenderer
111+
initialValues={{ select: 1 }}
112+
onSubmit={jest.fn()}
113+
FormTemplate={(props) => <FormTemplate {...props} />}
114+
schema={schema}
115+
componentMapper={componentMapper}
116+
/>
117+
);
118+
119+
const valueInput = wrapper.find('input[name="select"]');
120+
expect(valueInput).toHaveLength(1);
121+
expect(valueInput.props().value).toEqual('option 1');
122+
});
123+
59124
it('renders multi select', () => {
60125
const schema = {
61126
fields: [

0 commit comments

Comments
 (0)