/* eslint-disable react/no-array-index-key */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable react/require-default-props */ /* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable @typescript-eslint/no-explicit-any */ import classNames from 'classnames'; import PropTypes from 'prop-types'; import React, { useEffect } from 'react'; import { v4 as uuidv4 } from 'uuid'; import CLink from '../link/CLink'; // component - CoreUI / CPagination const CPagination = (props: any) => { const { className, // innerRef, addListClass, activePage, size, firstButton, previousButton, nextButton, lastButton, dots, arrows, doubleArrows, limit, pages, align, onActivePageChange, ...attributes } = props; useEffect(() => { pages < activePage && onActivePageChange(pages, true); }, [pages]); // render const listClasses = classNames( 'pagination', size && `pagination-${size}`, `justify-content-${align}`, addListClass ); const backArrowsClasses = classNames( 'page-item', activePage === 1 && 'disabled' ); const nextArrowsClasses = classNames( 'page-item', activePage === pages && 'disabled' ); const showDots = (() => { return dots && limit > 4 && limit < pages; })(); const maxPrevItems = (() => { return Math.floor((limit - 1) / 2); })(); const maxNextItems = (() => { return Math.ceil((limit - 1) / 2); })(); const beforeDots = (() => { return showDots && activePage > maxPrevItems + 1; })(); const afterDots = (() => { return showDots && activePage < pages - maxNextItems; })(); const computedLimit = (() => { // @ts-ignore return limit - afterDots - beforeDots; })(); const range = (() => { return activePage + maxNextItems; })(); const lastItem = (() => { // @ts-ignore return range >= pages ? pages : range - afterDots; })(); const itemsAmount = (() => { return pages < computedLimit ? pages : computedLimit; })(); const items = (() => { if (activePage - maxPrevItems <= 1) { return Array.from( { length: itemsAmount, }, (v, i) => i + 1 ); } return Array.from( { length: itemsAmount, }, (v, i) => { return lastItem - i; } ).reverse(); })(); const setPage = (number: number | string) => { if (number !== activePage) { onActivePageChange(number); } }; return ( <> ); }; CPagination.propTypes = { className: PropTypes.oneOfType([ PropTypes.string, PropTypes.array, PropTypes.object, ]), // innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]), activePage: PropTypes.number, dots: PropTypes.bool, arrows: PropTypes.bool, doubleArrows: PropTypes.bool, firstButton: PropTypes.oneOfType([PropTypes.node, PropTypes.string]), previousButton: PropTypes.oneOfType([PropTypes.node, PropTypes.string]), nextButton: PropTypes.oneOfType([PropTypes.node, PropTypes.string]), lastButton: PropTypes.oneOfType([PropTypes.node, PropTypes.string]), size: PropTypes.oneOf(['', 'sm', 'lg']), align: PropTypes.oneOf(['start', 'center', 'end']), addListClass: PropTypes.string, limit: PropTypes.number, pages: PropTypes.number, onActivePageChange: PropTypes.func.isRequired, }; CPagination.defaultProps = { activePage: 1, dots: true, arrows: true, doubleArrows: true, limit: 5, firstButton: <>«, previousButton: <>‹, nextButton: <>›, lastButton: <>», align: 'start', pages: 10, }; export default CPagination;