UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

154 lines (150 loc) 4.75 kB
"use client"; import { createContext as createContext$1 } from "../../utils/context.js"; import { mergeRefs } from "../../utils/ref.js"; import { utils_exports } from "../../utils/index.js"; import { useControllableState } from "../../hooks/use-controllable-state/index.js"; import { useI18n } from "../../providers/i18n-provider/i18n-provider.js"; import { useCallback, useMemo } from "react"; //#region src/components/pagination/use-pagination.ts const [PaginationContext, usePaginationContext] = createContext$1({ name: "PaginationContext" }); const usePagination = ({ boundaries = 1, defaultPage = 1, disabled = false, page, siblings = 1, total, onChange: onChangeProp,...rest }) => { const [currentPage, setCurrentPage] = useControllableState({ defaultValue: defaultPage, value: page, onChange: onChangeProp }); const { t } = useI18n("pagination"); const range = useMemo(() => { if (siblings * 2 + 3 + boundaries * 2 >= total) return computedRange(1, total); const prevSiblings = Math.max(currentPage - siblings, boundaries); const nextSiblings = Math.min(currentPage + siblings, total - boundaries); const prevDots = prevSiblings > boundaries + 2; const nextDots = nextSiblings < total - (boundaries + 1); if (!prevDots && nextDots) return [ ...computedRange(1, siblings * 2 + boundaries + 2), "ellipsis", ...computedRange(total - (boundaries - 1), total) ]; if (prevDots && !nextDots) { const nextPages = boundaries + 1 + 2 * siblings; return [ ...computedRange(1, boundaries), "ellipsis", ...computedRange(total - nextPages, total) ]; } return [ ...computedRange(1, boundaries), "ellipsis", ...computedRange(prevSiblings, nextSiblings), "ellipsis", ...computedRange(total - boundaries + 1, total) ]; }, [ boundaries, siblings, currentPage, total ]); const onChange = useCallback((page$1) => setCurrentPage(Math.max(1, Math.min(total, page$1))), [setCurrentPage, total]); const onChangeStart = useCallback(() => setCurrentPage(1), [setCurrentPage]); const onChangeEnd = useCallback(() => setCurrentPage(total), [setCurrentPage, total]); const onChangePrev = useCallback(() => setCurrentPage((prev) => Math.max(1, prev - 1)), [setCurrentPage]); const onChangeNext = useCallback(() => setCurrentPage((prev) => Math.min(total, prev + 1)), [setCurrentPage, total]); const getRootProps = useCallback(({ ref,...props } = {}) => ({ ...rest, ...props, ref: mergeRefs(ref, rest.ref), "aria-label": t("Pagination"), role: "navigation" }), [rest, t]); const getItemProps = useCallback(({ page: page$1,...props } = {}) => { if ((0, utils_exports.isNumber)(page$1)) return { type: "button", "aria-current": currentPage === page$1 ? "page" : void 0, "aria-label": t("Go to page {value}", { value: page$1 }), disabled, ...props, onClick: (0, utils_exports.handlerAll)(props.onClick, () => onChange(page$1)) }; else return { ...props, "data-ellipsis": "" }; }, [ currentPage, t, onChange, disabled ]); const getStartTriggerProps = useCallback((props = {}) => ({ type: "button", "aria-label": t("Go to first page"), disabled: disabled || currentPage === 1, ...props, onClick: (0, utils_exports.handlerAll)(props.onClick, onChangeStart) }), [ onChangeStart, t, disabled, currentPage ]); const getEndTriggerProps = useCallback((props = {}) => ({ type: "button", "aria-label": t("Go to last page"), disabled: disabled || currentPage === total, ...props, onClick: (0, utils_exports.handlerAll)(props.onClick, onChangeEnd) }), [ onChangeEnd, t, disabled, currentPage, total ]); const getPrevTriggerProps = useCallback((props = {}) => ({ type: "button", "aria-label": t("Go to previous page"), disabled: disabled || currentPage === 1, ...props, onClick: (0, utils_exports.handlerAll)(props.onClick, onChangePrev) }), [ onChangePrev, t, disabled, currentPage ]); return { currentPage, disabled, range, total, getEndTriggerProps, getItemProps, getNextTriggerProps: useCallback((props = {}) => ({ type: "button", "aria-label": t("Go to next page"), disabled: disabled || currentPage === total, ...props, onClick: (0, utils_exports.handlerAll)(props.onClick, onChangeNext) }), [ onChangeNext, t, disabled, currentPage, total ]), getPrevTriggerProps, getRootProps, getStartTriggerProps, onChange, onChangeEnd, onChangeNext, onChangePrev, onChangeStart }; }; const computedRange = (start, end) => Array.from({ length: end - start + 1 }, (_, index) => index + start); //#endregion export { PaginationContext, usePagination, usePaginationContext }; //# sourceMappingURL=use-pagination.js.map