Skip to content

Commit d97b770

Browse files
Zyf665coderabbitai[bot]afc163
authored
fix: filterSort don't work on group item (#1068)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: afc163 <[email protected]>
1 parent e55d4a1 commit d97b770

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/Select.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,26 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
431431
mergedSearchValue,
432432
mergedFieldNames,
433433
]);
434-
434+
const sorter = (inputOptions: DefaultOptionType[]) => {
435+
const sortedOptions = [...inputOptions].sort((a, b) =>
436+
filterSort(a, b, { searchValue: mergedSearchValue }),
437+
);
438+
return sortedOptions.map((item) => {
439+
if (Array.isArray(item.options)) {
440+
return {
441+
...item,
442+
options: item.options.length > 0 ? sorter(item.options) : item.options,
443+
};
444+
}
445+
return item;
446+
});
447+
};
435448
const orderedFilteredOptions = React.useMemo(() => {
436449
if (!filterSort) {
437450
return filledSearchOptions;
438451
}
439452

440-
return [...filledSearchOptions].sort((a, b) =>
441-
filterSort(a, b, { searchValue: mergedSearchValue }),
442-
);
453+
return sorter(filledSearchOptions);
443454
}, [filledSearchOptions, filterSort, mergedSearchValue]);
444455

445456
const displayOptions = React.useMemo(

tests/Select.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,42 @@ describe('Select.Basic', () => {
19481948
);
19491949
});
19501950

1951+
it('filterSort should work with search value when grouping', () => {
1952+
const { container } = render(
1953+
<Select
1954+
open
1955+
showSearch
1956+
searchValue="entry"
1957+
style={{ width: 100 }}
1958+
placeholder="Search to Select"
1959+
optionFilterProp="label"
1960+
filterSort={(optionA, optionB, info) => {
1961+
if (!info.searchValue) return 0;
1962+
const labelA = (optionA?.label ?? '').toLowerCase();
1963+
const labelB = (optionB?.label ?? '').toLowerCase();
1964+
const matchA = labelA.startsWith(info.searchValue);
1965+
const matchB = labelB.startsWith(info.searchValue);
1966+
if (matchA && !matchB) return -1;
1967+
if (!matchA && matchB) return 1;
1968+
return labelA.localeCompare(labelB);
1969+
}}
1970+
options={[
1971+
{
1972+
value: 'group1',
1973+
label: 'group1',
1974+
options: [
1975+
{ label: 'Entry1', value: 'Entry1' },
1976+
{ label: 'Entry2', value: 'Entry2' },
1977+
{ label: 'Entry3', value: 'Entry3' },
1978+
{ label: 'Entry', value: 'Entry' },
1979+
],
1980+
},
1981+
]}
1982+
/>,
1983+
);
1984+
expect(container.querySelector('.rc-select-item-option-grouped').textContent).toBe('Entry');
1985+
});
1986+
19511987
it('correctly handles the `tabIndex` prop', () => {
19521988
const { container } = render(<Select tabIndex={0} />);
19531989
expect(container.querySelector('.rc-select').getAttribute('tabindex')).toBeFalsy();

0 commit comments

Comments
 (0)