1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports [ "default" ] = Pagination ;
7+ var _react = require ( "react" ) ;
8+ var _appendURLParameter = _interopRequireDefault ( require ( "../util/appendURLParameter" ) ) ;
9+ var _excluded = [ "total" , "currentPage" , "itemsPerPage" , "setCurrentPage" ] ;
10+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { "default" : obj } ; }
11+ function _objectWithoutProperties ( source , excluded ) { if ( source == null ) return { } ; var target = _objectWithoutPropertiesLoose ( source , excluded ) ; var key , i ; if ( Object . getOwnPropertySymbols ) { var sourceSymbolKeys = Object . getOwnPropertySymbols ( source ) ; for ( i = 0 ; i < sourceSymbolKeys . length ; i ++ ) { key = sourceSymbolKeys [ i ] ; if ( excluded . indexOf ( key ) >= 0 ) continue ; if ( ! Object . prototype . propertyIsEnumerable . call ( source , key ) ) continue ; target [ key ] = source [ key ] ; } } return target ; }
12+ function _objectWithoutPropertiesLoose ( source , excluded ) { if ( source == null ) return { } ; var target = { } ; var sourceKeys = Object . keys ( source ) ; var key , i ; for ( i = 0 ; i < sourceKeys . length ; i ++ ) { key = sourceKeys [ i ] ; if ( excluded . indexOf ( key ) >= 0 ) continue ; target [ key ] = source [ key ] ; } return target ; }
13+ function _slicedToArray ( arr , i ) { return _arrayWithHoles ( arr ) || _iterableToArrayLimit ( arr , i ) || _unsupportedIterableToArray ( arr , i ) || _nonIterableRest ( ) ; }
14+ function _nonIterableRest ( ) { throw new TypeError ( "Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method." ) ; }
15+ function _unsupportedIterableToArray ( o , minLen ) { if ( ! o ) return ; if ( typeof o === "string" ) return _arrayLikeToArray ( o , minLen ) ; var n = Object . prototype . toString . call ( o ) . slice ( 8 , - 1 ) ; if ( n === "Object" && o . constructor ) n = o . constructor . name ; if ( n === "Map" || n === "Set" ) return Array . from ( o ) ; if ( n === "Arguments" || / ^ (?: U i | I ) n t (?: 8 | 1 6 | 3 2 ) (?: C l a m p e d ) ? A r r a y $ / . test ( n ) ) return _arrayLikeToArray ( o , minLen ) ; }
16+ function _arrayLikeToArray ( arr , len ) { if ( len == null || len > arr . length ) len = arr . length ; for ( var i = 0 , arr2 = new Array ( len ) ; i < len ; i ++ ) arr2 [ i ] = arr [ i ] ; return arr2 ; }
17+ function _iterableToArrayLimit ( arr , i ) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr [ Symbol . iterator ] || arr [ "@@iterator" ] ; if ( null != _i ) { var _s , _e , _x , _r , _arr = [ ] , _n = ! 0 , _d = ! 1 ; try { if ( _x = ( _i = _i . call ( arr ) ) . next , 0 === i ) { if ( Object ( _i ) !== _i ) return ; _n = ! 1 ; } else for ( ; ! ( _n = ( _s = _x . call ( _i ) ) . done ) && ( _arr . push ( _s . value ) , _arr . length !== i ) ; _n = ! 0 ) ; } catch ( err ) { _d = ! 0 , _e = err ; } finally { try { if ( ! _n && null != _i [ "return" ] && ( _r = _i [ "return" ] ( ) , Object ( _r ) !== _r ) ) return ; } finally { if ( _d ) throw _e ; } } return _arr ; } }
18+ function _arrayWithHoles ( arr ) { if ( Array . isArray ( arr ) ) return arr ; }
19+ var updatePageNumberInUrl = function updatePageNumberInUrl ( n ) {
20+ ( 0 , _appendURLParameter [ "default" ] ) ( "page" , n ) ;
21+ } ;
22+ function Pages ( _ref ) {
23+ var currentPage = _ref . currentPage ,
24+ totalPages = _ref . totalPages ,
25+ setCurrentPage = _ref . setCurrentPage ;
26+ var _useState = ( 0 , _react . useState ) ( [ ] ) ,
27+ _useState2 = _slicedToArray ( _useState , 2 ) ,
28+ pages = _useState2 [ 0 ] ,
29+ setPages = _useState2 [ 1 ] ;
30+ ( 0 , _react . useEffect ) ( function ( ) {
31+ var page = [ ] ;
32+ if ( totalPages > 5 ) {
33+ if ( currentPage !== 1 ) page . push ( 1 ) ;
34+ var middlePage = parseInt ( totalPages / 2 ) ;
35+ if ( middlePage === currentPage ) {
36+ page . push ( currentPage ) ;
37+ } else if ( middlePage > currentPage ) {
38+ page . push ( currentPage ) ;
39+ page . push ( middlePage ) ;
40+ } else {
41+ page . push ( middlePage ) ;
42+ page . push ( currentPage ) ;
43+ }
44+ if ( currentPage !== totalPages ) page . push ( totalPages ) ;
45+ } else {
46+ for ( var i = 1 ; i <= totalPages ; i ++ ) page . push ( i ) ;
47+ }
48+ var newPage = [ ] ;
49+ // eslint-disable-next-line array-callback-return
50+ page . map ( function ( n , i ) {
51+ /**
52+ * Nothing to do on last page
53+ */
54+ if ( i + 1 === page . length ) return newPage . push ( n ) ;
55+ /**
56+ * If current value and next value are siblings get original
57+ */
58+ if ( n + 1 === page [ i + 1 ] ) return newPage . push ( n ) ;
59+ newPage . push ( n ) ;
60+ newPage . push ( "..." ) ;
61+ } ) ;
62+ setPages ( newPage ) ;
63+ } , [ currentPage , totalPages ] ) ;
64+ return pages . map ( function ( n , i ) {
65+ if ( ! isNaN ( n ) ) {
66+ return /*#__PURE__*/ React . createElement ( "span" , {
67+ key : i ,
68+ onClick : function onClick ( ) {
69+ updatePageNumberInUrl ( n ) ;
70+ setCurrentPage ( n ) ;
71+ } ,
72+ className : "inline-block px-2 py-1 rounded-sm ml-1 select-none cursor-pointer " . concat ( currentPage === n ? "bg-gray-300 pointer-events-none" : "bg-gray-100" )
73+ } , n ) ;
74+ } else {
75+ return /*#__PURE__*/ React . createElement ( "span" , {
76+ key : i ,
77+ className : "inline-block px-2 py-1 tracking-wider"
78+ } , "..." ) ;
79+ }
80+ } ) ;
81+ }
82+ function Pagination ( _ref2 ) {
83+ var total = _ref2 . total ,
84+ currentPage = _ref2 . currentPage ,
85+ itemsPerPage = _ref2 . itemsPerPage ,
86+ setCurrentPage = _ref2 . setCurrentPage ,
87+ props = _objectWithoutProperties ( _ref2 , _excluded ) ;
88+ var totalPages = Math . ceil ( total / itemsPerPage ) ;
89+ var className = "inline-block px-2 py-1 font-bold cursor-pointer bg-emerald-300 rounded-sm text-gray-800 hover:bg-emerald-500 transition select-none" ;
90+ return totalPages > 1 && /*#__PURE__*/ React . createElement ( "div" , null , /*#__PURE__*/ React . createElement ( "span" , {
91+ className : currentPage === 1 ? "cursor-not-allowed" : ""
92+ } , /*#__PURE__*/ React . createElement ( "span" , {
93+ title : "previous page" ,
94+ onClick : function onClick ( ) {
95+ updatePageNumberInUrl ( currentPage - 1 ) ;
96+ setCurrentPage ( currentPage - 1 ) ;
97+ } ,
98+ className : "" . concat ( className , " " ) . concat ( currentPage === 1 ? "bg-emerald-100 pointer-events-none" : "" )
99+ } , "\u21FD" ) ) , /*#__PURE__*/ React . createElement ( Pages , {
100+ currentPage : currentPage ,
101+ totalPages : totalPages ,
102+ setCurrentPage : setCurrentPage
103+ } ) , /*#__PURE__*/ React . createElement ( "span" , {
104+ className : currentPage === totalPages ? "cursor-not-allowed" : ""
105+ } , /*#__PURE__*/ React . createElement ( "span" , {
106+ title : "next page" ,
107+ onClick : function onClick ( ) {
108+ updatePageNumberInUrl ( currentPage + 1 ) ;
109+ setCurrentPage ( currentPage + 1 ) ;
110+ } ,
111+ className : "" . concat ( className , " ml-1 " ) . concat ( currentPage === totalPages ? "bg-emerald-100 pointer-events-none" : "" )
112+ } , "\u21FE" ) ) ) ;
113+ }
0 commit comments