Skip to content

Commit a09e86e

Browse files
fix(Select): fix filtering in groups
1 parent c8ad85d commit a09e86e

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/components/Select/__stories__/SelectShowcase.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export const SelectShowcase = (props: SelectProps) => {
179179
<Select.Option value="val3" content="Value3" />
180180
<Select.Option value="val4" content="Value4" />
181181
</Select.OptionGroup>
182+
<Select.OptionGroup label="Group 3">
183+
<Select.Option value="val5" content="Value5" />
184+
<Select.Option value="val6" content="Value6" />
185+
</Select.OptionGroup>
182186
</ExampleItem>
183187
<ExampleItem
184188
title="Select with disabled options"

src/components/Select/__tests__/Select.filter.test.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import userEvent from '@testing-library/user-event';
66
import {TextInput} from '../../controls';
77
import type {SelectOption, SelectProps} from '../types';
88

9-
import {TEST_QA, generateOptions, setup} from './utils';
9+
import {TEST_QA, generateOptions, generateOptionsGroups, setup} from './utils';
1010

1111
afterEach(() => {
1212
cleanup();
@@ -102,4 +102,18 @@ describe('Select filter', () => {
102102
// 10, 20, 30, 40
103103
expect(queryAllByRole('option').length).toBe(4);
104104
});
105+
106+
test('should not display labels of empty groups during filtering', async () => {
107+
const {getByTestId, queryAllByRole} = setup({
108+
options: generateOptionsGroups(4, 1),
109+
filterable: true,
110+
});
111+
const user = userEvent.setup();
112+
const selectControl = getByTestId(TEST_QA);
113+
await user.click(selectControl);
114+
// 4 group labels + 1 option in each group
115+
expect(queryAllByRole('option').length).toBe(8);
116+
await user.keyboard('definitely not option');
117+
expect(queryAllByRole('option').length).toBe(0);
118+
});
105119
});

src/components/Select/__tests__/utils.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,13 @@ export function generateOptions(args: number | [string, string][]): SelectOption
8080

8181
return args.map(([value, content]) => ({value, content}));
8282
}
83+
84+
export function generateOptionsGroups(
85+
groupsCount: number,
86+
optionsCount: number,
87+
): SelectOptionGroup[] {
88+
return range(0, groupsCount).map((i) => ({
89+
label: `Group ${i + 1}`,
90+
options: generateOptions(optionsCount),
91+
}));
92+
}

src/components/Select/utils.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export const getFilteredFlattenOptions = (args: {
243243

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

249249
if (groupTitle && previousGroupTitle) {

0 commit comments

Comments
 (0)