Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/components/Combobox/Combobox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,5 +561,28 @@ describe('<Combobox />', () => {

assert.deepStrictEqual(value, []);
});

it('should remove invalid option from value', () => {
let value = [1, 2, 999];
const mockOnChange = (v) => {
value = v;
};
render(<Combobox options={OPTIONS} value={value} onChange={mockOnChange} multi />);

const removeOptionButtonOne = screen.getByRole('button', {
name: /remove option: 1/i,
});
within(removeOptionButtonOne).getByText(/r2-d2/i);

const removeOptionButtonTwo = screen.getByRole('button', {
name: /remove option: 2/i,
});
within(removeOptionButtonTwo).getByText(/bb8/i);

const removeOptionButtonInvalid = screen.queryByRole('button', {
name: /remove option: 999/i,
});
expect(removeOptionButtonInvalid).not.toBeInTheDocument();
});
});
});
4 changes: 3 additions & 1 deletion src/components/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ function Combobox<T>({
const selected = useMemo<Option<T> | Option<T>[]>(
() =>
multi
? (value || []).map((v: T) => options.find((option) => equal(option.value, v)))
? (value || [])
.map((v: T) => options.find((option) => equal(option.value, v)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could write this a bit more efficiently with reduce if you wanted, but also fine as is

.reduce<Option<T>[]>((acc, v) => {
      const found = options.find((option) => equal(option.value, v));
      if (found) acc.push(found);
      return acc;
    }, [])

.filter((option: Option<T>) => option !== undefined)
: options.find((option) => equal(option.value, value)),
[value, options, multi]
);
Expand Down
Loading