Skip to content

Commit

Permalink
support for scrollingContainerId prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Jul 1, 2023
1 parent 9cb7ba8 commit d1cef9e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
21 changes: 18 additions & 3 deletions documentation/Doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ FlatList comes with pagination out of the box. This is specially great for when
so you start with fetching a small portion and then let the user scroll to the bottom, show the loading indicator while you
fetch some more.

To make it work you need to render `FlatList` inside of a scrolling container with "`overflow: auto`" or "`overflow: scroll`".
If the container wrapping FlatList does not scroll it misses the point of infinite scrolling.
To make it work you need to render `FlatList` inside of a scrolling container ancestor element with "`overflow: auto`" or "`overflow: scroll`".

```jsx
// as direct child of a scrolling container
<div style={{overflow: "auto", height: "300px"}}>
<FlatList
list={this.props.people}
Expand All @@ -371,14 +371,29 @@ If the container wrapping FlatList does not scroll it misses the point of infini
</div>

// --- OR ---

// as a scrolling container itself using the "wrapperHtmlTag" prop
<FlatList
list={this.props.people}
renderItem={this.renderPerson}
renderWhenEmpty={this.showBlank}
wrapperHtmlTag="div"
style={{overflow: "auto", height: "300px"}}
/>

// --- OR ---
// as a DEEP child of a scrolling ancestor element using the "scrollingContainerId" prop
<div style={{overflow: "auto", height: "300px"}} id="scrolling-container">
<div className="mid-wrapper">
<div className="list-wrapper">
<FlatList
list={this.props.people}
renderItem={this.renderPerson}
renderWhenEmpty={this.showBlank}
scrollingContainerId="scrolling-container"
/>
</div>
</div>
</div>
```

Pagination props will not work while `renderOnScroll` is being used. Pagination already renders on scroll.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flatlist-react",
"version": "1.5.14",
"version": "1.5.15-next",
"description": "A helpful utility component to handle lists in react like a champ",
"main": "./lib/index.js",
"types": "./lib/index.d.js",
Expand Down
18 changes: 17 additions & 1 deletion src/___subComponents/InfiniteLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface InfiniteLoaderState {

interface InfiniteLoaderProps extends InfiniteLoaderInterface {
itemsCount: number;
scrollingContainerId?: string;
}

class InfiniteLoader extends Component<
Expand Down Expand Up @@ -46,10 +47,25 @@ class InfiniteLoader extends Component<
const { current: loadIndicatorContainer } = this.loaderContainerRef;

if (loadIndicatorContainer) {
let scrollingContainer = loadIndicatorContainer.parentNode as HTMLElement;

if (this.props.scrollingContainerId) {
// find the closest ancestor with the id
scrollingContainer = scrollingContainer.closest(
`#${this.props.scrollingContainerId}`
) as HTMLElement;

if (!scrollingContainer) {
throw new Error(
`Can't find scrolling container with id of "${this.props.scrollingContainerId}"`
);
}
}

this.setState(
{
loadIndicatorContainer,
scrollingContainer: loadIndicatorContainer.parentNode as HTMLElement,
scrollingContainer,
},
() => {
this.currentItemsCount = this.getScrollingContainerChildrenCount();
Expand Down
2 changes: 2 additions & 0 deletions src/flatListProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface FlatListProps<ListItem> {
scrollToTopOffset?: number;
scrollToTopPadding?: number;
scrollToTopPosition?: string;
scrollingContainerId?: string;
// others
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
Expand Down Expand Up @@ -183,6 +184,7 @@ export const defaultProps: FlatListProps<any> = {
scrollToTopOffset: undefined,
scrollToTopPadding: undefined,
scrollToTopPosition: undefined,
scrollingContainerId: undefined,
// SEARCH
search: {
by: "",
Expand Down
2 changes: 2 additions & 0 deletions src/flatlist-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function FlatList<ListItem>(props: FlatListProps<ListItem>) {
scrollToTopPadding,
scrollToTopOffset,
scrollToTopPosition,
scrollingContainerId,
pagination = {} as InfiniteLoaderInterface, // pagination props
// eslint-disable-next-line @typescript-eslint/naming-convention
// @ts-ignore
Expand Down Expand Up @@ -113,6 +114,7 @@ function FlatList<ListItem>(props: FlatListProps<ListItem>) {
loadingIndicator={
paginationLoadingIndicator || pagination.loadingIndicator
}
scrollingContainerId={scrollingContainerId}
loadingIndicatorPosition={
paginationLoadingIndicatorPosition ||
pagination.loadingIndicatorPosition
Expand Down

0 comments on commit d1cef9e

Please sign in to comment.