|
1 | 1 | import React from 'react'; |
2 | 2 | import TablePaginationPage from './TablePaginationPage'; |
3 | 3 |
|
4 | | -class TablePagination extends React.Component { |
5 | | - constructor(props) { |
6 | | - super(props); |
7 | | - this.state = { |
8 | | - previousDisabled: true, |
9 | | - nextDisabled: false, |
10 | | - }; |
11 | | - } |
| 4 | +const TablePagination = (props) => { |
| 5 | + const handleClick = (page) => { |
| 6 | + if (page >= 0 && page < props.pageCount) { |
| 7 | + props.changeHandler(page); |
| 8 | + } |
| 9 | + }; |
| 10 | + |
| 11 | + const hasPages = () => { |
| 12 | + return props.pageCount > 0; |
| 13 | + }; |
| 14 | + |
| 15 | + const renderPrevious = (paginations) => { |
| 16 | + paginations.push(<button key='0' onClick={handleClick.bind(this, props.currentPage - 1)}><i>{'<'}</i></button>); |
| 17 | + }; |
12 | 18 |
|
13 | | - handleClick(page) { |
14 | | - if (page >= 0 && page < this.props.pageCount) { |
15 | | - this.props.changeHandler(page); |
| 19 | + const renderNext = (paginations) => { |
| 20 | + paginations.push(<button onClick={handleClick.bind(this, props.currentPage + 1)} key={props.pageCount + 1}><i>{'>'}</i></button>); |
| 21 | + }; |
| 22 | + |
| 23 | + const renderLeftSidePagination = (current, paginations) => { |
| 24 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={current} pageNumber= {current} changeHandler={handleClick.bind(this, current - 1)}/>); |
| 25 | + }; |
| 26 | + |
| 27 | + const renderMiddleSidePagination = (current, paginations) => { |
| 28 | + if (current - 2 != 1 && current - 3 != 1) { |
| 29 | + paginations.push('...'); |
| 30 | + } |
| 31 | + if (current === props.pageCount && props.pageCount > 3) { |
| 32 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={current - 2} pageNumber= {current - 2} changeHandler={handleClick.bind(this, current - 3)}/>); |
16 | 33 | } |
17 | | - this.props.currentPage === 0 ? this.setState({ previousDisabled: true }) : this.setState({ previousDisabled: false }); |
18 | | - this.props.currentPage == this.props.pageCount - 2 ? this.setState({ nextDisabled: true }) : this.setState({ nextDisabled: false }); |
19 | | - } |
| 34 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={current - 1} pageNumber= {current - 1} changeHandler={handleClick.bind(this, current - 2)}/>); |
| 35 | + }; |
20 | 36 |
|
21 | | - renderPagination() { |
| 37 | + const renderRightSidePagination = (current, paginations) => { |
| 38 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={current + 1} pageNumber= {current + 1} changeHandler={handleClick.bind(this, current)}/>); |
| 39 | + if (current == 1 && props.pageCount > 3) { |
| 40 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={current + 2}pageNumber= {current + 2} changeHandler={handleClick.bind(this, current + 1)}/>); |
| 41 | + } |
| 42 | + if (current + 1 != props.pageCount - 1 && current + 2 != props.pageCount - 1) { |
| 43 | + paginations.push('...'); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const renderPagination = () => { |
22 | 48 | let paginations = []; |
23 | | - let current = this.props.currentPage + 1; |
24 | | - let allPages = this.props.pageCount; |
25 | | - if (allPages == 0) { |
| 49 | + let current = props.currentPage + 1; |
| 50 | + let allPages = props.pageCount; |
| 51 | + if (!hasPages()) { |
26 | 52 | return; |
27 | 53 | } |
28 | 54 |
|
29 | 55 | if (current > 1) { |
30 | | - paginations.push(<button disabled={this.props.currentPage === 0 ? this.state.previousDisabled : ''} key='0' onClick={this.handleClick.bind(this, this.props.currentPage - 1)}><i>{'<'}</i></button>); |
| 56 | + renderPrevious(paginations); |
31 | 57 | } |
32 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key='1' pageNumber= {1} changeHandler={this.handleClick.bind(this, 0)}/>); |
| 58 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key='1' pageNumber= {1} changeHandler={handleClick.bind(this, 0)}/>); |
| 59 | + |
33 | 60 | if (current > 2) { |
34 | | - if (current - 2 != 1 && current - 3 != 1) { |
35 | | - paginations.push('...'); |
36 | | - } |
37 | | - if (current === allPages && allPages > 3) { |
38 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={current - 2} pageNumber= {current - 2} changeHandler={this.handleClick.bind(this, current - 3)}/>); |
39 | | - } |
40 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={current - 1} pageNumber= {current - 1} changeHandler={this.handleClick.bind(this, current - 2)}/>); |
| 61 | + renderMiddleSidePagination(current, paginations); |
41 | 62 | } |
42 | 63 | if (current != 1 && current != allPages) { |
43 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={current} pageNumber= {current} changeHandler={this.handleClick.bind(this, current - 1)}/>); |
| 64 | + renderLeftSidePagination(current, paginations); |
44 | 65 | } |
45 | 66 | if (current < allPages - 1) { |
46 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={current + 1} pageNumber= {current + 1} changeHandler={this.handleClick.bind(this, current)}/>); |
47 | | - if (current == 1 && allPages > 3) { |
48 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={current + 2}pageNumber= {current + 2} changeHandler={this.handleClick.bind(this, current + 1)}/>); |
49 | | - } |
50 | | - if (current + 1 != allPages - 1 && current + 2 != allPages - 1) { |
51 | | - paginations.push('...'); |
52 | | - } |
| 67 | + renderRightSidePagination(current, paginations); |
53 | 68 | } |
54 | | - paginations.push(<TablePaginationPage currentPage={this.props.currentPage} key={allPages} pageNumber= {allPages} changeHandler={this.handleClick.bind(this, allPages - 1)}/>); |
| 69 | + paginations.push(<TablePaginationPage currentPage={props.currentPage} key={allPages} pageNumber= {allPages} changeHandler={handleClick.bind(this, allPages - 1)}/>); |
55 | 70 | if (current < allPages) |
56 | | - paginations.push(<button onClick={this.handleClick.bind(this, this.props.currentPage + 1)} key={allPages + 1} disabled={this.state.nextDisabled}><i>{'>'}</i></button>); |
| 71 | + renderNext(paginations); |
| 72 | + |
57 | 73 | return paginations; |
58 | | - } |
| 74 | + }; |
59 | 75 |
|
60 | | - render() { |
61 | | - return ( |
62 | | - <div className="table-pagination"> |
63 | | - {this.renderPagination()} |
64 | | - </div> |
65 | | - ); |
66 | | - } |
67 | | -} |
68 | | -export default TablePagination; |
| 76 | + return ( |
| 77 | + <div className="table-pagination"> |
| 78 | + {renderPagination()} |
| 79 | + </div> |
| 80 | + ); |
| 81 | +}; export default TablePagination; |
0 commit comments