All files / react-native-infinite-flatlist InfiniteFlatList.js

87.5% Statements 7/8
62.5% Branches 5/8
40% Functions 2/5
87.5% Lines 7/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                            2x   2x       2x   2x                                 1x                                                                                     1x                 2x          
import PropTypes from "prop-types";
import React from "react";
import { FlatList } from "react-native";
 
import ListEmpty from "./src/ListEmpty";
import ListFooter from "./src/ListFooter";
import LoadingIndicator from "./src/LoadingIndicator";
 
class InfiniteFlatList extends React.Component {
  render() {
    const {
      data,
      loading,
      refreshing,
      onRefresh,
      onEndReached,
      emptyText,
      renderItem,
      keyExtractor,
      removeClippedSubviews,
      onEndReachedThreshold,
      ...props
    } = this.props;
 
    Iif (!data.length && loading) {
      return <LoadingIndicator />;
    }
 
    const emptyListText = (loading || refreshing) ? 'Loading...' : emptyText;
 
    return (
      <FlatList data={data}
                renderItem={renderItem}
                keyExtractor={keyExtractor}
                refreshing={refreshing}
                onRefresh={onRefresh}
                onEndReached={onEndReached}
                onEndReachedThreshold={onEndReachedThreshold}
                removeClippedSubviews={removeClippedSubviews}
                ListEmptyComponent={<ListEmpty text={emptyListText} />}
                ListFooterComponent={<ListFooter loading={loading} />}
                {...props}
      />
    );
  }
}
 
InfiniteFlatList.propTypes = {
  /**
   * The items to display - default: []
   */
  data: PropTypes.array,
  /**
   * Loading flag (initial load and paging) - default: false
   */
  loading: PropTypes.bool,
  /**
   * Refreshing flag (pull-to-refresh) - default: false
   */
  refreshing: PropTypes.bool,
  /**
   * Text to display when no items - default: 'No data'
   */
  emptyText: PropTypes.string,
  /**
   * The render item function - default: noop
   */
  renderItem: PropTypes.func,
  /**
   * The fetch action to dispatch paging requests - default: noop
   */
  onEndReached: PropTypes.func,
  /**
   * How far from the end of the list before triggering the onEndReached function
   * */
  onEndReachedThreshold: PropTypes.number,
  /**
   * The fetch action to dispatch initial load and pull to refresh - default: noop
   */
  onRefresh: PropTypes.func,
  /**
   * The resource key extractor - default: object id
   */
  keyExtractor: PropTypes.func,
  /**
   * removeClippedSubviews - default: false
   */
  removeClippedSubviews: PropTypes.bool
};
 
InfiniteFlatList.defaultProps = {
  data: [],
  loading: false,
  refreshing: false,
  emptyText: 'No data',
  renderItem: () => {},
  onEndReached: () => {},
  onEndReachedThreshold: 0.75,
  onRefresh: () => {},
  keyExtractor: resource => resource.id,
  removeClippedSubviews: false
};
 
export default InfiniteFlatList;