Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 15 additions & 4 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,26 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
mergedSearchValue,
mergedFieldNames,
]);

const sorter = (inputOptions: DefaultOptionType[]) => {
const sortedOptions = [...inputOptions].sort((a, b) =>
filterSort(a, b, { searchValue: mergedSearchValue }),
);
return sortedOptions.map((item) => {
if (Array.isArray(item.options)) {
return {
...item,
options: item.options.length > 0 ? sorter(item.options) : item.options,
};
}
return item;
});
};
const orderedFilteredOptions = React.useMemo(() => {
if (!filterSort) {
return filledSearchOptions;
}

return [...filledSearchOptions].sort((a, b) =>
filterSort(a, b, { searchValue: mergedSearchValue }),
);
return sorter(filledSearchOptions);
}, [filledSearchOptions, filterSort, mergedSearchValue]);

const displayOptions = React.useMemo(
Expand Down
35 changes: 35 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,41 @@ describe('Select.Basic', () => {
'Communicated',
);
});
it('filterSort should work with search value when grouping', () => {
const { container } = render(
<Select
open
showSearch
searchValue="entry"
style={{ width: 100 }}
placeholder="Search to Select"
optionFilterProp="label"
filterSort={(optionA, optionB, info) => {
if (!info.searchValue) return 0;
const labelA = (optionA?.label ?? '').toLowerCase();
const labelB = (optionB?.label ?? '').toLowerCase();
const matchA = labelA.startsWith(info.searchValue);
const matchB = labelB.startsWith(info.searchValue);
if (matchA && !matchB) return -1;
if (!matchA && matchB) return 1;
return labelA.localeCompare(labelB);
}}
options={[
{
value: 'group1',
label: 'group1',
options: [
{ label: 'Entry1', value: 'Entry1' },
{ label: 'Entry2', value: 'Entry2' },
{ label: 'Entry3', value: 'Entry3' },
{ label: 'Entry', value: 'Entry' },
],
},
]}
/>,
);
expect(container.querySelector('.rc-select-item-option-grouped').textContent).toBe('Entry');
});

it('correctly handles the `tabIndex` prop', () => {
const { container } = render(<Select tabIndex={0} />);
Expand Down
Loading