|
| 1 | +/* eslint react/no-multi-comp: 0 */ |
| 2 | +import React from 'react'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import BootstrapTable from 'react-bootstrap-table2'; |
| 5 | +import paginator from 'react-bootstrap-table2-paginator'; |
| 6 | +import Code from 'components/common/code-block'; |
| 7 | +import { productsGenerator } from 'utils/common'; |
| 8 | + |
| 9 | +const products = productsGenerator(87); |
| 10 | + |
| 11 | +const columns = [{ |
| 12 | + dataField: 'id', |
| 13 | + text: 'Product ID' |
| 14 | +}, { |
| 15 | + dataField: 'name', |
| 16 | + text: 'Product Name' |
| 17 | +}, { |
| 18 | + dataField: 'price', |
| 19 | + text: 'Product Price' |
| 20 | +}]; |
| 21 | + |
| 22 | +const sourceCode = `\ |
| 23 | +import BootstrapTable from 'react-bootstrap-table2'; |
| 24 | +import paginator from 'react-bootstrap-table2-paginator'; |
| 25 | +// ... |
| 26 | +const RemotePagination = ({ data, page, sizePerPage, onTableChange, totalSize }) => ( |
| 27 | + <div> |
| 28 | + <BootstrapTable |
| 29 | + remote |
| 30 | + keyField="id" |
| 31 | + data={ data } |
| 32 | + columns={ columns } |
| 33 | + pagination={ paginator({ page, sizePerPage, totalSize }) } |
| 34 | + onTableChange={ onTableChange } |
| 35 | + /> |
| 36 | + <Code>{ sourceCode }</Code> |
| 37 | + </div> |
| 38 | +); |
| 39 | +
|
| 40 | +class Container extends React.Component { |
| 41 | + constructor(props) { |
| 42 | + super(props); |
| 43 | + this.state = { |
| 44 | + page: 1, |
| 45 | + data: products.slice(0, 10), |
| 46 | + sizePerPage: 10 |
| 47 | + }; |
| 48 | + } |
| 49 | +
|
| 50 | + handleTableChange = ({ page, sizePerPage }) => { |
| 51 | + const currentIndex = (page - 1) * sizePerPage; |
| 52 | + setTimeout(() => { |
| 53 | + this.setState(() => ({ |
| 54 | + page, |
| 55 | + data: products.slice(currentIndex, currentIndex + sizePerPage), |
| 56 | + sizePerPage |
| 57 | + })); |
| 58 | + }, 2000); |
| 59 | + } |
| 60 | +
|
| 61 | + render() { |
| 62 | + const { data, sizePerPage, page } = this.state; |
| 63 | + return ( |
| 64 | + <RemotePagination |
| 65 | + data={ data } |
| 66 | + page={ page } |
| 67 | + sizePerPage={ sizePerPage } |
| 68 | + totalSize={ products.length } |
| 69 | + onTableChange={ this.handleTableChange } |
| 70 | + /> |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
| 74 | +`; |
| 75 | + |
| 76 | +const RemotePagination = ({ data, page, sizePerPage, onTableChange, totalSize }) => ( |
| 77 | + <div> |
| 78 | + <BootstrapTable |
| 79 | + remote |
| 80 | + keyField="id" |
| 81 | + data={ data } |
| 82 | + columns={ columns } |
| 83 | + pagination={ paginator({ page, sizePerPage, totalSize }) } |
| 84 | + onTableChange={ onTableChange } |
| 85 | + /> |
| 86 | + <Code>{ sourceCode }</Code> |
| 87 | + </div> |
| 88 | +); |
| 89 | + |
| 90 | +RemotePagination.propTypes = { |
| 91 | + data: PropTypes.array.isRequired, |
| 92 | + page: PropTypes.number.isRequired, |
| 93 | + totalSize: PropTypes.number.isRequired, |
| 94 | + sizePerPage: PropTypes.number.isRequired, |
| 95 | + onTableChange: PropTypes.func.isRequired |
| 96 | +}; |
| 97 | + |
| 98 | +class Container extends React.Component { |
| 99 | + constructor(props) { |
| 100 | + super(props); |
| 101 | + this.state = { |
| 102 | + page: 1, |
| 103 | + data: products.slice(0, 10), |
| 104 | + sizePerPage: 10 |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + handleTableChange = ({ page, sizePerPage }) => { |
| 109 | + const currentIndex = (page - 1) * sizePerPage; |
| 110 | + setTimeout(() => { |
| 111 | + this.setState(() => ({ |
| 112 | + page, |
| 113 | + data: products.slice(currentIndex, currentIndex + sizePerPage), |
| 114 | + sizePerPage |
| 115 | + })); |
| 116 | + }, 2000); |
| 117 | + } |
| 118 | + |
| 119 | + render() { |
| 120 | + const { data, sizePerPage, page } = this.state; |
| 121 | + return ( |
| 122 | + <RemotePagination |
| 123 | + data={ data } |
| 124 | + page={ page } |
| 125 | + sizePerPage={ sizePerPage } |
| 126 | + totalSize={ products.length } |
| 127 | + onTableChange={ this.handleTableChange } |
| 128 | + /> |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export default Container; |
0 commit comments