GithubHelp home page GithubHelp logo

jsdf / react-native-refreshable-listview Goto Github PK

View Code? Open in Web Editor NEW
1.4K 49.0 169.0 296 KB

Deprecated. A pull-to-refresh ListView which shows a loading spinner while your data reloads

JavaScript 79.34% Objective-C 14.16% Java 6.50%

react-native-refreshable-listview's Introduction

React Native RefreshableListView

A pull-to-refresh ListView which shows a loading spinner while your data reloads

Deprecated: now you can use the built-in RefreshControl instead.

Build Status

In action (from ReactNativeHackerNews):

React Native Hacker News

Usage

Note: these are the docs for the 2.x branch, currently in beta. If you are looking for the docs for a 1.x version, see the 1.x branch.

You can install the latest beta with npm install react-native-refreshable-listview@next

RefreshableListView

Replace a ListView with a RefreshableListView to add pulldown-to-refresh functionality. Accepts the same props as ListView, plus a few extras (see the props definitions below).

var React = require('react-native')
var {Text, View, ListView} = React
var RefreshableListView = require('react-native-refreshable-listview')

var ArticleStore = require('../stores/ArticleStore')
var StoreWatchMixin = require('./StoreWatchMixin')
var ArticleView = require('./ArticleView')

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}) // assumes immutable objects

var ArticlesScreen = React.createClass({
  mixins: [StoreWatchMixin],
  getInitialState() {
    return {dataSource: ds.cloneWithRows(ArticleStore.all())}
  },
  getStoreWatches() {
    this.watchStore(ArticleStore, () => {
      this.setState({dataSource: ds.cloneWithRows(ArticleStore.all())})
    })
  },
  reloadArticles() {
    // returns a Promise of reload completion
    // for a Promise-free version see ControlledRefreshableListView below
    return ArticleStore.reload()
  },
  renderArticle(article) {
    return <ArticleView article={article} />
  },
  render() {
    return (
      <RefreshableListView
        dataSource={this.state.dataSource}
        renderRow={this.renderArticle}
        loadData={this.reloadArticles}
        refreshDescription="Refreshing articles"
      />
    )
  }
})

Props

  • loadData: func.isRequired A function returning a Promise or taking a callback, invoked upon pulldown. The refreshing indicator (spinner) will show until the Promise resolves or the callback is called.
  • refreshDescription: oneOfType([string, element]) Text/element to show alongside spinner. If a custom refreshingIndicatorComponent is used this value will be passed as its description prop.
  • refreshingIndicatorComponent: oneOfType([func, element]) Content to show in list header when refreshing. Can be a component class or instantiated element. Defaults to RefreshableListView.RefreshingIndicator. You can easily customise the appearance of the indicator by passing in a customised <RefreshableListView.RefreshingIndicator />, or provide your own entirely custom content to be displayed.
  • minDisplayTime: number Minimum time the spinner will show for.
  • minBetweenTime: number Minimum time after a refresh before another refresh can be performed.
  • minPulldownDistance: number Minimum distance (in px) which the list has to be scrolled off the top to trigger a refresh.
  • ignoreInertialScroll: bool Require the user to be actually touching the screen when the pulldown distance exceeds minPulldownDistance to trigger a refresh (eg. not just inertially scrolling off the top). Defaults to true.
  • onScroll: func An event handler for the onScroll event which will be chained after the one defined by the RefreshableListView.
  • scrollEventThrottle: number How often ListView produces scroll events, in ms. Defaults to a fairly low value, try setting it higher if you encounter performance issues. Keep in mind that a higher value will make the pulldown-to-refresh behaviour less responsive.
  • colors: array of strings Colors to be used for pull to refresh indicator in Android
  • progressBackgroundColor: string Color to be used for pull to refresh indicator background in Android

ControlledRefreshableListView

Low level component used by RefreshableListView. Use this directly if you want to manually control the refreshing status (rather than using a Promise).

This component is more suitable for use in a Redux-style connected component.

var React = require('react-native')
var {Text, View, ListView} = React
var {ControlledRefreshableListView} = require('react-native-refreshable-listview')

var ArticleView = require('./ArticleView')

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}) // assumes immutable objects

