|
1 | | -/* Copyright 2015 Zeroarc Software, LLC |
2 | | -* |
3 | | -* Pager control |
4 | | -*/ |
| 1 | +//@flow |
| 2 | +// tabletable - Copyright 2017 Zeroarc Software, LLC |
| 3 | +'use strict'; |
5 | 4 |
|
6 | | -// External |
7 | | -let React = require('react'); |
8 | | -let ReactShallowCompare = require('react-addons-shallow-compare'); |
9 | | -let ClassNames = require('classnames'); |
10 | | -let Autobind = require('autobind-decorator'); |
| 5 | +const React = require('react'); |
| 6 | +const ReactShallowCompare = require('react-addons-shallow-compare'); |
| 7 | +const ClassNames = require('classnames'); |
| 8 | +const Autobind = require('autobind-decorator'); |
| 9 | + |
| 10 | +type Props = { |
| 11 | + displayPages: number, |
| 12 | + maxPage: number, |
| 13 | + currentPage: number, |
| 14 | + onPageChange: (page: number) => void |
| 15 | +}; |
11 | 16 |
|
12 | 17 | @Autobind |
13 | 18 | export default class TabletablePager extends React.Component { |
14 | | - shouldComponentUpdate(nextProps, nextState) { |
15 | | - return ReactShallowCompare(this, nextProps, nextState); |
| 19 | + props: Props; |
| 20 | + |
| 21 | + static defaultProps: { |
| 22 | + maxPage: number, |
| 23 | + currentPage: number, |
| 24 | + } |
| 25 | + |
| 26 | + shouldComponentUpdate(nextProps: Props) { |
| 27 | + return ReactShallowCompare(this, nextProps); |
16 | 28 | } |
17 | 29 |
|
18 | 30 | render() { |
19 | 31 | let options = []; |
20 | 32 |
|
21 | 33 | let startIndex = Math.max(this.props.currentPage - Math.floor(this.props.displayPages / 2), 1); |
22 | | - let endIndex = Math.min(startIndex + (this.props.displayPages - 1), this.props.maxPage); |
| 34 | + const endIndex = Math.min(startIndex + (this.props.displayPages - 1), this.props.maxPage); |
23 | 35 |
|
24 | 36 | if (this.props.maxPage >= this.props.displayPages && (endIndex - startIndex) <= this.props.displayPages) { |
25 | 37 | startIndex = endIndex - (this.props.displayPages - 1); |
26 | 38 | } |
27 | 39 |
|
28 | 40 | for(let i = startIndex; i <= endIndex; i++){ |
29 | | - let thisButtonClasses = ClassNames('btn', 'btn-white', 'btn-sm', { |
| 41 | + const thisButtonClasses = ClassNames('btn', 'btn-white', 'btn-sm', { |
30 | 42 | 'label-success': this.props.currentPage === i |
31 | 43 | }); |
32 | 44 | options.push(<button key={i} className={thisButtonClasses} data-value={i} onClick={this.pageChange}>{i}</button>); |
@@ -57,32 +69,32 @@ export default class TabletablePager extends React.Component { |
57 | 69 | // Custom methods |
58 | 70 | // |
59 | 71 |
|
60 | | - pageChange(e) { |
61 | | - e.stopPropagation(); |
| 72 | + pageChange(e: SyntheticInputEvent) { |
| 73 | + e.preventDefault(); |
62 | 74 | this.props.onPageChange(parseInt(e.target.getAttribute('data-value'))); |
63 | 75 | } |
64 | 76 |
|
65 | | - previousPageChange(e) { |
66 | | - e.stopPropagation(); |
| 77 | + previousPageChange(e: SyntheticInputEvent) { |
| 78 | + e.preventDefault(); |
67 | 79 | if (this.props.currentPage > 1) { |
68 | 80 | this.props.onPageChange(this.props.currentPage - 1); |
69 | 81 | } |
70 | 82 | } |
71 | 83 |
|
72 | | - nextPageChange(e) { |
73 | | - e.stopPropagation(); |
| 84 | + nextPageChange(e: SyntheticInputEvent) { |
| 85 | + e.preventDefault(); |
74 | 86 | if (this.props.currentPage < this.props.maxPage) { |
75 | 87 | this.props.onPageChange(this.props.currentPage + 1); |
76 | 88 | } |
77 | 89 | } |
78 | 90 |
|
79 | | - firstPageChange(e) { |
80 | | - e.stopPropagation(); |
| 91 | + firstPageChange(e: SyntheticInputEvent) { |
| 92 | + e.preventDefault(); |
81 | 93 | this.props.onPageChange(1); |
82 | 94 | } |
83 | 95 |
|
84 | | - lastPageChange(e) { |
85 | | - e.stopPropagation(); |
| 96 | + lastPageChange(e: SyntheticInputEvent) { |
| 97 | + e.preventDefault(); |
86 | 98 | this.props.onPageChange(this.props.maxPage); |
87 | 99 | } |
88 | 100 | } |
|
0 commit comments