UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

90 lines (87 loc) 3.86 kB
'use client'; import { jsx, jsxs } from 'react/jsx-runtime'; import { forwardRef } from 'react'; import { get } from '../../helpers/get.js'; import { resolveProps } from '../../helpers/resolve-props.js'; import { useResolveTheme } from '../../helpers/resolve-theme.js'; import { twMerge } from '../../helpers/tailwind-merge.js'; import { ChevronLeftIcon } from '../../icons/chevron-left-icon.js'; import { ChevronRightIcon } from '../../icons/chevron-right-icon.js'; import { useThemeProvider } from '../../theme/provider.js'; import { range } from './helpers.js'; import { PaginationButton, PaginationNavigation } from './PaginationButton.js'; import { paginationTheme } from './theme.js'; const Pagination = forwardRef((props, ref) => { const provider = useThemeProvider(); const theme = useResolveTheme( [paginationTheme, provider.theme?.pagination, props.theme], [get(provider.clearTheme, "pagination"), props.clearTheme], [get(provider.applyTheme, "pagination"), props.applyTheme] ); const { className, currentPage, layout = "pagination", nextLabel = "Next", onPageChange, previousLabel = "Previous", renderPaginationButton = (props2) => /* @__PURE__ */ jsx(PaginationButton, { ...props2 }), showIcons: showIcon = false, totalPages, ...restProps } = resolveProps(props, provider.props?.pagination); const lastPage = Math.min(Math.max(layout === "pagination" ? currentPage + 2 : currentPage + 4, 5), totalPages); const firstPage = Math.max(1, lastPage - 4); function goToNextPage() { onPageChange(Math.min(currentPage + 1, totalPages)); } function goToPreviousPage() { onPageChange(Math.max(currentPage - 1, 1)); } return /* @__PURE__ */ jsxs("nav", { ref, className: twMerge(theme.base, className), ...restProps, children: [ layout === "table" && /* @__PURE__ */ jsxs("div", { className: theme.layout.table.base, children: [ "Showing ", /* @__PURE__ */ jsx("span", { className: theme.layout.table.span, children: firstPage }), " to\xA0", /* @__PURE__ */ jsx("span", { className: theme.layout.table.span, children: lastPage }), " of\xA0", /* @__PURE__ */ jsx("span", { className: theme.layout.table.span, children: totalPages }), " Entries" ] }), /* @__PURE__ */ jsxs("ul", { className: theme.pages.base, children: [ /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs( PaginationNavigation, { className: twMerge(theme.pages.previous.base, showIcon && theme.pages.showIcon), onClick: goToPreviousPage, disabled: currentPage === 1, children: [ showIcon && /* @__PURE__ */ jsx(ChevronLeftIcon, { "aria-hidden": true, className: theme.pages.previous.icon }), previousLabel ] } ) }), layout === "pagination" && range(firstPage, lastPage).map((page) => /* @__PURE__ */ jsx("li", { "aria-current": page === currentPage ? "page" : void 0, children: renderPaginationButton({ className: twMerge(theme.pages.selector.base, currentPage === page && theme.pages.selector.active), active: page === currentPage, onClick: () => onPageChange(page), children: page }) }, page)), /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs( PaginationNavigation, { className: twMerge(theme.pages.next.base, showIcon && theme.pages.showIcon), onClick: goToNextPage, disabled: currentPage === totalPages, children: [ nextLabel, showIcon && /* @__PURE__ */ jsx(ChevronRightIcon, { "aria-hidden": true, className: theme.pages.next.icon }) ] } ) }) ] }) ] }); }); Pagination.displayName = "Pagination"; export { Pagination }; //# sourceMappingURL=Pagination.js.map