2019-11-15 14:35:25 +08:00
|
|
|
import { useState } from 'react';
|
2024-04-01 15:49:45 +08:00
|
|
|
|
2022-12-24 15:05:33 +08:00
|
|
|
import extendsObject from '../../_util/extendsObject';
|
2024-04-01 15:49:45 +08:00
|
|
|
import type { PaginationProps } from '../../pagination';
|
2023-03-03 14:55:46 +08:00
|
|
|
import type { TablePaginationConfig } from '../interface';
|
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
|
|
|
mergedPagination: TablePaginationConfig,
|
2023-01-16 16:31:08 +08:00
|
|
|
pagination?: TablePaginationConfig | boolean,
|
2019-11-15 14:35:25 +08:00
|
|
|
) {
|
|
|
|
const param: any = {
|
|
|
|
current: mergedPagination.current,
|
|
|
|
pageSize: mergedPagination.pageSize,
|
|
|
|
};
|
2023-03-03 14:55:46 +08:00
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
const paginationObj = pagination && typeof pagination === 'object' ? pagination : {};
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
Object.keys(paginationObj).forEach((pageProp) => {
|
|
|
|
const value = mergedPagination[pageProp as keyof typeof paginationObj];
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
param[pageProp] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:55:46 +08:00
|
|
|
function usePagination(
|
2019-11-15 14:35:25 +08:00
|
|
|
total: number,
|
|
|
|
onChange: (current: number, pageSize: number) => void,
|
2023-03-03 14:55:46 +08:00
|
|
|
pagination?: TablePaginationConfig | false,
|
2023-03-25 13:59:21 +08:00
|
|
|
): readonly [TablePaginationConfig, (current?: number, pageSize?: number) => 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
|
|
|
|
2023-03-03 14:55:46 +08:00
|
|
|
const [innerPagination, setInnerPagination] = useState<{ current?: number; pageSize?: number }>(
|
|
|
|
() => ({
|
|
|
|
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) {
|
2023-03-03 14:55:46 +08:00
|
|
|
return [{}, () => {}] as const;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
...mergedPagination,
|
|
|
|
onChange: onInternalChange,
|
|
|
|
},
|
|
|
|
refreshPagination,
|
2023-03-03 14:55:46 +08:00
|
|
|
] as const;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
2023-03-03 14:55:46 +08:00
|
|
|
|
|
|
|
export default usePagination;
|