forked from raystack/apsara
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added new component for infinite scroll
- Loading branch information
Ayushi Sharma
committed
Apr 19, 2024
1 parent
ec7457d
commit 6ec1c85
Showing
5 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { debounce } from "lodash"; | ||
import React, { useState, useEffect } from "react"; | ||
|
||
interface InfiniteScrollProps<T> { | ||
fetchMoreData: (page: number, pageSize: number, filters: any) => Promise<T[]>; | ||
contentRef: React.RefObject<HTMLDivElement>; | ||
pageSize?: number; | ||
filters?: any; | ||
renderItem: (item: T) => React.ReactNode; | ||
loadingComponent?: React.ReactNode; | ||
noMoreDataComponent?: React.ReactNode; | ||
} | ||
|
||
const InfiniteScroll: React.FC<InfiniteScrollProps<any>> = ({ | ||
fetchMoreData, | ||
contentRef, | ||
pageSize = 10, | ||
filters, | ||
renderItem, | ||
loadingComponent, | ||
noMoreDataComponent, | ||
}) => { | ||
const [data, setData] = useState<any[]>([]); | ||
const [page, setPage] = useState(1); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [hasMoreData, setHasMoreData] = useState(true); | ||
const [isFirstFetch, setIsFirstFetch] = useState(true); | ||
|
||
const onBottomHit = debounce(async () => { | ||
if (!isLoading && hasMoreData && isBottom(contentRef)) { | ||
setIsLoading(true); | ||
try { | ||
const newData = await fetchMoreData(page, pageSize, filters); | ||
setData((prevData) => prevData.concat(newData)); | ||
setPage((prevPage) => prevPage + 1); | ||
setHasMoreData(newData.length > 0); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
} | ||
}, 200); | ||
|
||
useEffect(() => { | ||
setPage(1); | ||
setHasMoreData(true); | ||
setData([]); | ||
}, [filters]); | ||
|
||
useEffect(() => { | ||
if (isFirstFetch) { | ||
onBottomHit(); | ||
} | ||
setIsFirstFetch(false); | ||
|
||
const onScroll = () => { | ||
onBottomHit(); | ||
}; | ||
|
||
const currentContentRef = contentRef.current; | ||
|
||
if (currentContentRef) { | ||
currentContentRef.addEventListener("scroll", onScroll); | ||
} | ||
|
||
return () => { | ||
if (currentContentRef) { | ||
currentContentRef.removeEventListener("scroll", onScroll); | ||
} | ||
}; | ||
}, [ | ||
contentRef, | ||
isLoading, | ||
hasMoreData, | ||
page, | ||
filters, | ||
fetchMoreData, | ||
setData, | ||
setPage, | ||
setHasMoreData, | ||
isFirstFetch, | ||
onBottomHit, | ||
]); | ||
|
||
const isBottom = (ref: React.RefObject<HTMLDivElement>) => { | ||
if (!ref.current) { | ||
return false; | ||
} | ||
|
||
const { scrollTop, scrollHeight, clientHeight } = ref.current; | ||
|
||
return scrollTop + clientHeight >= scrollHeight; | ||
}; | ||
|
||
return ( | ||
<div className="infinite-list"> | ||
{data.map(renderItem)} | ||
{isLoading ? loadingComponent : null} | ||
{!hasMoreData && noMoreDataComponent} | ||
</div> | ||
); | ||
}; | ||
|
||
export default InfiniteScroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters