Skip to content

Commit b9d42b9

Browse files
committed
fix pagination issues
1 parent 1f4efe4 commit b9d42b9

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flatlist-react",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "A helpful utility component to handle lists in react like a champ",
55
"main": "./lib/index.js",
66
"scripts": {

src/flatlist-react.tsx

+25-4
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ const FlatList = forwardRef((props: Props, ref: Ref<HTMLElement>) => {
8686
sortBy, sortDesc, sort, sortCaseInsensitive, sortGroupBy, sortGroupDesc, // sort props
8787
searchBy, searchOnEveryWord, searchTerm, searchCaseInsensitive, // search props
8888
display, displayRow, rowGap, displayGrid, gridGap, minColumnWidth, // display props,
89-
hasMoreItems, loadMoreItems, paginationLoadingIndicator, paginationLoadingIndicatorPosition,
90-
pagination, // pagination props
89+
hasMoreItems, loadMoreItems, paginationLoadingIndicator, paginationLoadingIndicatorPosition, // pagination props
9190
...otherProps // props to be added to the wrapper container if wrapperHtmlTag is specified
9291
} = props;
9392
// tslint:disable-next-line:prefer-const
94-
let {list, group, search, ...tagProps} = otherProps;
93+
let {list, group, search, pagination, ...tagProps} = otherProps;
9594

9695
list = convertListToArray(list);
9796

@@ -209,6 +208,13 @@ const FlatList = forwardRef((props: Props, ref: Ref<HTMLElement>) => {
209208
});
210209
}
211210

211+
if (pagination && pagination.loadMore) {
212+
pagination = {
213+
...(FlatList.defaultProps && FlatList.defaultProps.pagination),
214+
...pagination
215+
};
216+
}
217+
212218
const content = (
213219
<Fragment>
214220
{
@@ -222,7 +228,7 @@ const FlatList = forwardRef((props: Props, ref: Ref<HTMLElement>) => {
222228
{...{display, displayRow, rowGap, displayGrid, gridGap, minColumnWidth}}
223229
showGroupSeparatorAtTheBottom={group.separatorAtTheBottom || showGroupSeparatorAtTheBottom}
224230
/>
225-
{(loadMoreItems || pagination.loadMore) &&
231+
{(loadMoreItems || (pagination && pagination.loadMore)) &&
226232
<InfiniteLoader
227233
hasMore={hasMoreItems || pagination.hasMore}
228234
loadMore={loadMoreItems || pagination.loadMore}
@@ -324,6 +330,15 @@ FlatList.propTypes = {
324330
* the minimum column width when display grid is activated
325331
*/
326332
minColumnWidth: string,
333+
/**
334+
* a pagination shorthand configuration
335+
*/
336+
pagination: shape({
337+
hasMore: bool,
338+
loadMore: func,
339+
loadingIndicator: oneOfType([node, func, element]),
340+
loadingIndicatorPosition: string,
341+
}),
327342
/**
328343
* a custom element to be used instead of the default loading indicator for pagination
329344
*/
@@ -441,6 +456,12 @@ FlatList.defaultProps = {
441456
limit: 0,
442457
loadMoreItems: null,
443458
minColumnWidth: '',
459+
pagination: {
460+
hasMore: false,
461+
loadMore: null,
462+
loadingIndicator: null,
463+
loadingIndicatorPosition: '',
464+
},
444465
paginationLoadingIndicator: undefined,
445466
paginationLoadingIndicatorPosition: '',
446467
renderWhenEmpty: null,

src/subComponents/InfiniteLoader.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import DefaultLoadIndicator from './DefaultLoadIndicator';
44
import {bool, element, func, node, oneOf, oneOfType} from 'prop-types';
55

66
export interface InfiniteLoaderInterface {
7-
loadingIndicator: () => JSX.Element | JSX.Element | null;
7+
loadingIndicator: null | (() => JSX.Element) | JSX.Element;
88
loadingIndicatorPosition: string;
99
hasMore: boolean;
10-
loadMore: () => void;
10+
loadMore: null | (() => void);
1111
}
1212

1313
interface State {
@@ -140,7 +140,7 @@ class InfiniteLoader extends Component<InfiniteLoaderInterface, State> {
140140
if (loaderPosition <= startingPoint) {
141141
this.waitingForUpdate = true;
142142
if (!this.state.loading) {
143-
this.setState({loading: true}, this.props.loadMore);
143+
this.setState({loading: true}, this.props.loadMore as (() => void));
144144
}
145145
}
146146
}
@@ -165,6 +165,7 @@ class InfiniteLoader extends Component<InfiniteLoaderInterface, State> {
165165
{
166166
hasMore &&
167167
(loadingIndicator ?
168+
// @ts-ignore
168169
(isFunction(loadingIndicator) ? loadingIndicator() : loadingIndicator) :
169170
DefaultLoadIndicator)
170171
}

0 commit comments

Comments
 (0)