|
| 1 | +import classNames from "classnames"; |
| 2 | +import React, { ReactNode, Component } from "react"; |
| 3 | +import "./Pagination.scss"; |
| 4 | +import { PaginationProps } from "./PaginationProps"; |
| 5 | +import { PaginationState } from "./PaginationState"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Component which will display pagination. |
| 9 | + */ |
| 10 | +class Pagination extends Component<PaginationProps, PaginationState> { |
| 11 | + /** |
| 12 | + * Dots for pagination. |
| 13 | + */ |
| 14 | + private static readonly DOTS: string = "..."; |
| 15 | + |
| 16 | + /** |
| 17 | + * Is the component mounted. |
| 18 | + */ |
| 19 | + private _isMounted?: boolean; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new instance of Pagination. |
| 23 | + * @param props The props. |
| 24 | + */ |
| 25 | + constructor(props: PaginationProps) { |
| 26 | + super(props); |
| 27 | + this.state = { |
| 28 | + paginationRange: [], |
| 29 | + lastPage: 0, |
| 30 | + isMobile: false |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The component updated. |
| 36 | + * @param prevProps previous props |
| 37 | + */ |
| 38 | + public componentDidUpdate(prevProps: PaginationProps): void { |
| 39 | + if (this.props !== prevProps) { |
| 40 | + this.setState( |
| 41 | + { paginationRange: this.updatePaginationRange() }, |
| 42 | + () => this.setState( |
| 43 | + { lastPage: this.state.paginationRange[this.state.paginationRange.length - 1] as number } |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * The component mounted. |
| 51 | + */ |
| 52 | + public componentDidMount(): void { |
| 53 | + this._isMounted = true; |
| 54 | + window.addEventListener("resize", this.resize.bind(this)); |
| 55 | + this.resize(); |
| 56 | + } |
| 57 | + |
| 58 | + public resize() { |
| 59 | + const isMobileViewPort = window.innerWidth < 768; |
| 60 | + |
| 61 | + if (this.state.isMobile !== isMobileViewPort && this._isMounted) { |
| 62 | + this.setState( |
| 63 | + { isMobile: isMobileViewPort }, |
| 64 | + () => this.setState({ paginationRange: this.updatePaginationRange() }) |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * The component will unmounted. |
| 71 | + */ |
| 72 | + public async componentWillUnmount(): Promise<void> { |
| 73 | + this._isMounted = false; |
| 74 | + window.removeEventListener("resize", this.resize.bind(this)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Render the component. |
| 79 | + * @returns The node to render. |
| 80 | + */ |
| 81 | + public render(): ReactNode { |
| 82 | + return ( |
| 83 | + <ul |
| 84 | + className={classNames("pagination", { |
| 85 | + [this.props.classNames as string]: this.props.classNames !== undefined, |
| 86 | + hidden: (this.props.currentPage === 0 || this.state.paginationRange.length < 2) |
| 87 | + })} |
| 88 | + > |
| 89 | + <li |
| 90 | + className={classNames("pagination-item", { |
| 91 | + disabled: this.props.currentPage < 11, |
| 92 | + hidden: this.state.isMobile |
| 93 | + })} |
| 94 | + onClick={() => { |
| 95 | + this.props.onPageChange(this.props.currentPage - 10); |
| 96 | + }} |
| 97 | + > |
| 98 | + <div className="arrow left" /> |
| 99 | + <div className="arrow left" /> |
| 100 | + </li> |
| 101 | + <li |
| 102 | + className={classNames("pagination-item", { |
| 103 | + disabled: this.props.currentPage === 1 |
| 104 | + })} |
| 105 | + onClick={() => { |
| 106 | + this.props.onPageChange(this.props.currentPage - 1); |
| 107 | + }} |
| 108 | + > |
| 109 | + <div className="arrow left" /> |
| 110 | + </li> |
| 111 | + {this.state.paginationRange.map((pageNumber: (number|string), idx: number) => { |
| 112 | + if (pageNumber === Pagination.DOTS) { |
| 113 | + return <li key={idx} className="pagination-item dots">{pageNumber}</li>; |
| 114 | + } |
| 115 | + |
| 116 | + return ( |
| 117 | + <li |
| 118 | + key={idx} |
| 119 | + className={classNames("pagination-item", { |
| 120 | + selected: pageNumber === this.props.currentPage |
| 121 | + })} |
| 122 | + onClick={() => this.props.onPageChange(pageNumber as number)} |
| 123 | + > |
| 124 | + {pageNumber} |
| 125 | + </li> |
| 126 | + ); |
| 127 | + })} |
| 128 | + <li |
| 129 | + className={classNames("pagination-item", { |
| 130 | + disabled: this.props.currentPage === this.state.lastPage |
| 131 | + })} |
| 132 | + onClick={() => { |
| 133 | + this.props.onPageChange(this.props.currentPage + 1); |
| 134 | + }} |
| 135 | + > |
| 136 | + <div className="arrow right" /> |
| 137 | + </li> |
| 138 | + <li |
| 139 | + className={classNames("pagination-item", { |
| 140 | + disabled: this.props.currentPage > this.state.lastPage - 10, |
| 141 | + hidden: this.state.isMobile |
| 142 | + })} |
| 143 | + onClick={() => { |
| 144 | + this.props.onPageChange(this.props.currentPage + 10); |
| 145 | + }} |
| 146 | + > |
| 147 | + <div className="arrow right" /> |
| 148 | + <div className="arrow right" /> |
| 149 | + </li> |
| 150 | + </ul> |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * Update pagination range. |
| 157 | + * @returns The range of available pages. |
| 158 | + */ |
| 159 | + protected updatePaginationRange(): (string|number)[] { |
| 160 | + let paginationRange: (string|number)[] = []; |
| 161 | + |
| 162 | + const totalPageCount: number = Math.ceil(this.props.totalCount / this.props.pageSize); |
| 163 | + |
| 164 | + // Min page range is determined as siblingsCount + firstPage + lastPage + currentPage + 2*DOTS |
| 165 | + const minPageRangeCount: number = this.props.siblingsCount + 5; |
| 166 | + |
| 167 | + if (minPageRangeCount >= totalPageCount) { |
| 168 | + paginationRange = this.range(1, totalPageCount); |
| 169 | + } |
| 170 | + |
| 171 | + const leftSiblingIndex = Math.max(this.props.currentPage - this.props.siblingsCount, 1); |
| 172 | + const rightSiblingIndex = Math.min( |
| 173 | + this.props.currentPage + this.props.siblingsCount, |
| 174 | + totalPageCount |
| 175 | + ); |
| 176 | + |
| 177 | + /* |
| 178 | + * Do not show dots if there is only one position left |
| 179 | + * after/before the left/right page count. |
| 180 | + */ |
| 181 | + const shouldShowLeftDots = leftSiblingIndex > 2; |
| 182 | + const shouldShowRightDots = rightSiblingIndex < totalPageCount - 2; |
| 183 | + |
| 184 | + const firstPageIndex = 1; |
| 185 | + const lastPageIndex = totalPageCount; |
| 186 | + |
| 187 | + if (!shouldShowLeftDots && shouldShowRightDots) { |
| 188 | + const leftItemCount = 3 + (2 * this.props.siblingsCount); |
| 189 | + const leftRange = this.range(1, leftItemCount); |
| 190 | + |
| 191 | + paginationRange = [...leftRange, Pagination.DOTS, totalPageCount]; |
| 192 | + } |
| 193 | + |
| 194 | + if (shouldShowLeftDots && !shouldShowRightDots) { |
| 195 | + const rightItemCount = 3 + (2 * this.props.siblingsCount); |
| 196 | + const rightRange = this.range( |
| 197 | + totalPageCount - rightItemCount + 1, |
| 198 | + totalPageCount |
| 199 | + ); |
| 200 | + |
| 201 | + paginationRange = [firstPageIndex, Pagination.DOTS, ...rightRange]; |
| 202 | + } |
| 203 | + |
| 204 | + if (shouldShowLeftDots && shouldShowRightDots) { |
| 205 | + const middleRange = this.range(leftSiblingIndex, rightSiblingIndex); |
| 206 | + |
| 207 | + paginationRange = [firstPageIndex, Pagination.DOTS, ...middleRange, Pagination.DOTS, lastPageIndex]; |
| 208 | + } |
| 209 | + |
| 210 | + /* |
| 211 | + * Add extra range for large number of pages |
| 212 | + */ |
| 213 | + const rightRemainingPages = totalPageCount - (this.props.currentPage + this.props.siblingsCount); |
| 214 | + const leftRemainingPages = this.props.currentPage - this.props.siblingsCount; |
| 215 | + |
| 216 | + if (!this.state.isMobile && |
| 217 | + this.props.extraPageRangeLimit && |
| 218 | + rightRemainingPages > this.props.extraPageRangeLimit) { |
| 219 | + const remainderMidPoint = Math.floor((rightRemainingPages) / 2) + this.props.currentPage; |
| 220 | + const rMiddleRange: (string|number)[] = this.range(remainderMidPoint - 1, remainderMidPoint + 1); |
| 221 | + rMiddleRange.push(Pagination.DOTS); |
| 222 | + const lastItemIndex = paginationRange.length - 1; |
| 223 | + paginationRange.splice(lastItemIndex, 0, ...rMiddleRange); |
| 224 | + } |
| 225 | + |
| 226 | + if (!this.state.isMobile && |
| 227 | + this.props.extraPageRangeLimit && |
| 228 | + leftRemainingPages > this.props.extraPageRangeLimit) { |
| 229 | + const remainderMidPoint = Math.floor(leftRemainingPages / 2); |
| 230 | + const lMiddleRange: (string|number)[] = this.range(remainderMidPoint - 1, remainderMidPoint + 1); |
| 231 | + lMiddleRange.unshift(Pagination.DOTS); |
| 232 | + paginationRange.splice(1, 0, ...lMiddleRange); |
| 233 | + } |
| 234 | + |
| 235 | + return paginationRange; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Creates an array of elements from start value to end value. |
| 240 | + * @param start Start value. |
| 241 | + * @param end End value. |
| 242 | + * @returns Array of elements from start to end value. |
| 243 | + */ |
| 244 | + private range(start: number, end: number): number[] { |
| 245 | + const length = end - start + 1; |
| 246 | + return Array.from({ length }, (_, idx) => idx + start); |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +export default Pagination; |
0 commit comments