@sytechui/use-pagination 2.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Next UI Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @sytechui/use-pagination
2
+
3
+ A Quick description of the component
4
+
5
+ > This is an internal utility, not intended for public usage.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ yarn add @sytechui/use-pagination
11
+ # or
12
+ npm i @sytechui/use-pagination
13
+ ```
14
+
15
+ ## Contribution
16
+
17
+ Yes please! See the
18
+ [contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
19
+ for details.
20
+
21
+ ## License
22
+
23
+ This project is licensed under the terms of the
24
+ [MIT license](https://github.com/heroui-inc/heroui/blob/master/LICENSE).
@@ -0,0 +1,52 @@
1
+ declare enum PaginationItemType {
2
+ DOTS = "dots",
3
+ PREV = "prev",
4
+ NEXT = "next"
5
+ }
6
+ interface UsePaginationProps {
7
+ /**
8
+ * The total number of pages.
9
+ */
10
+ total: number;
11
+ /**
12
+ * The selected page on initial render.
13
+ * @default 1
14
+ */
15
+ initialPage?: number;
16
+ /**
17
+ * The controlled selected page.
18
+ */
19
+ page?: number;
20
+ /**
21
+ * The number of pages to show on each side of the current page.
22
+ * @default 1
23
+ */
24
+ siblings?: number;
25
+ /**
26
+ * The number of pages to show at the beginning and end of the pagination.
27
+ * @default 1
28
+ */
29
+ boundaries?: number;
30
+ /**
31
+ * If `true`, the range will include "prev" and "next" buttons.
32
+ * @default false
33
+ */
34
+ showControls?: boolean;
35
+ /**
36
+ * Callback fired when the page changes.
37
+ */
38
+ onChange?: (page: number) => void;
39
+ }
40
+ type PaginationItemValue = number | PaginationItemType;
41
+ declare function usePagination(props: UsePaginationProps): {
42
+ range: PaginationItemValue[];
43
+ activePage: number;
44
+ setPage: (pageNumber: number) => void;
45
+ next: () => void;
46
+ previous: () => void;
47
+ first: () => void;
48
+ last: () => void;
49
+ };
50
+ type UsePaginationReturn = ReturnType<typeof usePagination>;
51
+
52
+ export { PaginationItemType, type PaginationItemValue, type UsePaginationProps, type UsePaginationReturn, usePagination };
@@ -0,0 +1,52 @@
1
+ declare enum PaginationItemType {
2
+ DOTS = "dots",
3
+ PREV = "prev",
4
+ NEXT = "next"
5
+ }
6
+ interface UsePaginationProps {
7
+ /**
8
+ * The total number of pages.
9
+ */
10
+ total: number;
11
+ /**
12
+ * The selected page on initial render.
13
+ * @default 1
14
+ */
15
+ initialPage?: number;
16
+ /**
17
+ * The controlled selected page.
18
+ */
19
+ page?: number;
20
+ /**
21
+ * The number of pages to show on each side of the current page.
22
+ * @default 1
23
+ */
24
+ siblings?: number;
25
+ /**
26
+ * The number of pages to show at the beginning and end of the pagination.
27
+ * @default 1
28
+ */
29
+ boundaries?: number;
30
+ /**
31
+ * If `true`, the range will include "prev" and "next" buttons.
32
+ * @default false
33
+ */
34
+ showControls?: boolean;
35
+ /**
36
+ * Callback fired when the page changes.
37
+ */
38
+ onChange?: (page: number) => void;
39
+ }
40
+ type PaginationItemValue = number | PaginationItemType;
41
+ declare function usePagination(props: UsePaginationProps): {
42
+ range: PaginationItemValue[];
43
+ activePage: number;
44
+ setPage: (pageNumber: number) => void;
45
+ next: () => void;
46
+ previous: () => void;
47
+ first: () => void;
48
+ last: () => void;
49
+ };
50
+ type UsePaginationReturn = ReturnType<typeof usePagination>;
51
+
52
+ export { PaginationItemType, type PaginationItemValue, type UsePaginationProps, type UsePaginationReturn, usePagination };
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ PaginationItemType: () => PaginationItemType,
24
+ usePagination: () => usePagination
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_react = require("react");
28
+ var import_i18n = require("@react-aria/i18n");
29
+ var import_shared_utils = require("@sytechui/shared-utils");
30
+ var PaginationItemType = /* @__PURE__ */ ((PaginationItemType2) => {
31
+ PaginationItemType2["DOTS"] = "dots";
32
+ PaginationItemType2["PREV"] = "prev";
33
+ PaginationItemType2["NEXT"] = "next";
34
+ return PaginationItemType2;
35
+ })(PaginationItemType || {});
36
+ function usePagination(props) {
37
+ const {
38
+ page,
39
+ total,
40
+ siblings = 1,
41
+ boundaries = 1,
42
+ initialPage = 1,
43
+ showControls = false,
44
+ onChange
45
+ } = props;
46
+ const [activePage, setActivePage] = (0, import_react.useState)(page || initialPage);
47
+ const { direction } = (0, import_i18n.useLocale)();
48
+ const isRTL = direction === "rtl";
49
+ const onChangeActivePage = (newPage) => {
50
+ setActivePage(newPage);
51
+ onChange && onChange(newPage);
52
+ };
53
+ (0, import_react.useEffect)(() => {
54
+ if (page && page !== activePage) {
55
+ setActivePage(page);
56
+ }
57
+ }, [page]);
58
+ const setPage = (0, import_react.useCallback)(
59
+ (pageNumber) => {
60
+ if (pageNumber <= 0) {
61
+ onChangeActivePage(1);
62
+ } else if (pageNumber > total) {
63
+ onChangeActivePage(total);
64
+ } else {
65
+ onChangeActivePage(pageNumber);
66
+ }
67
+ },
68
+ [total, activePage, onChangeActivePage]
69
+ );
70
+ const next = () => setPage(activePage + 1);
71
+ const previous = () => setPage(activePage - 1);
72
+ const first = () => setPage(1);
73
+ const last = () => setPage(total);
74
+ const formatRange = (0, import_react.useCallback)(
75
+ (range2) => {
76
+ if (showControls) {
77
+ return ["prev" /* PREV */, ...range2, "next" /* NEXT */];
78
+ }
79
+ return range2;
80
+ },
81
+ [isRTL, showControls]
82
+ );
83
+ const paginationRange = (0, import_react.useMemo)(() => {
84
+ const totalPageNumbers = siblings * 2 + 3 + boundaries * 2;
85
+ if (totalPageNumbers >= total) {
86
+ return formatRange((0, import_shared_utils.range)(1, total));
87
+ }
88
+ const leftSiblingIndex = Math.max(activePage - siblings, boundaries);
89
+ const rightSiblingIndex = Math.min(activePage + siblings, total - boundaries);
90
+ const shouldShowLeftDots = leftSiblingIndex > boundaries + 2;
91
+ const shouldShowRightDots = rightSiblingIndex < total - (boundaries + 1);
92
+ if (!shouldShowLeftDots && shouldShowRightDots) {
93
+ const leftItemCount = siblings * 2 + boundaries + 2;
94
+ return formatRange([
95
+ ...(0, import_shared_utils.range)(1, leftItemCount),
96
+ "dots" /* DOTS */,
97
+ ...(0, import_shared_utils.range)(total - (boundaries - 1), total)
98
+ ]);
99
+ }
100
+ if (shouldShowLeftDots && !shouldShowRightDots) {
101
+ const rightItemCount = boundaries + 1 + 2 * siblings;
102
+ return formatRange([
103
+ ...(0, import_shared_utils.range)(1, boundaries),
104
+ "dots" /* DOTS */,
105
+ ...(0, import_shared_utils.range)(total - rightItemCount, total)
106
+ ]);
107
+ }
108
+ return formatRange([
109
+ ...(0, import_shared_utils.range)(1, boundaries),
110
+ "dots" /* DOTS */,
111
+ ...(0, import_shared_utils.range)(leftSiblingIndex, rightSiblingIndex),
112
+ "dots" /* DOTS */,
113
+ ...(0, import_shared_utils.range)(total - boundaries + 1, total)
114
+ ]);
115
+ }, [total, activePage, siblings, boundaries, formatRange]);
116
+ return {
117
+ range: paginationRange,
118
+ activePage,
119
+ setPage,
120
+ next,
121
+ previous,
122
+ first,
123
+ last
124
+ };
125
+ }
126
+ // Annotate the CommonJS export names for ESM import in node:
127
+ 0 && (module.exports = {
128
+ PaginationItemType,
129
+ usePagination
130
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,104 @@
1
+ // src/index.ts
2
+ import { useMemo, useCallback, useState, useEffect } from "react";
3
+ import { useLocale } from "@react-aria/i18n";
4
+ import { range } from "@sytechui/shared-utils";
5
+ var PaginationItemType = /* @__PURE__ */ ((PaginationItemType2) => {
6
+ PaginationItemType2["DOTS"] = "dots";
7
+ PaginationItemType2["PREV"] = "prev";
8
+ PaginationItemType2["NEXT"] = "next";
9
+ return PaginationItemType2;
10
+ })(PaginationItemType || {});
11
+ function usePagination(props) {
12
+ const {
13
+ page,
14
+ total,
15
+ siblings = 1,
16
+ boundaries = 1,
17
+ initialPage = 1,
18
+ showControls = false,
19
+ onChange
20
+ } = props;
21
+ const [activePage, setActivePage] = useState(page || initialPage);
22
+ const { direction } = useLocale();
23
+ const isRTL = direction === "rtl";
24
+ const onChangeActivePage = (newPage) => {
25
+ setActivePage(newPage);
26
+ onChange && onChange(newPage);
27
+ };
28
+ useEffect(() => {
29
+ if (page && page !== activePage) {
30
+ setActivePage(page);
31
+ }
32
+ }, [page]);
33
+ const setPage = useCallback(
34
+ (pageNumber) => {
35
+ if (pageNumber <= 0) {
36
+ onChangeActivePage(1);
37
+ } else if (pageNumber > total) {
38
+ onChangeActivePage(total);
39
+ } else {
40
+ onChangeActivePage(pageNumber);
41
+ }
42
+ },
43
+ [total, activePage, onChangeActivePage]
44
+ );
45
+ const next = () => setPage(activePage + 1);
46
+ const previous = () => setPage(activePage - 1);
47
+ const first = () => setPage(1);
48
+ const last = () => setPage(total);
49
+ const formatRange = useCallback(
50
+ (range2) => {
51
+ if (showControls) {
52
+ return ["prev" /* PREV */, ...range2, "next" /* NEXT */];
53
+ }
54
+ return range2;
55
+ },
56
+ [isRTL, showControls]
57
+ );
58
+ const paginationRange = useMemo(() => {
59
+ const totalPageNumbers = siblings * 2 + 3 + boundaries * 2;
60
+ if (totalPageNumbers >= total) {
61
+ return formatRange(range(1, total));
62
+ }
63
+ const leftSiblingIndex = Math.max(activePage - siblings, boundaries);
64
+ const rightSiblingIndex = Math.min(activePage + siblings, total - boundaries);
65
+ const shouldShowLeftDots = leftSiblingIndex > boundaries + 2;
66
+ const shouldShowRightDots = rightSiblingIndex < total - (boundaries + 1);
67
+ if (!shouldShowLeftDots && shouldShowRightDots) {
68
+ const leftItemCount = siblings * 2 + boundaries + 2;
69
+ return formatRange([
70
+ ...range(1, leftItemCount),
71
+ "dots" /* DOTS */,
72
+ ...range(total - (boundaries - 1), total)
73
+ ]);
74
+ }
75
+ if (shouldShowLeftDots && !shouldShowRightDots) {
76
+ const rightItemCount = boundaries + 1 + 2 * siblings;
77
+ return formatRange([
78
+ ...range(1, boundaries),
79
+ "dots" /* DOTS */,
80
+ ...range(total - rightItemCount, total)
81
+ ]);
82
+ }
83
+ return formatRange([
84
+ ...range(1, boundaries),
85
+ "dots" /* DOTS */,
86
+ ...range(leftSiblingIndex, rightSiblingIndex),
87
+ "dots" /* DOTS */,
88
+ ...range(total - boundaries + 1, total)
89
+ ]);
90
+ }, [total, activePage, siblings, boundaries, formatRange]);
91
+ return {
92
+ range: paginationRange,
93
+ activePage,
94
+ setPage,
95
+ next,
96
+ previous,
97
+ first,
98
+ last
99
+ };
100
+ }
101
+ export {
102
+ PaginationItemType,
103
+ usePagination
104
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@sytechui/use-pagination",
3
+ "version": "2.2.20",
4
+ "description": "State management hook for Pagination component, it lets you manage pagination with controlled and uncontrolled state",
5
+ "keywords": [
6
+ "use-pagination"
7
+ ],
8
+ "author": "HeroUI <support@heroui.com>",
9
+ "homepage": "https://heroui.com",
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "sideEffects": false,
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/heroui-inc/heroui.git",
22
+ "directory": "packages/hooks/use-pagination"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/heroui-inc/heroui/issues"
26
+ },
27
+ "dependencies": {
28
+ "@react-aria/i18n": "3.12.16",
29
+ "@sytechui/shared-utils": "2.1.12"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=18 || >=19.0.0-rc.0"
33
+ },
34
+ "clean-package": "../../../clean-package.config.json",
35
+ "tsup": {
36
+ "clean": true,
37
+ "target": "es2019",
38
+ "format": [
39
+ "cjs",
40
+ "esm"
41
+ ]
42
+ },
43
+ "module": "dist/index.mjs",
44
+ "types": "dist/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.mjs",
49
+ "require": "./dist/index.js"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup src --dts",
55
+ "dev": "pnpm build:fast --watch",
56
+ "clean": "rimraf dist .turbo",
57
+ "typecheck": "tsc --noEmit",
58
+ "build:fast": "tsup src"
59
+ }
60
+ }