UNPKG

@visa/nova-react

Version:

Visa Product Design System Nova React library

18 lines (17 loc) 2.27 kB
/** * Copyright (c) 2025 Visa, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * **/ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),a=require("./utils.js");const t={defaultSelected:1,endBlockMaxLength:3,maxPageNumber:null,middleBlockMaxLength:3,separator:-1,startBlockMaxLength:3,startPage:1},r=r=>{const{defaultSelected:l,endBlockMaxLength:n,maxPageNumber:o,middleBlockMaxLength:s,separator:g,startBlockMaxLength:u,startPage:c,totalPages:d}={...t,...r},[i,P]=e.useState(Math.min(Math.max(l,c),d)),m=c,x=d+c-1,h=null===o?x:Math.min(o,x),M=i===m,L=i===h,f=d>n+s+u,p=i<m+u,k=i>h-n,B=!p&&!k,b=p?a.generateArray(m,u):[m],y=(()=>{if(!B)return[];const e=Math.floor(s/2);return i-e<=m?a.generateArray(i-(i-m)+1,s):i+e>=h?a.generateArray(i+(h-i)-s,s):a.generateArray(i-e,s)})(),A=k?a.generateArray(h-n+1,n):[h],N=f?[b,y,A].map((e=>e.length?[...e,g]:[])).flat().slice(0,-1):a.generateArray(m,h-m+1);return e.useEffect((()=>{if(n<1||s<1||u<1)throw new Error("endBlockMaxLength, middleBlockMaxLength, and startBlockMaxLength all must be greater than 0");if(c<0||null!==o&&o<1)throw new Error("startPage must be 0 or greater, and maxPageNumber must be 1 or greater")}),[n,o,s,u,c]),{isFirstPage:M,isLastPage:L,onFirstPage:()=>P(m),onLastPage:()=>P(h),onNextPage:()=>P(Math.min(h,i+1)),onPageChange:e=>{P(e>h?h:e<m?m:e)},onPreviousPage:()=>P(Math.max(m,i-1)),pages:N,selectedPage:i}};r.displayName="usePagination",r.defaultProps={defaultSelected:1,endBlockMaxLength:3,maxPageNumber:null,middleBlockMaxLength:3,separator:-1,startBlockMaxLength:3,startPage:1},exports.calculatePagesFromTo=a.calculatePagesFromTo,exports.calculateTotalPages=a.calculateTotalPages,exports.default=r,exports.usePagination=r;