Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Select): fix filtering in case of using grouped options #1011

Merged
merged 1 commit into from
Sep 15, 2023
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
4 changes: 4 additions & 0 deletions src/components/Select/__stories__/SelectShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export const SelectShowcase = (props: SelectProps) => {
<Select.Option value="val3" content="Value3" />
<Select.Option value="val4" content="Value4" />
</Select.OptionGroup>
<Select.OptionGroup label="Group 3">
<Select.Option value="val5" content="Value5" />
<Select.Option value="val6" content="Value6" />
</Select.OptionGroup>
</ExampleItem>
<ExampleItem
title="Select with disabled options"
Expand Down
16 changes: 15 additions & 1 deletion src/components/Select/__tests__/Select.filter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import userEvent from '@testing-library/user-event';
import {TextInput} from '../../controls';
import type {SelectOption, SelectProps} from '../types';

import {TEST_QA, generateOptions, setup} from './utils';
import {TEST_QA, generateOptions, generateOptionsGroups, setup} from './utils';

afterEach(() => {
cleanup();
Expand Down Expand Up @@ -102,4 +102,18 @@ describe('Select filter', () => {
// 10, 20, 30, 40
expect(queryAllByRole('option').length).toBe(4);
});

test('should not display labels of empty groups during filtering', async () => {
const {getByTestId, queryAllByRole} = setup({
options: generateOptionsGroups(4, 1),
filterable: true,
});
const user = userEvent.setup();
const selectControl = getByTestId(TEST_QA);
await user.click(selectControl);
// 4 group labels + 1 option in each group
expect(queryAllByRole('option').length).toBe(8);
await user.keyboard('definitely not option');
expect(queryAllByRole('option').length).toBe(0);
});
});
10 changes: 10 additions & 0 deletions src/components/Select/__tests__/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ export function generateOptions(args: number | [string, string][]): SelectOption

return args.map(([value, content]) => ({value, content}));
}

export function generateOptionsGroups(
groupsCount: number,
optionsCount: number,
): SelectOptionGroup[] {
return range(0, groupsCount).map((i) => ({
label: `Group ${i + 1}`,
options: generateOptions(optionsCount),
}));
}
2 changes: 1 addition & 1 deletion src/components/Select/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const getFilteredFlattenOptions = (args: {

return filteredOptions.reduce((acc, option, index) => {
const groupTitle = isGroupTitle(option);
const previousGroupTitle = isGroupTitle(acc[index - 1]);
const previousGroupTitle = isGroupTitle(acc[acc.length - 1]);
const isLastOption = index === filteredOptions.length - 1;

if (groupTitle && previousGroupTitle) {
Expand Down
Loading