var ArticlesScreen = React.createClass({
  propTypes: {
    // eg. props mapped from store state
    articles: React.PropTypes.array.isRequired,
    isRefreshingArticles: React.PropTypes.bool.isRequired,
    // eg. a bound action creator
    refreshArticles: React.PropTypes.func.isRequired,
  },
  getInitialState() {
    return {dataSource: ds.cloneWithRows(this.props.articles)}
  },
  componentWillReceiveProps(nextProps) {
    if (this.props.articles !== nextProps.articles) {
      this.setState({dataSource: ds.cloneWithRows(nextProps.articles)})
    }
  },
  renderArticle(article) {
    return <ArticleView article={article} />
  },
  render() {
    return (
      <ControlledRefreshableListView
        dataSource={this.state.dataSource}
        renderRow={this.renderArticle}
        isRefreshing={this.props.isRefreshingArticles}
        onRefresh={this.props.refreshArticles}
        refreshDescription="Refreshing articles"
      />
    )
  }
})

Props

  • onRefresh: func.isRequired Called when user pulls listview down to refresh.
  • isRefreshing: bool.isRequired Whether or not to show the refreshing indicator.
  • refreshDescription: oneOfType([string, element]) See RefreshableListView
  • refreshingIndicatorComponent: oneOfType([func, element]) See RefreshableListView
  • minPulldownDistance: number See RefreshableListView
  • ignoreInertialScroll: bool See RefreshableListView
  • onScroll: func See RefreshableListView
  • scrollEventThrottle: number See RefreshableListView

RefreshableListView.RefreshingIndicator

Component with activity indicator to be displayed in list header when refreshing. (also exposed as ControlledRefreshableListView.RefreshingIndicator)

Props

  • description: oneOfType([string, element]) Text/element to show alongside spinner.
  • stylesheet: object A stylesheet object which overrides one or more of the styles defined in the RefreshingIndicator stylesheet.
  • activityIndicatorComponent: oneOfType([func, element]) The spinner to display. Defaults to <ActivityIndicatorIOS />.

RefreshableListView.DataSource, ControlledRefreshableListView.DataSource

Aliases of ListView.DataSource, for convenience.

Howto

Customise the refresh indicator (spinner)

Your first option is to style the default RefreshingIndicator:

var indicatorStylesheet = StyleSheet.create({
  wrapper: {
    backgroundColor: 'red',
    height: 60,
    marginTop: 10,
  },
  content: {
    backgroundColor: 'blue',
    marginTop: 10,
    height: 60,
  },
})

<RefreshableListView
  refreshingIndicatorComponent={
    <RefreshableListView.RefreshingIndicator stylesheet={indicatorStylesheet} />
  }
/>

Alternatively, you can provide a custom RefreshingIndicator:

var MyRefreshingIndicator = React.createClass({
  render() {
    return (
      <View>
        <MySpinner />
        <Text>{this.props.description}</Text>
      </View>
    )
  },
})

<RefreshableListView refreshingIndicatorComponent={MyRefreshingIndicator} />
// or
<RefreshableListView refreshingIndicatorComponent={<MyRefreshingIndicator />} />

Changelog

  • 2.0.0-beta4

    • added Android support! (@maraujop)
  • 2.0.0-beta3

    • fixed proptype warnings from internal component
    • adjusted default styling of refreshing indicator
  • 2.0.0-beta2

    • pulling down now reveals refreshing indicator behind current view, rather than the refreshing indicator moving down from the top of the screen
    • renderHeaderWrapper is no longer used
    • fixed infinite refreshing when pulled down
  • 1.2.0

    • deprecated renderHeader in favour of renderHeaderWrapper as some developers seemed to be confused by the fact that a renderHeader handler for a standard ListView will not automatically just work with this component, but rather needs to be modified as described in the documentation. The new prop renderHeaderWrapper works identically to the previous one, however hopefully now it is named differently it will be more apparent that its behaviour is not the same as with ListView. The renderHeader prop will be removed in 2.0.
  • 1.1.0

    • added behaviour to ignore inertial scrolling (@dhrrgn)
    • exposed props: ignoreInertialScroll, scrollEventThrottle
  • 1.0.0

    • Split RefreshableListView into 3 parts:
      • RefreshableListView handles 'refreshing' state by invoking 'loadData' callback and waiting for resolution.
      • ControlledRefreshableListView handles rendering of ListView header, depending on isRefreshing prop. Calls onRefresh handler when pulldown-to-refresh scroll motion occurs.
      • RefreshingIndicator is the component rendered in the header of the ListView when refreshing. Pass in a customised version of this (or a completely different component) to RefreshableListView or ControlledRefreshableListView if you want to customise refresh indicator appearance.
    • Added Jest unit tests
  • 0.3.0 added minPulldownTime & minBetweenTime props, fixed bug where refresh could happen twice

  • 0.2.0 added support for ListView props setNativeProps and getScrollResponder (@almost & @sscotth)

react-native-refreshable-listview's People

Contributors

alexrabarts avatar almost avatar atom2ueki avatar dhrrgn avatar jsdf avatar maraujop avatar mauron85 avatar sscotth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-refreshable-listview's Issues

