|
| 1 | +import React from 'react'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +import BootstrapMixin from './BootstrapMixin'; |
| 4 | +import PaginationButton from './PaginationButton'; |
| 5 | + |
| 6 | +const Pagination = React.createClass({ |
| 7 | + mixins: [BootstrapMixin], |
| 8 | + |
| 9 | + propTypes: { |
| 10 | + activePage: React.PropTypes.number, |
| 11 | + items: React.PropTypes.number, |
| 12 | + maxButtons: React.PropTypes.number, |
| 13 | + ellipsis: React.PropTypes.bool, |
| 14 | + first: React.PropTypes.bool, |
| 15 | + last: React.PropTypes.bool, |
| 16 | + prev: React.PropTypes.bool, |
| 17 | + next: React.PropTypes.bool, |
| 18 | + onSelect: React.PropTypes.func |
| 19 | + }, |
| 20 | + |
| 21 | + getDefaultProps() { |
| 22 | + return { |
| 23 | + activePage: 1, |
| 24 | + items: 1, |
| 25 | + maxButtons: 0, |
| 26 | + first: false, |
| 27 | + last: false, |
| 28 | + prev: false, |
| 29 | + next: false, |
| 30 | + ellipsis: true, |
| 31 | + bsClass: 'pagination' |
| 32 | + }; |
| 33 | + }, |
| 34 | + |
| 35 | + renderPageButtons() { |
| 36 | + let pageButtons = []; |
| 37 | + let startPage, endPage, hasHiddenPagesBefore, hasHiddenPagesAfter; |
| 38 | + let { |
| 39 | + maxButtons, |
| 40 | + activePage, |
| 41 | + items, |
| 42 | + onSelect, |
| 43 | + ellipsis |
| 44 | + } = this.props; |
| 45 | + |
| 46 | + if(maxButtons){ |
| 47 | + let hiddenPagesBefore = activePage - parseInt(maxButtons / 2); |
| 48 | + startPage = hiddenPagesBefore > 1 ? hiddenPagesBefore : 1; |
| 49 | + hasHiddenPagesAfter = startPage + maxButtons <= items; |
| 50 | + |
| 51 | + if(!hasHiddenPagesAfter){ |
| 52 | + endPage = items; |
| 53 | + startPage = items - maxButtons + 1; |
| 54 | + } else { |
| 55 | + endPage = startPage + maxButtons - 1; |
| 56 | + } |
| 57 | + } else { |
| 58 | + startPage = 1; |
| 59 | + endPage = items; |
| 60 | + } |
| 61 | + |
| 62 | + for(let pagenumber = startPage; pagenumber <= endPage; pagenumber++){ |
| 63 | + pageButtons.push( |
| 64 | + <PaginationButton |
| 65 | + key={pagenumber} |
| 66 | + eventKey={pagenumber} |
| 67 | + active={pagenumber === activePage} |
| 68 | + onSelect={onSelect}> |
| 69 | + {pagenumber} |
| 70 | + </PaginationButton> |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + if(maxButtons && hasHiddenPagesAfter && ellipsis){ |
| 75 | + pageButtons.push( |
| 76 | + <PaginationButton |
| 77 | + key='ellipsis' |
| 78 | + disabled> |
| 79 | + <span aria-label='More'>...</span> |
| 80 | + </PaginationButton> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return pageButtons; |
| 85 | + }, |
| 86 | + |
| 87 | + renderPrev() { |
| 88 | + if(!this.props.prev){ |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return ( |
| 93 | + <PaginationButton |
| 94 | + key='prev' |
| 95 | + eventKey={this.props.activePage - 1} |
| 96 | + disabled={this.props.activePage === 1} |
| 97 | + onSelect={this.props.onSelect}> |
| 98 | + <span aria-label='Previous'>‹</span> |
| 99 | + </PaginationButton> |
| 100 | + ); |
| 101 | + }, |
| 102 | + |
| 103 | + renderNext() { |
| 104 | + if(!this.props.next){ |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + <PaginationButton |
| 110 | + key='next' |
| 111 | + eventKey={this.props.activePage + 1} |
| 112 | + disabled={this.props.activePage === this.props.items} |
| 113 | + onSelect={this.props.onSelect}> |
| 114 | + <span aria-label='Next'>›</span> |
| 115 | + </PaginationButton> |
| 116 | + ); |
| 117 | + }, |
| 118 | + |
| 119 | + renderFirst() { |
| 120 | + if(!this.props.first){ |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + return ( |
| 125 | + <PaginationButton |
| 126 | + key='first' |
| 127 | + eventKey={1} |
| 128 | + disabled={this.props.activePage === 1 } |
| 129 | + onSelect={this.props.onSelect}> |
| 130 | + <span aria-label='First'>«</span> |
| 131 | + </PaginationButton> |
| 132 | + ); |
| 133 | + }, |
| 134 | + |
| 135 | + renderLast() { |
| 136 | + if(!this.props.last){ |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + return ( |
| 141 | + <PaginationButton |
| 142 | + key='last' |
| 143 | + eventKey={this.props.items} |
| 144 | + disabled={this.props.activePage === this.props.items} |
| 145 | + onSelect={this.props.onSelect}> |
| 146 | + <span aria-label='Last'>»</span> |
| 147 | + </PaginationButton> |
| 148 | + ); |
| 149 | + }, |
| 150 | + |
| 151 | + render() { |
| 152 | + let classes = this.getBsClassSet(); |
| 153 | + return ( |
| 154 | + <ul |
| 155 | + {...this.props} |
| 156 | + className={classNames(this.props.className, classes)}> |
| 157 | + {this.renderFirst()} |
| 158 | + {this.renderPrev()} |
| 159 | + {this.renderPageButtons()} |
| 160 | + {this.renderNext()} |
| 161 | + {this.renderLast()} |
| 162 | + </ul> |
| 163 | + ); |
| 164 | + } |
| 165 | +}); |
| 166 | + |
| 167 | +export default Pagination; |
0 commit comments