useSelect fetch on scroll
it's me again!)
How to use scroll pagination with select via useSelect hook? Maybe you have any example?
How to use scroll pagination with select via useSelect hook? Maybe you have any example?



Select antd is going to have a custom prop for it but no, they left this to the user useSelectInfiniteScroll()filterOption ---
const [options, setOptions] = useState<SelectProps["options"]>([]);
const [search, setSearch] = useState<string>("");
const hasMore = useRef(true);
---
const { selectProps: categorySelectProps } = useSelect<ICategory>({
resource: "categories",
fetchSize: 20,
pagination: { current: page },
queryOptions: {
keepPreviousData: true,
onSuccess(data) {
if (!data.data?.length) {
hasMore.current = false;
}
const normalizedData = data.data?.map((item) => ({
label: item.title,
value: item.id,
}));
if (!search?.length) {
setOptions((prev) => [...(prev || []), ...normalizedData]);
} else {
setOptions(normalizedData);
}
},
},
onSearch: (value) => {
setPage(1);
hasMore.current = true;
setSearch(value);
return [
{
field: "title",
operator: "contains",
value,
},
];
},
});
---
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Select
mode="multiple"
style={{ minWidth: 200 }}
{...categorySelectProps}
// need to add options with searchValue and onPopupScroll
options={options}
onPopupScroll={(event) => {
const target = event.currentTarget;
if (
target.scrollTop +
target.offsetHeight ===
target.scrollHeight
) {
if (
categorySelectProps.options &&
hasMore.current
) {
setPage((curr) => curr + 1);
}
}
}}
/>
</FilterDropdown>
)}