Skip to content

Commit

Permalink
fix(datatable): fix infinite scroll (#141)
Browse files Browse the repository at this point in the history
* fix: infinite scroll not triggering after data fetched

* fix: loading state and dependency

* chore: update package version

* chore: pr comment fix

* fix: changed ref logic

* chore: remove unwanted code
  • Loading branch information
paanSinghCoder authored Sep 5, 2024
1 parent 26f6c32 commit 7976e5c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/www/examples/shield-ts/assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const Assets = () => {
}, [isLoading, hasMoreData, page]);

useEffect(() => {
setData(getData())
loadMoreData()
}, [])

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/raystack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raystack/apsara",
"version": "0.18.6",
"version": "0.19.0",
"types": "dist/index.d.ts",
"sideEffects": false,
"engines": {
Expand Down
56 changes: 33 additions & 23 deletions packages/raystack/table/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ function DataTableRoot<TData, TValue>({

const [tableState, setTableState] = useState<Partial<TableState>>({});
const observerRef = useRef<IntersectionObserver | null>(null);
const lastRowRef = useRef<HTMLTableRowElement | null>(null);

useEffect(() => {
if (!onLoadMore) return;

const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && !isLoading) {
onLoadMore();
}
}, { threshold: 0.1 });

observerRef.current = observer;

return () => observer.disconnect();
}, [onLoadMore]);

useEffect(() => {
const observer = observerRef.current;
const lastRow = lastRowRef.current;

if (observer && lastRow) {
observer.disconnect();
observer.observe(lastRow);
}

return () => {
if (observer && lastRow) {
observer.unobserve(lastRow);
}
};
}, [isLoading]);

const getLoader = (loaderRow: number, columns: ApsaraColumnDef<TData>[]) => (
[...new Array(loaderRow)].map((_, rowIndex) => (
Expand Down Expand Up @@ -162,28 +193,6 @@ function DataTableRoot<TData, TValue>({
state: tableState,
});

const lastRowRef = useCallback(
(node: HTMLElement | null) => {
if (isLoading) return;
if (observerRef.current) observerRef.current.disconnect();

observerRef.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && onLoadMore) {
onLoadMore();
}
});

if (node) observerRef.current.observe(node);
},
[isLoading, onLoadMore]
);

useEffect(() => {
return () => {
if (observerRef.current) observerRef.current.disconnect();
};
}, []);

const tableStyle = {
...(table.getRowModel().rows?.length
? { width: "100%" }
Expand Down Expand Up @@ -289,11 +298,12 @@ function DataTableRoot<TData, TValue>({
</Table.Row>
))
) : (
!isLoading ?
<Table.Row>
<Table.Cell colSpan={columns.length}>
{emptyState || "No results."}
</Table.Cell>
</Table.Row>
</Table.Row> : <></>
)}
{isLoading && getLoader(loaderRow, columns)}
</Table.Body>
Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7976e5c

Please sign in to comment.