@visa/nova-react
Version:
Visa Product Design System Nova React library. Compatible with React ^19.
2 lines (1 loc) • 2.9 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),a=require("./utils.js");const t={blockMaxLength:3,compact:!1,defaultSelected:1,maxPageNumber:null,separator:-1,startPage:1,totalPages:1},n=(n=t)=>{const o=n.blockMaxLength??t.blockMaxLength,{blockMaxLength:r,compact:l,defaultSelected:s,selectedPage:g,setSelectedPage:c,separator:d,totalPages:i}={...t,...n};let{endBlockMaxLength:u,maxPageNumber:P,middleBlockMaxLength:m,startBlockMaxLength:x,startPage:h}={...t,endBlockMaxLength:o,middleBlockMaxLength:o,startBlockMaxLength:o,...n};x<1&&(x=3),m<1&&(m=3),u<1&&(u=3),h<0&&(h=1),null!==P&&P<1&&(P=null);const[M,L]=e.useState(Math.min(Math.max(s,h),i)),k=void 0!==g&&void 0!==c,f=k?g:M,b=k?c:L;e.useEffect(()=>{const e={...t,...n};void 0!==e.startBlockMaxLength&&e.startBlockMaxLength<1&&console.warn(`⚠️ usePagination: startBlockMaxLength must be >= 1. Received: ${e.startBlockMaxLength}. Using default: 3`),void 0!==e.middleBlockMaxLength&&e.middleBlockMaxLength<1&&console.warn(`⚠️ usePagination: middleBlockMaxLength must be >= 1. Received: ${e.middleBlockMaxLength}. Using default: 3`),void 0!==e.endBlockMaxLength&&e.endBlockMaxLength<1&&console.warn(`⚠️ usePagination: endBlockMaxLength must be >= 1. Received: ${e.endBlockMaxLength}. Using default: 3`),void 0!==e.startPage&&e.startPage<0&&console.warn(`⚠️ usePagination: startPage must be >= 0. Received: ${e.startPage}. Using default: 1`),null!==e.maxPageNumber&&void 0!==e.maxPageNumber&&e.maxPageNumber<1&&console.warn(`⚠️ usePagination: maxPageNumber must be >= 1 or null. Received: ${e.maxPageNumber}. Using default: null`)},[n]),e.useEffect(()=>{const e=void 0!==g,a=void 0!==c;e!==a&&console.warn(`⚠️ usePagination: Both selectedPage and setSelectedPage must be provided together.\nCurrently: selectedPage=${e?g:"undefined"}, setSelectedPage=${a?"provided":"undefined"}\nFalling back to uncontrolled mode.`)},[g,c]);const v=h,B=i+h-1,p=null===P?B:Math.min(P,B),y=f===v,A=f===p,N=i>u+m+x,$=f<v+x,w=f>p-u,S=!$&&!w,F=$?a.generateArray(v,x):[v],R=(()=>{if(!S)return[];const e=Math.floor(m/2);return f-e<=v?a.generateArray(f-(f-v)+1,m):f+e>=p?a.generateArray(f+(p-f)-m,m):a.generateArray(f-e,m)})(),U=w?a.generateArray(p-u+1,u):[p],T=e.useMemo(()=>{if(r>i)return a.generateArray(h,p-h+1);const e=Math.floor(r/2);return f-e<=h?a.generateArray(h,r):f+e>=p?a.generateArray(p-r+1,r):a.generateArray(f-e,r)},[r,p,f,h,i]);return{isFirstPage:y,isLastPage:A,onFirstPage:()=>b(v),onLastPage:()=>b(p),onNextPage:()=>b(Math.min(p,f+1)),onPageChange:e=>{b(e>p?p:e<v?v:e)},onPreviousPage:()=>b(Math.max(v,f-1)),pages:l?T:N?[F,R,U].map(e=>e.length?[...e,d]:[]).flat().slice(0,-1):a.generateArray(v,p-v+1),selectedPage:f}};n.displayName="usePagination",exports.calculatePagesFromTo=a.calculatePagesFromTo,exports.calculateTotalPages=a.calculateTotalPages,exports.default=n,exports.usePagination=n;