2019-11-15 14:35:25 +08:00
|
|
|
import { useState } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { PaginationProps } from '../../pagination';
|
|
|
|
import type { TablePaginationConfig } from '../interface';
|
2022-12-24 15:05:33 +08:00
|
|
|
import extendsObject from '../../_util/extendsObject';
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
export const DEFAULT_PAGE_SIZE = 10;
|
|
|
|
|
|
|
|
export function getPaginationParam(
|
2020-04-27 23:45:14 +08:00
|
|
|
pagination: TablePaginationConfig | boolean | undefined,
|
|
|
|
mergedPagination: TablePaginationConfig,
|
2019-11-15 14:35:25 +08:00
|
|
|
) {
|
|
|
|
const param: any = {
|
|
|
|
current: mergedPagination.current,
|
|
|
|
pageSize: mergedPagination.pageSize,
|
|
|
|
};
|
|
|
|
const paginationObj = pagination && typeof pagination === 'object' ? pagination : {};
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
Object.keys(paginationObj).forEach((pageProp) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const value = (mergedPagination as any)[pageProp];
|
|
|
|
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
param[pageProp] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function usePagination(
|
|
|
|
total: number,
|
|
|
|
pagination: TablePaginationConfig | false | undefined,
|
|
|
|
onChange: (current: number, pageSize: number) => void,
|
|
|
|
): [TablePaginationConfig, () => void] {
|
2019-12-30 15:35:53 +08:00
|
|
|
const { total: paginationTotal = 0, ...paginationObj } =
|
|
|
|
pagination && typeof pagination === 'object' ? pagination : {};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-10-27 23:02:52 +08:00
|
|
|
const [innerPagination, setInnerPagination] = useState<{
|
|
|
|
current?: number;
|
|
|
|
pageSize?: number;
|
2020-12-09 17:12:32 +08:00
|
|
|
}>(() => ({
|
|
|
|
current: 'defaultCurrent' in paginationObj ? paginationObj.defaultCurrent : 1,
|
|
|
|
pageSize:
|
|
|
|
'defaultPageSize' in paginationObj ? paginationObj.defaultPageSize : DEFAULT_PAGE_SIZE,
|
|
|
|
}));
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// ============ Basic Pagination Config ============
|
2020-04-28 20:29:34 +08:00
|
|
|
const mergedPagination = extendsObject<Partial<TablePaginationConfig>>(
|
|
|
|
innerPagination,
|
|
|
|
paginationObj,
|
|
|
|
{
|
|
|
|
total: paginationTotal > 0 ? paginationTotal : total,
|
|
|
|
},
|
|
|
|
);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2021-02-03 11:42:11 +08:00
|
|
|
// Reset `current` if data length or pageSize changed
|
|
|
|
const maxPage = Math.ceil((paginationTotal || total) / mergedPagination.pageSize!);
|
|
|
|
if (mergedPagination.current! > maxPage) {
|
2021-02-26 21:51:35 +08:00
|
|
|
// Prevent a maximum page count of 0
|
|
|
|
mergedPagination.current = maxPage || 1;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 12:18:21 +08:00
|
|
|
const refreshPagination = (current?: number, pageSize?: number) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
setInnerPagination({
|
2021-11-26 12:18:21 +08:00
|
|
|
current: current ?? 1,
|
2020-07-10 15:17:15 +08:00
|
|
|
pageSize: pageSize || mergedPagination.pageSize,
|
2019-11-15 14:35:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-10 15:17:15 +08:00
|
|
|
const onInternalChange: PaginationProps['onChange'] = (current, pageSize) => {
|
2021-02-03 11:42:11 +08:00
|
|
|
if (pagination) {
|
|
|
|
pagination.onChange?.(current, pageSize);
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
2020-07-10 15:17:15 +08:00
|
|
|
refreshPagination(current, pageSize);
|
2021-02-03 11:42:11 +08:00
|
|
|
onChange(current, pageSize || mergedPagination?.pageSize!);
|
2019-11-15 14:35:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (pagination === false) {
|
|
|
|
return [{}, () => {}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
...mergedPagination,
|
|
|
|
onChange: onInternalChange,
|
|
|
|
},
|
|
|
|
refreshPagination,
|
|
|
|
];
|
|
|
|
}
|