2019-11-15 14:35:25 +08:00
|
|
|
import { useState } from 'react';
|
2020-04-27 23:45:14 +08:00
|
|
|
import { PaginationProps } from '../../pagination';
|
2019-11-15 14:35:25 +08:00
|
|
|
import { TablePaginationConfig } from '../interface';
|
|
|
|
|
|
|
|
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 : {};
|
|
|
|
|
|
|
|
Object.keys(paginationObj).forEach(pageProp => {
|
|
|
|
const value = (mergedPagination as any)[pageProp];
|
|
|
|
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
param[pageProp] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:29:34 +08:00
|
|
|
function extendsObject<T extends Object>(...list: T[]) {
|
|
|
|
const result: T = {} as T;
|
|
|
|
|
|
|
|
list.forEach(obj => {
|
|
|
|
if (obj) {
|
|
|
|
Object.keys(obj).forEach(key => {
|
|
|
|
const val = (obj as any)[key];
|
|
|
|
if (val !== undefined) {
|
|
|
|
(result as any)[key] = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
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;
|
|
|
|
}>(() => {
|
2019-11-15 14:35:25 +08:00
|
|
|
return {
|
|
|
|
current: 'defaultCurrent' in paginationObj ? paginationObj.defaultCurrent : 1,
|
|
|
|
pageSize:
|
|
|
|
'defaultPageSize' in paginationObj ? paginationObj.defaultPageSize : DEFAULT_PAGE_SIZE,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// ============ 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
|
|
|
|
2019-12-30 15:35:53 +08:00
|
|
|
if (!paginationTotal) {
|
2019-11-28 14:46:49 +08:00
|
|
|
// Reset `current` if data length changed. Only reset when paginationObj do not have total
|
|
|
|
const maxPage = Math.ceil(total / mergedPagination.pageSize!);
|
|
|
|
if (maxPage < mergedPagination.current!) {
|
|
|
|
mergedPagination.current = 1;
|
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 15:17:15 +08:00
|
|
|
const refreshPagination = (current: number = 1, pageSize?: number) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
setInnerPagination({
|
|
|
|
current,
|
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) => {
|
|
|
|
const paginationPageSize = mergedPagination?.pageSize;
|
|
|
|
if (pageSize && pageSize !== paginationPageSize) {
|
|
|
|
current = 1;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
2020-07-10 15:17:15 +08:00
|
|
|
if (pagination && pagination.onChange) pagination.onChange(current, pageSize);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-07-10 15:17:15 +08:00
|
|
|
refreshPagination(current, pageSize);
|
|
|
|
onChange(current, pageSize || paginationPageSize!);
|
2019-11-15 14:35:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (pagination === false) {
|
|
|
|
return [{}, () => {}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
...mergedPagination,
|
|
|
|
onChange: onInternalChange,
|
|
|
|
},
|
|
|
|
refreshPagination,
|
|
|
|
];
|
|
|
|
}
|