UNPKG

@zag-js/pagination

Version:

Core logic for the pagination widget implemented as a state machine

195 lines (193 loc) • 6.81 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/pagination.connect.ts var pagination_connect_exports = {}; __export(pagination_connect_exports, { connect: () => connect }); module.exports = __toCommonJS(pagination_connect_exports); var import_dom_query = require("@zag-js/dom-query"); var import_pagination = require("./pagination.anatomy.js"); var dom = __toESM(require("./pagination.dom.js")); var import_pagination2 = require("./pagination.utils.js"); function connect(service, normalize) { const { send, scope, prop, computed, context } = service; const totalPages = computed("totalPages"); const page = context.get("page"); const pageSize = context.get("pageSize"); const translations = prop("translations"); const count = prop("count"); const getPageUrl = prop("getPageUrl"); const type = prop("type"); const previousPage = computed("previousPage"); const nextPage = computed("nextPage"); const pageRange = computed("pageRange"); const isFirstPage = page === 1; const isLastPage = page >= totalPages; const pages = (0, import_pagination2.getTransformedRange)({ page, totalPages, siblingCount: prop("siblingCount"), boundaryCount: prop("boundaryCount") }); return { count, page, pageSize, totalPages, pages, previousPage, nextPage, pageRange, slice(data) { return data.slice(pageRange.start, pageRange.end); }, setPageSize(size) { send({ type: "SET_PAGE_SIZE", size }); }, setPage(page2) { send({ type: "SET_PAGE", page: page2 }); }, goToNextPage() { send({ type: "NEXT_PAGE" }); }, goToPrevPage() { send({ type: "PREVIOUS_PAGE" }); }, goToFirstPage() { send({ type: "FIRST_PAGE" }); }, goToLastPage() { send({ type: "LAST_PAGE" }); }, getRootProps() { return normalize.element({ id: dom.getRootId(scope), ...import_pagination.parts.root.attrs, dir: prop("dir"), "aria-label": translations.rootLabel }); }, getEllipsisProps(props) { return normalize.element({ id: dom.getEllipsisId(scope, props.index), ...import_pagination.parts.ellipsis.attrs, dir: prop("dir") }); }, getItemProps(props) { const index = props.value; const isCurrentPage = index === page; return normalize.element({ id: dom.getItemId(scope, index), ...import_pagination.parts.item.attrs, dir: prop("dir"), "data-index": index, "data-selected": (0, import_dom_query.dataAttr)(isCurrentPage), "aria-current": isCurrentPage ? "page" : void 0, "aria-label": translations.itemLabel?.({ page: index, totalPages }), onClick() { send({ type: "SET_PAGE", page: index }); }, ...type === "button" && { type: "button" }, ...type === "link" && getPageUrl && { href: getPageUrl({ page: index, pageSize }) } }); }, getPrevTriggerProps() { return normalize.element({ id: dom.getPrevTriggerId(scope), ...import_pagination.parts.prevTrigger.attrs, dir: prop("dir"), "data-disabled": (0, import_dom_query.dataAttr)(isFirstPage), "aria-label": translations.prevTriggerLabel, onClick() { send({ type: "PREVIOUS_PAGE" }); }, ...type === "button" && { disabled: isFirstPage, type: "button" }, ...type === "link" && getPageUrl && previousPage && { href: getPageUrl({ page: previousPage, pageSize }) } }); }, getFirstTriggerProps() { return normalize.element({ id: dom.getFirstTriggerId(scope), ...import_pagination.parts.firstTrigger.attrs, dir: prop("dir"), "data-disabled": (0, import_dom_query.dataAttr)(isFirstPage), "aria-label": translations.firstTriggerLabel, onClick() { send({ type: "FIRST_PAGE" }); }, ...type === "button" && { disabled: isFirstPage, type: "button" }, ...type === "link" && getPageUrl && { href: getPageUrl({ page: 1, pageSize }) } }); }, getNextTriggerProps() { return normalize.element({ id: dom.getNextTriggerId(scope), ...import_pagination.parts.nextTrigger.attrs, dir: prop("dir"), "data-disabled": (0, import_dom_query.dataAttr)(isLastPage), "aria-label": translations.nextTriggerLabel, onClick() { send({ type: "NEXT_PAGE" }); }, ...type === "button" && { disabled: isLastPage, type: "button" }, ...type === "link" && getPageUrl && nextPage && { href: getPageUrl({ page: nextPage, pageSize }) } }); }, getLastTriggerProps() { return normalize.element({ id: dom.getLastTriggerId(scope), ...import_pagination.parts.lastTrigger.attrs, dir: prop("dir"), "data-disabled": (0, import_dom_query.dataAttr)(isLastPage), "aria-label": translations.lastTriggerLabel, onClick() { send({ type: "LAST_PAGE" }); }, ...type === "button" && { disabled: isLastPage, type: "button" }, ...type === "link" && getPageUrl && { href: getPageUrl({ page: totalPages, pageSize }) } }); } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { connect });