Skip to content

Commit 96528ed

Browse files
committed
Remove invalid options for combobox component
Fix: Ensure selected options array does not contain undefined values
1 parent 25803bc commit 96528ed

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/components/Combobox/Combobox.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,5 +561,28 @@ describe('<Combobox />', () => {
561561

562562
assert.deepStrictEqual(value, []);
563563
});
564+
565+
it('should remove invalid option from value', () => {
566+
let value = [1, 2, 999];
567+
const mockOnChange = (v) => {
568+
value = v;
569+
};
570+
render(<Combobox options={OPTIONS} value={value} onChange={mockOnChange} multi />);
571+
572+
const removeOptionButtonOne = screen.getByRole('button', {
573+
name: /remove option: 1/i,
574+
});
575+
within(removeOptionButtonOne).getByText(/r2-d2/i);
576+
577+
const removeOptionButtonTwo = screen.getByRole('button', {
578+
name: /remove option: 2/i,
579+
});
580+
within(removeOptionButtonTwo).getByText(/bb8/i);
581+
582+
const removeOptionButtonInvalid = screen.queryByRole('button', {
583+
name: /remove option: 999/i,
584+
});
585+
expect(removeOptionButtonInvalid).not.toBeInTheDocument();
586+
});
564587
});
565588
});

src/components/Combobox/Combobox.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ function Combobox<T>({
9595
const selected = useMemo<Option<T> | Option<T>[]>(
9696
() =>
9797
multi
98-
? (value || []).map((v: T) => options.find((option) => equal(option.value, v)))
98+
? (value || [])
99+
.map((v: T) => options.find((option) => equal(option.value, v)))
100+
.filter((option: Option<T>) => option !== undefined)
99101
: options.find((option) => equal(option.value, value)),
100102
[value, options, multi]
101103
);

0 commit comments

Comments
 (0)