2.0.0-beta4 - second pull to refresh in a row acts weirdly and slowly on iOS

When I pull to refresh first time - it runs nice and smoothly.
Then I pull again to refresh and list freezes for a second, then return back to top, and scroll again down with activity indicator. After update list keeps scrolled little bellow.

In previous versions (I mean 1.x.x) it's working fine.

please fix

can not build the project

[tid:com.facebook.React.JavaScript] Running application "RefreshableListViewTest" with appParams: {"rootTag":1,"initialProps":{}}. DEV === false, development-level warning are OFF, performance optimizations are ON

Why uses promise?

Hi,

I'm just wondering why we need to use promise when invoke loadData?
I thought in React, the better way to do this is via Flux + Store + State. For example, we have a "loading" state, and let the RefreshableListview decides whether to show spinnger according to that state.
In that way, things become more easy to handle. Otherwise, the promising will introduce extra "async" behavior to the view, which in my humble opinion is not the best way.

How do you think?
Thanks

2.0.0beta4

the scrollToCompat.js has a little bug, that is
function scrollToCompat(scrollResponder, y, x, animated = false) { if (isLegacyReactNative()) { scrollResponder.scrollTo(y, x, animated) } else { scrollResponder.scrollTo({x, y, animated}) } }
'animate' to 'animated',because the official doc need {x:x,y:y,animated:true}

Background Color

It'd be nice either to default to transparent or allow to change the white background color behind the spinner:
screen shot 2015-04-30 at 4 48 31 pm

Also how do you align the spinner vertically in the middle?

render header method

if I add a renderHeader method in RefreshableListView component, the refreshable will not work, is this a bug ?

How to trigger loadData by on release ?

is there a function or way to control load data only after release on pull action ? it need able to working with minPulldownDistance so that the data only refresh when hit both conditions

refreshingIndictatorComponent, a typo?

is refreshingIndictatorComponent a typo?, shouldn't it be refreshingIndicatorComponent ?, I know it's a stupid issue, but I had some debugging time while using the feature, so I thought I could comment on this. :-)

Cheers

Navigation Breaks Images

Assuming you have a RefreshableListView as the root component. If you push components two levels deep in the navigator and pop back to root any images in the RefreshableListView no longer get updated if you change the image URL on the state.

              <Image
                source={{uri: rowData.posters.thumbnail}}
                style={styles.cellImage} />

if you change rowData.posters.thumbnail image does not update anymore.

The pull to refresh logic is broken

This line

if (!(this.props.ignoreInertialScroll && this.isTouching)) {

Should be

if (!this.props.ignoreInertialScroll || this.isTouching) {

Another issue is that if I pull and hold, It will load the list multiple times.

RN V0.28 Native module cannot be null

I upgrade my RN from 0.24 to 0.28 ,and a redbox come up when I require your library, I just comment the DataSource,i works! ,I have tried something and I find I can't do this such as:var DataSource = ListView.DataSource, maybe this is RNV0.28 bug? I waste one day to find this.....from the entire project by commenting my code.....so I hope anyone who use this library can save his time... :)

Pass on contentContainerStyle

It should be possible to set the custom content container styling for the List/ScrollView from the outside.
Although the props are copied over the contentContainerStyle is set again later and therefore overwriting it. IMHO it should be enough to simply change (works perfectly fine):
contentContainerStyle={this.getContentContainerStyle()}
to
contentContainerStyle={[this.getContentContainerStyle(), this.props.contentContainerStyle]}

in ControlledRefreshableListView without adding a separate prop as the ListView doesn't expose the prop either but instead simply copies it down to the ScrollView.

backgroundColor of listview

When the listview is pulled down a white background is exposed. Is there a way to set this color? I tried defining it in a stylesheet but that didn't seem to work.

Can only get 2.0.0-beta2

Can only get the second version of the beta using the given npm command. Anyway to get beta4?
Thanks.

2 listview in TabBarItemIOS

2 listview in TabBarItemIOS, there is a bug maybe. when pull down,the listview would be reload text,but,the image can't change

Is this compatible with react-native 0.20?

Hi,

I am using react-native 0.20 and I keep getting this error:
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of xxxx.

Has anyone had the same problem?

refreshingIndicator is empty on renderHeaderWrapper function

Hello. First of all, nice plugin. :)

I have the following code:

<RefreshableListView
          ref="PostView"
          renderHeaderWrapper={this.renderHeader}

My renderHeader function looks like this:

renderHeader: function(refreshingIndicator) {
    console.log(refreshingIndicator);

However, my console log returns null. Got any idea why? Am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.