import { useState } from 'react' ; import { PaginationProps , PaginationConfig } from 'antd/es/pagination' ; export const DEFAULT_PAGE_SIZE = 10 ; export function getPaginationParam ( pagination , mergedPagination ) { const param = { 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 ; } export default function usePagination ( total , pagination , onChange ) { const { total : paginationTotal = 0 , ... paginationObj } = pagination && typeof pagination === 'object' ? pagination : { } ; const [ innerPagination , setInnerPagination ] = useState < PaginationConfig > ( ( ) => { return { current : 'defaultCurrent' in paginationObj ? paginationObj . defaultCurrent : 1 , pageSize : 'defaultPageSize' in paginationObj ? paginationObj . defaultPageSize : DEFAULT_PAGE_SIZE , } ; } ) ; const mergedPagination = { ... innerPagination , ... paginationObj , total : paginationTotal > 0 ? paginationTotal : total , } ; if ( ! paginationTotal ) { const maxPage = Math . ceil ( total / mergedPagination . pageSize ! ) ; if ( maxPage < mergedPagination . current ! ) { mergedPagination . current = 1 ; } } const refreshPagination = ( current = 1 ) => { setInnerPagination ( { ... mergedPagination , current , } ) ; } ; const onInternalChange = ( ... args ) => { const [ current ] = args ; refreshPagination ( current ) ; onChange ( current , args [ 1 ] || mergedPagination . pageSize ! ) ; if ( pagination && pagination . onChange ) { pagination . onChange ( ... args ) ; } } ; const onInternalShowSizeChange = ( ... args ) => { const [ , pageSize ] = args ; setInnerPagination ( { ... mergedPagination , current : 1 , pageSize , } ) ; onChange ( 1 , pageSize ) ; if ( pagination && pagination . onShowSizeChange ) { pagination . onShowSizeChange ( ... args ) ; } } ; if ( pagination === false ) { return [ { } , ( ) => { } ] ; } return [ { ... mergedPagination , onChange : onInternalChange , onShowSizeChange : onInternalShowSizeChange , } , refreshPagination , ] ; }