import { useState } from 'react'; import { PaginationProps } from '../../pagination'; import { TablePaginationConfig } from '../interface'; export const DEFAULT_PAGE_SIZE = 10; export function getPaginationParam( pagination: TablePaginationConfig | boolean | undefined, mergedPagination: TablePaginationConfig, ) { 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; } function extendsObject(...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; } export default function usePagination( total: number, pagination: TablePaginationConfig | false | undefined, onChange: (current: number, pageSize: number) => void, ): [TablePaginationConfig, () => void] { const { total: paginationTotal = 0, ...paginationObj } = pagination && typeof pagination === 'object' ? pagination : {}; const [innerPagination, setInnerPagination] = useState<{ current?: number; pageSize?: number; }>(() => ({ current: 'defaultCurrent' in paginationObj ? paginationObj.defaultCurrent : 1, pageSize: 'defaultPageSize' in paginationObj ? paginationObj.defaultPageSize : DEFAULT_PAGE_SIZE, })); // ============ Basic Pagination Config ============ const mergedPagination = extendsObject>( innerPagination, paginationObj, { total: paginationTotal > 0 ? paginationTotal : total, }, ); // Reset `current` if data length or pageSize changed const maxPage = Math.ceil((paginationTotal || total) / mergedPagination.pageSize!); if (mergedPagination.current! > maxPage) { mergedPagination.current = maxPage; } const refreshPagination = (current: number = 1, pageSize?: number) => { setInnerPagination({ current, pageSize: pageSize || mergedPagination.pageSize, }); }; const onInternalChange: PaginationProps['onChange'] = (current, pageSize) => { if (pagination) { pagination.onChange?.(current, pageSize); } refreshPagination(current, pageSize); onChange(current, pageSize || mergedPagination?.pageSize!); }; if (pagination === false) { return [{}, () => {}]; } return [ { ...mergedPagination, onChange: onInternalChange, }, refreshPagination, ]; }