Skip to content

Commit c7793c2

Browse files
fix(compass-components): fixes custom options combobox not remembering previously selected custom options (#4272)
1 parent 5453f29 commit c7793c2

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

packages/compass-components/src/components/combobox-with-custom-option.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ export const ComboboxWithCustomOption = <M extends boolean>({
1818
multiselect = false as M,
1919
...props
2020
}: ComboboxWithCustomOptionProps<M>) => {
21+
const [customOptions, setCustomOptions] = useState<string[]>([]);
2122
const [search, setSearch] = useState('');
23+
2224
const comboboxOptions = useMemo(() => {
23-
const _opts = options.map((option, index) => (
25+
const totalOptions = [...options, ...customOptions];
26+
const _opts = totalOptions.map((option, index) => (
2427
<ComboboxOption
2528
key={`combobox-option-${index}`}
2629
value={option}
2730
displayName={option}
2831
/>
2932
));
30-
if (search && !options.includes(search)) {
33+
34+
if (search && !totalOptions.includes(search)) {
3135
_opts.push(
3236
<ComboboxOption
3337
key={`combobox-option-new`}
@@ -37,7 +41,8 @@ export const ComboboxWithCustomOption = <M extends boolean>({
3741
);
3842
}
3943
return _opts;
40-
}, [options, search, optionLabel]);
44+
}, [options, customOptions, search, optionLabel]);
45+
4146
return (
4247
<Combobox
4348
{...props}
@@ -46,9 +51,18 @@ export const ComboboxWithCustomOption = <M extends boolean>({
4651
onChange={(value: string | string[] | null) => {
4752
if (!onChange) return;
4853
if (multiselect) {
49-
(onChange as onChangeType<true>)(value as SelectValueType<true>);
54+
const multiSelectValues = value as SelectValueType<true>;
55+
const customOptions = multiSelectValues.filter(
56+
(value) => !options.includes(value)
57+
);
58+
setCustomOptions(customOptions);
59+
(onChange as onChangeType<true>)(multiSelectValues);
5060
} else {
51-
(onChange as onChangeType<false>)(value as SelectValueType<false>);
61+
const selectValue = value as SelectValueType<false>;
62+
if (selectValue && !options.includes(selectValue)) {
63+
setCustomOptions([selectValue]);
64+
}
65+
(onChange as onChangeType<false>)(selectValue);
5266
}
5367
}}
5468
>

0 commit comments

Comments
 (0)