1.0.0 • Published 2 years ago

d-react-native-awesome-list v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

React Native Custom List (Using FLatList, SectionList)

Help to implement FlatList, SectionList from React Native faster, iOS + Android

Setup

npm install --save https://github.com/boythan/react-native-awesome-list.git
# --- or ---
yarn add https://github.com/boythan/react-native-awesome-list.git

Using

FlatList with raw data

import AwesomeListComponent from "react-native-awesome-list";

//...


export default const RawDataAwesomeList = () => {
  const listRef = useRef(null)
  //...

  const source = () => {
    return Promise.resolve(YOUR_RAW_DATA_LIST)
  }

  //When you want to reload data in list
  //after this function called, awesomeList will run source then transformer again to fetch new data for list
  const refreshData = () => {
    listRef.refresh()
  }

  //render row item in list
  const renderItem = ({item, index}) => <Text>{item.name}</Text>

  return <AwesomeListComponent
            source={source}
            renderItem={(renderItem}
            ref={listRef}
        />

  //...
}

FlatList with Promise, API call

import AwesomeListComponent from "react-native-awesome-list";

//...

export default const CustomerAwesomeList = () => {
  const listRef = useRef(null)
  //...

  //Receive a promise that return the data will be displayed in list
  const source = (paging) => {
    return API.customerList(paging)
  }

  //Custom data from source function, ensure return an array
  const transformer = (res) => {
    return res?.data?.data ?? []
  }

  //When you want to reload data in list
  //after this function called, awesomeList will run source then transformer again to fetch new data for list
  const refreshData = () => {
    listRef.refresh()
  }

  //render row item in list
  const renderItem = ({item, index}) => <Text>{item.name}</Text>

  return <AwesomeListComponent
            isPaging
            source={source}
            transformer={transformer}
            renderItem={renderItem}
            ref={listRef}
        />

  //...
}

SectionList

import AwesomeListComponent from "react-native-awesome-list";

//...

export default const CustomerAwesomeList = () => {
  const listRef = useRef(null)
  //...

  //Receive a promise that return the data will be displayed in list
  const source = () => {
    return API.customerList()
  }

  //Custom data from source function, ensure return an array
  const transformer = (res) => {
    return res?.data?.data ?? []
  }
  //Custom data from source function, ensure return an array
  const createSections = (res) => {
    return [{
      title: "Section 1",
      data: []
    }]
  }

  //When you want to reload data in list
  //after this function called, awesomeList will run source then transformer again to fetch new data for list
  const refreshData = () => {
    listRef.refresh()
  }

  //render row item in list
  const renderItem = ({item, index}) => <Text>{item.name}</Text>
  const renderSectionHeader = ({section}) => <Text>{section?.title}</Text>

  return <AwesomeListComponent
            ref={listRef}
            isSectionList={true}
            source={source}
            renderItem={renderItem}
            transformer={transformer}
            renderSectionHeader={renderSectionHeader}
            createSections={createSections}

          />

  //...
}

API

Props

PropDefaultTypeDescription
source() => Promise.resolve([])() => PromiseSource of data
transformer(response) => response() => arrayArray will be displayed in list
renderItem({item, index}) => funcrender function for list item rendering
keyExtractor(item) => item?.id ?? itemfuncunique key for row
isPagingfalsebooleanlist will be paging(load more) or not
pageSize20numbernumber of item will be load in each page
containerStyleundefinedstyleroot wrapper style
listStyleundefinedstylelist style
emptyViewStyleundefinedstyleempty view style
renderSeparatorundefinedfuncseparator component
isSectionListfalsebooleanis section list or not
renderSectionHeaderundefinedfuncsection header component
createSectionsundefinedfuncreceive section data follow format {title: string, data: array}
renderEmptyViewundefinedfuncempty view (when data is empty) component
renderErrorViewundefinedfuncerror view (when source function is rejected) component
renderProgressundefinedfuncprogress view (when source function is waiting promise) component
listHeaderComponentundefinedfuncList's header component
emptyText"No result"textCustom empty text (when data is empty)
filterEmptyText"No filter result"textCustom filter empty text (when filter data is empty)
numColumns1numbernumber of column in list

Methods

methodparamsDescription
refreshnullRefresh data, awesomeList will re-run source then transformer
applyFilteractionFilterwhen you want to apply some filter for this list E.g listRef.applyFilter(item => item.isSelect)
removeFilternullwhen you want to remove all filter that you applied before

Enjoy

Questions or suggestions?

Feel free to open an issue