Skip to content

Commit c03d249

Browse files
Merge pull request #167 from nqviet/issue_75
issue #75
2 parents 4ebbd62 + 6795ef5 commit c03d249

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

src/components/Pagination/index.jsx

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from "react";
1+
import React, { useRef, useState, useEffect } from "react";
22
import PT from "prop-types";
33
import Dropdown from "../Dropdown";
44
import {
@@ -12,6 +12,20 @@ import "./styles.scss";
1212

1313
const N = PAGINATION_MAX_PAGE_DISPLAY;
1414

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+
1529
/**
1630
* Pagination with the first page index being as 0 and the last page index being as `total - 1`
1731
*/
@@ -20,24 +34,6 @@ const Pagination = ({ length, pageIndex, pageSize, onChange }) => {
2034
const perPageOptions = utils.createDropdownOptions(PAGINATION_PER_PAGES);
2135
utils.setSelectedDropdownOption(perPageOptions, `${pageSize}`);
2236

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-
4137
const onChangePageSize = (options) => {
4238
const selectedOption = utils.getSelectedDropdownOption(options);
4339
const newPageSize = +selectedOption.label;
@@ -59,33 +55,40 @@ const Pagination = ({ length, pageIndex, pageSize, onChange }) => {
5955
}
6056
};
6157

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([]);
7060

7161
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)
8070
}
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+
}
8588
}
86-
setDisplayPages(updateDisplayPages);
87-
}
88-
}, [pageIndex]);
89+
90+
previousPropsRef.current = { length, pageSize, pageIndex };
91+
}, [length, pageSize, pageIndex, displayPages, setDisplayPages]);
8992

9093
const formatPage = (p) => `${p + 1}`;
9194

0 commit comments

Comments
 (0)