Giter Site home page Giter Site logo

dozoisch / react-variable-height-infinite-scroller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tnrich/react-variable-height-infinite-scroller

0.0 0.0 0.0 843 KB

variable row height scroller (no precomputation of row height necessary)

JavaScript 99.97% HTML 0.03%

react-variable-height-infinite-scroller's Introduction

react-variable-height-infinite-scroller

An infinite scroller especially made for variable row heights (no precomputation of row height necessary).

See a Demo

Why

Because sometimes you don't know the size of the rows you're going to render before rendering

Install:

npm i --save react-variable-height-infinite-scroller

Usage:

Props

Name Default Description
averageElementHeight number required This is a guess you make of what is the average height. This is used to approximate number of rows when rendering more or less rows
containerHeight number required Maximum height of the scroll container
preloadRowStart number required If you want to start at a particular row to begin with
totalNumberOfRows number required Length of the data array
renderRow function required Function to render a row
rowToJumpTo (optional) Object of shape { row: Number }. Row you want to jump to. Must be passed as a new object each time to allow for difference checking
containerClassName string (optional) infiniteContainer className to apply on container
onScroll function (optional) no-op Hook to call on scroll

Taken from the demo code:

import React from 'react';
import InfiniteScroller from './InfiniteScroller.js';

function getFakeRowsWithHeights(numberOfRows) {
  let newFakeRows = [];
  for (let i = 0; i < numberOfRows; i++) {
    newFakeRows.push({height: Math.floor(1000 * Math.random())});
  }
  return newFakeRows;
}

const App = React.createClass({
  getNewRandomRow(totalRows) {
    return {row: Math.floor(totalRows * Math.random())};
  },

  getInitialState() {
    return {
      rowToJumpTo: null,
      newRowToJumpTo: this.getNewRandomRow(100),
      fakeRows: getFakeRowsWithHeights(100),
    };
  },
  render() {
    const newNumberOfRowsToDisplay = Math.floor(Math.random() * 200);
    return (
      <div overflow="scroll">
        <button onClick={() => {
          this.setState({
            rowToJumpTo: this.state.newRowToJumpTo,
            newRowToJumpTo: this.getNewRandomRow(this.state.fakeRows.length),
          });
        }}>
          Jump to a random row: Row #{this.state.newRowToJumpTo.row} (its height is {this.state.fakeRows[this.state.newRowToJumpTo.row].height})
        </button>
        <button onClick={() => {
          this.setState({
            fakeRows: getFakeRowsWithHeights(newNumberOfRowsToDisplay),
          });
        }}>
          Create {newNumberOfRowsToDisplay} new rows
        </button>
        <InfiniteScroller
          averageElementHeight={100} // this is a guess you make!
          containerHeight={600}
          rowToJumpTo={this.state.rowToJumpTo} // (optional) row you want to jump to. Must be passed as a new object each time to allow for difference checking
          renderRow={renderRow} // function to render a row
          totalNumberOfRows={this.state.fakeRows.length} // an array of data for your rows
          preloadRowStart={10} // if you want to start at a particular row to begin with
        />
      </div>
    );
  },

  renderRow(rowNumber) {
    const heightOfRow = this.state.fakeRows[rowNumber].height;
    return (
      <div
        key={rowNumber}
        style={{height: heightOfRow, background: heightOfRow % 2 === 0 ? 'red' : 'orange'}}
      >
        {heightOfRow}
      </div>
    );
  },
});

React.render(<App />, document.getElementById('container'));

Contributing

Changelog is now autogenerated. So commits have to be prefixed by one the four following prefixes:

  • [added] added a new feature
  • [changed] changed an existing feature
  • [fixed] fixed a bug
  • [removed] removed something or a file

Run npm test to lint

#Changelog: Changelog

react-variable-height-infinite-scroller's People

Contributors

tnrich avatar dozoisch avatar

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.