1
- import React , { useEffect , useRef , useState } from "react" ;
1
+ import React , { useRef , useState , useEffect } from "react" ;
2
2
import PT from "prop-types" ;
3
3
import Dropdown from "../Dropdown" ;
4
4
import {
@@ -12,6 +12,20 @@ import "./styles.scss";
12
12
13
13
const N = PAGINATION_MAX_PAGE_DISPLAY ;
14
14
15
+ const createDisplayPages = ( p , n ) => {
16
+ const pages = [ ] ;
17
+ for (
18
+ let start = utils . clamp ( p - N , 0 , n ) ,
19
+ end = utils . clamp ( p + N , 0 , n ) ,
20
+ i = start ;
21
+ i < end ;
22
+ i += 1
23
+ ) {
24
+ pages . push ( i ) ;
25
+ }
26
+ return pages . slice ( - N ) ;
27
+ } ;
28
+
15
29
/**
16
30
* Pagination with the first page index being as 0 and the last page index being as `total - 1`
17
31
*/
@@ -20,24 +34,6 @@ const Pagination = ({ length, pageIndex, pageSize, onChange }) => {
20
34
const perPageOptions = utils . createDropdownOptions ( PAGINATION_PER_PAGES ) ;
21
35
utils . setSelectedDropdownOption ( perPageOptions , `${ pageSize } ` ) ;
22
36
23
- const createDisplayPages = ( p , n ) => {
24
- const pages = [ ] ;
25
- for (
26
- let start = utils . clamp ( p - N , 0 , n ) ,
27
- end = utils . clamp ( p + N , 0 , n ) ,
28
- i = start ;
29
- i < end ;
30
- i += 1
31
- ) {
32
- pages . push ( i ) ;
33
- }
34
- return pages . slice ( - N ) ;
35
- } ;
36
-
37
- const [ displayPages , setDisplayPages ] = useState (
38
- createDisplayPages ( pageIndex , total )
39
- ) ;
40
-
41
37
const onChangePageSize = ( options ) => {
42
38
const selectedOption = utils . getSelectedDropdownOption ( options ) ;
43
39
const newPageSize = + selectedOption . label ;
@@ -59,33 +55,40 @@ const Pagination = ({ length, pageIndex, pageSize, onChange }) => {
59
55
}
60
56
} ;
61
57
62
- const latestPropsRef = useRef ( null ) ;
63
- latestPropsRef . current = { displayPages, pageIndex } ;
64
-
65
- useEffect ( ( ) => {
66
- const newTotal = Math . ceil ( length / pageSize ) ;
67
- const _pageIndex = latestPropsRef . current . pageIndex ;
68
- setDisplayPages ( createDisplayPages ( _pageIndex , newTotal ) ) ;
69
- } , [ length , pageSize ] ) ;
58
+ const previousPropsRef = useRef ( ) ;
59
+ const [ displayPages , setDisplayPages ] = useState ( [ ] ) ;
70
60
71
61
useEffect ( ( ) => {
72
- const _displayPages = latestPropsRef . current . displayPages ;
73
- const start = _displayPages [ 0 ] ;
74
- const end = _displayPages [ _displayPages . length - 1 ] ;
75
-
76
- const updateDisplayPages = [ ] ;
77
- if ( pageIndex < start ) {
78
- for ( let i = pageIndex ; i < pageIndex + N ; i += 1 ) {
79
- updateDisplayPages . push ( i ) ;
62
+ let _displayPages = displayPages ;
63
+
64
+ if ( ! previousPropsRef . current
65
+ || previousPropsRef . current . length !== length
66
+ || previousPropsRef . current . pageSize !== pageSize ) {
67
+ const newTotal = Math . ceil ( length / pageSize ) ;
68
+ _displayPages = createDisplayPages ( pageIndex , newTotal ) ;
69
+ setDisplayPages ( _displayPages )
80
70
}
81
- setDisplayPages ( updateDisplayPages ) ;
82
- } else if ( pageIndex > end ) {
83
- for ( let i = pageIndex ; i > pageIndex - N ; i -= 1 ) {
84
- updateDisplayPages . unshift ( i ) ;
71
+
72
+ if ( ! previousPropsRef . current || previousPropsRef . current . pageIndex !== pageIndex ) {
73
+ const start = _displayPages [ 0 ] ;
74
+ const end = _displayPages [ _displayPages . length - 1 ] ;
75
+
76
+ const updateDisplayPages = [ ] ;
77
+ if ( pageIndex < start ) {
78
+ for ( let i = pageIndex ; i < pageIndex + N ; i += 1 ) {
79
+ updateDisplayPages . push ( i ) ;
80
+ }
81
+ setDisplayPages ( updateDisplayPages ) ;
82
+ } else if ( pageIndex > end ) {
83
+ for ( let i = pageIndex ; i > pageIndex - N ; i -= 1 ) {
84
+ updateDisplayPages . unshift ( i ) ;
85
+ }
86
+ setDisplayPages ( updateDisplayPages ) ;
87
+ }
85
88
}
86
- setDisplayPages ( updateDisplayPages ) ;
87
- }
88
- } , [ pageIndex ] ) ;
89
+
90
+ previousPropsRef . current = { length , pageSize , pageIndex } ;
91
+ } , [ length , pageSize , pageIndex , displayPages , setDisplayPages ] ) ;
89
92
90
93
const formatPage = ( p ) => `${ p + 1 } ` ;
91
94
0 commit comments