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: filterSort don't work on group item #1068

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: sorter(item.options),
};
}
return { ...item };
});
};
Zyf665 marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
afc163 marked this conversation as resolved.
Show resolved Hide resolved
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