Skip to content

Commit

Permalink
fix(pagination): fix bug of pagination (#1862)
Browse files Browse the repository at this point in the history
Co-authored-by: maxin <maxin@growingio.com>
  • Loading branch information
nnmax and maxin committed Feb 28, 2022
1 parent 639a9b3 commit dd71336
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const Pagination = WithRef<HTMLDivElement, PaginationProps>((props, ref) => {
const handleRowsChange = (rows: number, previousRows: number) => {
setPageSize(rows);
onPageSizeChange?.(rows, previousRows);
goToPage(1, null);
};

if (hideOnSinglePage && maxPages <= 1) return null;
Expand Down
42 changes: 15 additions & 27 deletions src/pagination/hooks/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isFunction, range } from 'lodash';
import { useEffect, useRef } from 'react';
import { useCallback, useEffect } from 'react';
import PaginationProps, { PaginationItemProps, PaginationItemType } from '../interface';
import useControlledState from '../../utils/hooks/useControlledState';
import usePage from './usePage';
Expand Down Expand Up @@ -31,35 +31,23 @@ const usePagination = (

const [startPage, endPage, maxPages] = usePage(currentPage, pageSize, total);

useEffect(() => {
if (currentPage > maxPages) {
setCurrentPage(1);
}
}, [maxPages, currentPage, setCurrentPage]);

const goToPage: ReturnType<typeof usePagination>['goToPage'] = (value, event) => {
if (value) {
setCurrentPage(value);
}
if (isFunction(onChange)) {
onChange(value, pageSize, event);
}
};

/**
* 原始的总数
*/
const originTotal = useRef(total);
const goToPage: ReturnType<typeof usePagination>['goToPage'] = useCallback(
(page, event) => {
if (page) {
setCurrentPage(Math.min(page, maxPages));
}
if (isFunction(onChange)) {
onChange(page, pageSize, event);
}
},
[maxPages, onChange, pageSize, setCurrentPage]
);

// 总数改变跳回首页,
useEffect(() => {
// 保证首次渲染不跳转
if (originTotal.current !== total) {
goToPage(1, null);
if (currentPage > maxPages) {
goToPage(maxPages, null);
}
// 没必要监听 goToPage 依赖变化
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [total]);
}, [maxPages, currentPage, setCurrentPage, goToPage]);

const computePage = (type: PaginationItemType): number => {
switch (type) {
Expand Down
4 changes: 2 additions & 2 deletions src/table/hook/useSorter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const collectSortStates = <RecordType,>(
columns.forEach((column, index) => {
const columnPosition = getColumnPos(index, position);
const columnKey = getColumnKey(column, columnPosition);
const { defaultSortOrder } = column;
const { defaultSortOrder = null } = column;

if ('children' in column) {
if ('sortOrder' in column) {
Expand All @@ -39,7 +39,7 @@ const collectSortStates = <RecordType,>(
} else if ('sorter' in column) {
if ('sortOrder' in column) {
push(column, columnKey);
} else if (init && defaultSortOrder) {
} else if (init) {
push(column, columnKey, {
sortOrder: defaultSortOrder,
isControlled: false,
Expand Down

1 comment on commit dd71336

@vercel
Copy link

@vercel vercel bot commented on dd71336 Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.