@purpurds/pagination 5.19.1
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/dist/LICENSE.txt +73 -0
- package/dist/navigation.d.ts +2 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/pagination-page-selector.d.ts +17 -0
- package/dist/pagination-page-selector.d.ts.map +1 -0
- package/dist/pagination-page-size-selector.d.ts +13 -0
- package/dist/pagination-page-size-selector.d.ts.map +1 -0
- package/dist/pagination-page-trigger.d.ts +14 -0
- package/dist/pagination-page-trigger.d.ts.map +1 -0
- package/dist/pagination-pages.d.ts +13 -0
- package/dist/pagination-pages.d.ts.map +1 -0
- package/dist/pagination-step-trigger.d.ts +14 -0
- package/dist/pagination-step-trigger.d.ts.map +1 -0
- package/dist/pagination-truncation-separator.d.ts +11 -0
- package/dist/pagination-truncation-separator.d.ts.map +1 -0
- package/dist/pagination.cjs.js +134 -0
- package/dist/pagination.cjs.js.map +1 -0
- package/dist/pagination.d.ts +24 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.es.js +4041 -0
- package/dist/pagination.es.js.map +1 -0
- package/dist/styles.css +1 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/use-page-options.hook.d.ts +8 -0
- package/dist/use-page-options.hook.d.ts.map +1 -0
- package/dist/use-page-size-options.hook.d.ts +8 -0
- package/dist/use-page-size-options.hook.d.ts.map +1 -0
- package/dist/use-pagination-page-selector-events.hook.d.ts +10 -0
- package/dist/use-pagination-page-selector-events.hook.d.ts.map +1 -0
- package/dist/use-pagination-pages.hook.d.ts +7 -0
- package/dist/use-pagination-pages.hook.d.ts.map +1 -0
- package/package.json +69 -0
- package/src/global.d.ts +4 -0
- package/src/navigation.ts +3 -0
- package/src/pagination-page-selector.module.scss +26 -0
- package/src/pagination-page-selector.test.tsx +78 -0
- package/src/pagination-page-selector.tsx +105 -0
- package/src/pagination-page-size-selector.module.scss +34 -0
- package/src/pagination-page-size-selector.test.tsx +73 -0
- package/src/pagination-page-size-selector.tsx +78 -0
- package/src/pagination-page-trigger.module.scss +41 -0
- package/src/pagination-page-trigger.test.tsx +172 -0
- package/src/pagination-page-trigger.tsx +93 -0
- package/src/pagination-pages.module.scss +8 -0
- package/src/pagination-pages.test.tsx +95 -0
- package/src/pagination-pages.tsx +72 -0
- package/src/pagination-step-trigger.module.scss +27 -0
- package/src/pagination-step-trigger.test.tsx +106 -0
- package/src/pagination-step-trigger.tsx +92 -0
- package/src/pagination-truncation-separator.module.scss +8 -0
- package/src/pagination-truncation-separator.test.tsx +25 -0
- package/src/pagination-truncation-separator.tsx +29 -0
- package/src/pagination.module.scss +105 -0
- package/src/pagination.stories.tsx +197 -0
- package/src/pagination.test.tsx +76 -0
- package/src/pagination.tsx +177 -0
- package/src/types.ts +43 -0
- package/src/use-page-options.hook.ts +21 -0
- package/src/use-page-size-options.hook.ts +21 -0
- package/src/use-pagination-page-selector-events.hook.ts +59 -0
- package/src/use-pagination-pages.hook.ts +41 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Option } from "@purpurds/autocomplete";
|
|
2
|
+
|
|
3
|
+
type UsePaginationPageSelectorEvents = {
|
|
4
|
+
onKeyUp: (event: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
5
|
+
onSelectionChange: (option?: Option) => void;
|
|
6
|
+
resetSelection: () => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const usePaginationPageSelectorEvents = (
|
|
10
|
+
currentPage: number,
|
|
11
|
+
navigationFunction: (page: number, url: string) => void,
|
|
12
|
+
numberOfPages: number,
|
|
13
|
+
onPageChange: (page: number) => void,
|
|
14
|
+
options: Option[],
|
|
15
|
+
setValue: React.Dispatch<React.SetStateAction<string | number>>,
|
|
16
|
+
asLink?: boolean,
|
|
17
|
+
hrefGetter?: (page: number) => string
|
|
18
|
+
): UsePaginationPageSelectorEvents => {
|
|
19
|
+
const onSelectionChange = (option?: Option) => {
|
|
20
|
+
if (!option) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const page = Number(option.id);
|
|
24
|
+
onPageChange(page);
|
|
25
|
+
setValue(() => page);
|
|
26
|
+
if (asLink) {
|
|
27
|
+
const url = hrefGetter?.(page) ?? "";
|
|
28
|
+
navigationFunction(page, url);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const pageIsValid = (page: number | typeof NaN): page is number => {
|
|
33
|
+
return !Number.isNaN(page) && page > 0 && page <= numberOfPages;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const selectFirstMatchingOption = (page: number | typeof NaN) => {
|
|
37
|
+
if (pageIsValid(page)) {
|
|
38
|
+
const firstMatchingPage = options.find((option) => Number(option.id) === page);
|
|
39
|
+
onSelectionChange(firstMatchingPage);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const onKeyUp = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
44
|
+
if (event.key === "Enter") {
|
|
45
|
+
const page = Number((event.target as HTMLInputElement).value);
|
|
46
|
+
selectFirstMatchingOption(page);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const resetSelection = () => {
|
|
51
|
+
setValue(() => currentPage);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
onSelectionChange,
|
|
56
|
+
onKeyUp,
|
|
57
|
+
resetSelection,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
type UsePaginationPagesHook = {
|
|
4
|
+
pages: number[];
|
|
5
|
+
numberOfPages: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const usePaginationPages = (
|
|
9
|
+
totalItems: number,
|
|
10
|
+
pageSize: number,
|
|
11
|
+
currentPage: number
|
|
12
|
+
): UsePaginationPagesHook => {
|
|
13
|
+
const [pages, setPages] = React.useState<number[]>([]);
|
|
14
|
+
const [numberOfPages, setNumberOfPages] = React.useState<number>(0);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const totalNumberOfPages = Math.ceil(totalItems / pageSize);
|
|
18
|
+
setNumberOfPages(totalNumberOfPages);
|
|
19
|
+
setPages(getPagesToDisplay(totalNumberOfPages, currentPage));
|
|
20
|
+
}, [totalItems, pageSize, currentPage]);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
pages,
|
|
24
|
+
numberOfPages,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function getPagesToDisplay(totalNumberOfPages: number, currentPage: number): number[] {
|
|
29
|
+
if (totalNumberOfPages <= 5) {
|
|
30
|
+
return Array.from({ length: totalNumberOfPages }, (_, i) => i + 1);
|
|
31
|
+
}
|
|
32
|
+
const firstThreePages = [1, 2, 3];
|
|
33
|
+
if (firstThreePages.includes(currentPage)) {
|
|
34
|
+
return [...firstThreePages, 4, totalNumberOfPages];
|
|
35
|
+
}
|
|
36
|
+
const lastThreePages = [totalNumberOfPages - 2, totalNumberOfPages - 1, totalNumberOfPages];
|
|
37
|
+
if (lastThreePages.includes(currentPage)) {
|
|
38
|
+
return [1, totalNumberOfPages - 3, ...lastThreePages];
|
|
39
|
+
}
|
|
40
|
+
return [1, currentPage - 1, currentPage, currentPage + 1, totalNumberOfPages];
|
|
41
|
+
}
|