import * as React from 'react' ; import { buildPageRange } from './buildPageRange' ; import { getRangeMax , getRangeMin } from './common/utils/helpers' ; import { PaginationModel } from './types' ; export type UsePaginationModelConfig = { /**
     * The page number for the last page (it can also be used as a total page
     * count)
     */ lastPage : number ; default firstPage ? : number ; default initialCurrentPage ? : number ; /**
     * The function called when the page changes
     */ onPageChange ? : ( pageNumber : number ) => void ; default rangeSize ? : number ; } ; export const usePaginationModel = ( { firstPage = 1 , initialCurrentPage = 1 , lastPage , rangeSize = 5 , onPageChange , } ) : PaginationModel => { const [ currentPage , setCurrentPage ] = React . useState ( initialCurrentPage ) ; const changePage = ( page ) => { onPageChange ?. ( page ) ; setCurrentPage ( page ) ; } ; const first = ( ) => { changePage ( firstPage ) ; } ; const last = ( ) => { changePage ( lastPage ) ; } ; const next = ( ) => { changePage ( currentPage + 1 ) ; } ; const previous = ( ) => { changePage ( currentPage - 1 ) ; } ; const goTo = ( pageNumber ) => { if ( pageNumber < firstPage ) { changePage ( firstPage ) ; } else if ( pageNumber > lastPage ) { changePage ( lastPage ) ; } else { changePage ( pageNumber ) ; } } ; const range = buildPageRange ( { currentPage , lastPage , rangeSize } ) ; const rangeMin = getRangeMin ( range ) ; const rangeMax = getRangeMax ( range ) ; const state = { firstPage , currentPage , lastPage , range , rangeSize , rangeMin , rangeMax , } ; const events = { setCurrentPage : changePage , first , last , next , previous , goTo , } ; return { state , events , } ; } ;