ratty-blush
ratty-blush2y ago

useSelect fetch on scroll

it's me again!) How to use scroll pagination with select via useSelect hook? Maybe you have any example?
29 Replies
useful-bronze
useful-bronze2y ago
ratty-blush
ratty-blush2y ago
i saw it, but in your example it doesn't work( https://owaiyvrkbgithub-hbbq--3000.local.webcontainer.io/posts/create it fetches data ones and thats it, but what about scrolling?)
useful-bronze
useful-bronze2y ago
oh sorry, i will debug and inform you
ratty-blush
ratty-blush2y ago
so i use this getList method in data provider
No description
ratty-blush
ratty-blush2y ago
and as i understand from docs, useSelect use em as default method
useful-bronze
useful-bronze2y ago
yes, you are right i check infinite loading example, in post list page, on table's category filter works as expected. am i missing something ? code example is here: pages/posts/list.tsx
ratty-blush
ratty-blush2y ago
No description
ratty-blush
ratty-blush2y ago
this one)
useful-bronze
useful-bronze2y ago
infinite scroll example only implemented on here. category section filter
No description
ratty-blush
ratty-blush2y ago
wow i try to find it in create)) or edit)
useful-bronze
useful-bronze2y ago
i guess we need to write this to doc, we have 4 page and only table filter has implemented code. sorry about that.
ratty-blush
ratty-blush2y ago
yep, that would be nice for the next similar questions) from Refine users)
useful-bronze
useful-bronze2y ago
this is the fetch on scroll related code. src/pages/posts/list.tsx
---

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>
)}
---

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>
)}
yes you are absolutely right, thanks for the feedback ! we will handle it as soon as possible
ratty-blush
ratty-blush2y ago
yep, found this)
useful-bronze
useful-bronze2y ago
actually we have useSelectInfiniteScroll() feature in our backlog. when we ship this feature, it will be much easier to use. just like a useSelect !