|
| 1 | +/** |
| 2 | + * This file provided by Facebook is for non-commercial testing and evaluation |
| 3 | + * purposes only. Facebook reserves all rights not expressly granted. |
| 4 | + * |
| 5 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 6 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 7 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 8 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 9 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 10 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 11 | + */ |
| 12 | + |
| 13 | +"use strict"; |
| 14 | + |
| 15 | +var ExampleImage = require('./helpers/ExampleImage'); |
| 16 | +var FakeObjectDataListStore = require('./helpers/FakeObjectDataListStore'); |
| 17 | +var FixedDataTable = require('fixed-data-table'); |
| 18 | +var React = require('react'); |
| 19 | + |
| 20 | +const {Table, Column, Cell} = FixedDataTable; |
| 21 | + |
| 22 | +const TextCell = ({rowIndex, data, col, ...props}) => { |
| 23 | + var rowObject = data.getObjectAt(rowIndex); |
| 24 | + return ( |
| 25 | + <Cell {...props}> |
| 26 | + {rowObject ? rowObject[col] : 'pending'} |
| 27 | + </Cell> |
| 28 | + ); |
| 29 | +}; |
| 30 | + |
| 31 | +class PagedData { |
| 32 | + constructor(callback) { |
| 33 | + this._dataList = new FakeObjectDataListStore(2000); |
| 34 | + this._end = 50; |
| 35 | + this._pending = false; |
| 36 | + this._callback = callback; |
| 37 | + } |
| 38 | + |
| 39 | + getSize() { |
| 40 | + return 2000; |
| 41 | + } |
| 42 | + |
| 43 | + fetchRange(end) { |
| 44 | + if (this._pending) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + this._pending = true; |
| 49 | + return new Promise((resolve) => setTimeout(resolve, 1000)) |
| 50 | + .then(() => { |
| 51 | + this._pending = false; |
| 52 | + this._end = end; |
| 53 | + this._callback(end) |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + getObjectAt(index) { |
| 58 | + if (index >= this._end) { |
| 59 | + this.fetchRange(Math.min(2000, index + 50)); |
| 60 | + return null; |
| 61 | + } |
| 62 | + return this._dataList.getObjectAt(index); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class PaginationExample extends React.Component { |
| 67 | + constructor(props) { |
| 68 | + super(props); |
| 69 | + |
| 70 | + this._updateData = this._updateData.bind(this); |
| 71 | + this.state = { |
| 72 | + pagedData: new PagedData(this._updateData), |
| 73 | + end: 50 |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + //Just need to force a refresh |
| 78 | + _updateData(end) { |
| 79 | + this.setState({ |
| 80 | + end: end |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + render() { |
| 85 | + var {pagedData} = this.state; |
| 86 | + |
| 87 | + return ( |
| 88 | + <div> |
| 89 | + <Table |
| 90 | + rowHeight={50} |
| 91 | + rowsCount={pagedData.getSize()} |
| 92 | + headerHeight={50} |
| 93 | + width={1000} |
| 94 | + height={500} |
| 95 | + {...this.props}> |
| 96 | + <Column |
| 97 | + header={<Cell></Cell>} |
| 98 | + cell={({rowIndex}) => (<Cell>{rowIndex}</Cell>)} |
| 99 | + fixed={true} |
| 100 | + width={50} |
| 101 | + /> |
| 102 | + <Column |
| 103 | + header={<Cell>First Name</Cell>} |
| 104 | + cell={<TextCell data={pagedData} col="firstName" />} |
| 105 | + fixed={true} |
| 106 | + width={100} |
| 107 | + /> |
| 108 | + <Column |
| 109 | + header={<Cell>Last Name</Cell>} |
| 110 | + cell={<TextCell data={pagedData} col="lastName" />} |
| 111 | + fixed={true} |
| 112 | + width={100} |
| 113 | + /> |
| 114 | + <Column |
| 115 | + header={<Cell>City</Cell>} |
| 116 | + cell={<TextCell data={pagedData} col="city" />} |
| 117 | + width={100} |
| 118 | + /> |
| 119 | + <Column |
| 120 | + header={<Cell>Street</Cell>} |
| 121 | + cell={<TextCell data={pagedData} col="street" />} |
| 122 | + width={200} |
| 123 | + /> |
| 124 | + <Column |
| 125 | + header={<Cell>Zip Code</Cell>} |
| 126 | + cell={<TextCell data={pagedData} col="zipCode" />} |
| 127 | + width={200} |
| 128 | + /> |
| 129 | + </Table> |
| 130 | + </div> |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = PaginationExample; |
0 commit comments