11/** @flow */
2- import React , { Component } from 'react'
2+ import React , { Component , PropTypes } from 'react'
33import { ContentBox , ContentBoxHeader , ContentBoxParagraph } from '../demo/ContentBox'
4+ import Immutable from 'immutable'
45import InfiniteLoader from './InfiniteLoader'
56import VirtualScroll from '../VirtualScroll'
67import styles from './InfiniteLoader.example.css'
@@ -9,6 +10,10 @@ const STATUS_LOADING = 1
910const STATUS_LOADED = 2
1011
1112export default class InfiniteLoaderExample extends Component {
13+ static propTypes = {
14+ list : PropTypes . instanceOf ( Immutable . List ) . isRequired
15+ }
16+
1217 constructor ( props ) {
1318 super ( props )
1419
@@ -17,6 +22,7 @@ export default class InfiniteLoaderExample extends Component {
1722 randomScrollToIndex : null
1823 }
1924
25+ this . _clearData = this . _clearData . bind ( this )
2026 this . _isRowLoaded = this . _isRowLoaded . bind ( this )
2127 this . _loadMoreRows = this . _loadMoreRows . bind ( this )
2228 this . _rowRenderer = this . _rowRenderer . bind ( this )
@@ -37,17 +43,28 @@ export default class InfiniteLoaderExample extends Component {
3743 />
3844
3945 < ContentBoxParagraph >
40- Coming soon...
46+ This component manages just-in-time data fetching to ensure that the all visible rows have been loaded.
47+ It also uses a threshold to determine how early to pre-fetch rows (before a user scrolls to them).
48+ </ ContentBoxParagraph >
49+
50+ < ContentBoxParagraph >
51+ < button
52+ className = { styles . button }
53+ onClick = { this . _clearData }
54+ >
55+ Flush Cached Data
56+ </ button >
4157 </ ContentBoxParagraph >
4258
4359 < InfiniteLoader
60+ ref = 'InfiniteLoader'
4461 isRowLoaded = { this . _isRowLoaded }
4562 loadMoreRows = { this . _loadMoreRows }
4663 rowsCount = { list . size }
4764 >
4865 < VirtualScroll
4966 className = { styles . VirtualScroll }
50- height = { 300 }
67+ height = { 200 }
5168 rowsCount = { list . size }
5269 rowHeight = { 30 }
5370 rowRenderer = { this . _rowRenderer }
@@ -58,6 +75,12 @@ export default class InfiniteLoaderExample extends Component {
5875 )
5976 }
6077
78+ _clearData ( ) {
79+ this . setState ( {
80+ loadedRowsMap : { }
81+ } )
82+ }
83+
6184 _isRowLoaded ( index ) {
6285 const { loadedRowsMap } = this . state
6386 return ! ! loadedRowsMap [ index ]
@@ -74,23 +97,29 @@ export default class InfiniteLoaderExample extends Component {
7497 for ( var i = startIndex ; i <= stopIndex ; i ++ ) {
7598 loadedRowsMap [ i ] = STATUS_LOADED
7699 }
100+
101+ promiseResolver ( )
77102 } , 1000 ) // TODO Randomize time?
103+
104+ let promiseResolver
105+
106+ return new Promise ( resolve => promiseResolver = resolve )
78107 }
79108
80109 _rowRenderer ( index ) {
81110 const { list } = this . props
82111 const { loadedRowsMap } = this . state
83112
113+ const row = list . get ( index )
84114 let content
85115
86116 if ( loadedRowsMap [ index ] === STATUS_LOADED ) {
87- const row = list . get ( index )
88117 content = row . name
89118 } else {
90119 content = (
91120 < div
92121 className = { styles . placeholder }
93- style = { { width : 100 + Math . round ( Math . random ( ) * 100 ) } }
122+ style = { { width : row . size } }
94123 />
95124 )
96125 }
0 commit comments