@cloud-ru/ds-pagination 1.0.0
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/CHANGELOG.md +8 -0
- package/LICENSE +201 -0
- package/README.md +174 -0
- package/dist/cjs/components/Pagination/Pagination.d.ts +24 -0
- package/dist/cjs/components/Pagination/Pagination.js +64 -0
- package/dist/cjs/components/Pagination/index.d.ts +1 -0
- package/dist/cjs/components/Pagination/index.js +17 -0
- package/dist/cjs/components/Pagination/styles.module.css +19 -0
- package/dist/cjs/components/PaginationNumberItem/PaginationNumberItem.d.ts +10 -0
- package/dist/cjs/components/PaginationNumberItem/PaginationNumberItem.js +18 -0
- package/dist/cjs/components/PaginationNumberItem/index.d.ts +1 -0
- package/dist/cjs/components/PaginationNumberItem/index.js +17 -0
- package/dist/cjs/components/PaginationNumberItem/styles.module.css +149 -0
- package/dist/cjs/components/PaginationSlider/PaginationSlider.d.ts +17 -0
- package/dist/cjs/components/PaginationSlider/PaginationSlider.js +16 -0
- package/dist/cjs/components/PaginationSlider/index.d.ts +1 -0
- package/dist/cjs/components/PaginationSlider/index.js +17 -0
- package/dist/cjs/components/PaginationSlider/styles.module.css +25 -0
- package/dist/cjs/components/PaginationSliderItem/PaginationSliderItem.d.ts +10 -0
- package/dist/cjs/components/PaginationSliderItem/PaginationSliderItem.js +12 -0
- package/dist/cjs/components/PaginationSliderItem/index.d.ts +1 -0
- package/dist/cjs/components/PaginationSliderItem/index.js +17 -0
- package/dist/cjs/components/PaginationSliderItem/styles.module.css +102 -0
- package/dist/cjs/components/index.d.ts +2 -0
- package/dist/cjs/components/index.js +18 -0
- package/dist/cjs/constants.d.ts +31 -0
- package/dist/cjs/constants.js +37 -0
- package/dist/cjs/contexts.d.ts +7 -0
- package/dist/cjs/contexts.js +11 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/style.css +295 -0
- package/dist/cjs/types.d.ts +5 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utils/getPaginationEntries.d.ts +20 -0
- package/dist/cjs/utils/getPaginationEntries.js +85 -0
- package/dist/cjs/utils/getRange.d.ts +1 -0
- package/dist/cjs/utils/getRange.js +6 -0
- package/dist/cjs/utils/index.d.ts +2 -0
- package/dist/cjs/utils/index.js +18 -0
- package/dist/esm/components/Pagination/Pagination.d.ts +24 -0
- package/dist/esm/components/Pagination/Pagination.js +58 -0
- package/dist/esm/components/Pagination/index.d.ts +1 -0
- package/dist/esm/components/Pagination/index.js +1 -0
- package/dist/esm/components/Pagination/styles.module.css +19 -0
- package/dist/esm/components/PaginationNumberItem/PaginationNumberItem.d.ts +10 -0
- package/dist/esm/components/PaginationNumberItem/PaginationNumberItem.js +12 -0
- package/dist/esm/components/PaginationNumberItem/index.d.ts +1 -0
- package/dist/esm/components/PaginationNumberItem/index.js +1 -0
- package/dist/esm/components/PaginationNumberItem/styles.module.css +149 -0
- package/dist/esm/components/PaginationSlider/PaginationSlider.d.ts +17 -0
- package/dist/esm/components/PaginationSlider/PaginationSlider.js +10 -0
- package/dist/esm/components/PaginationSlider/index.d.ts +1 -0
- package/dist/esm/components/PaginationSlider/index.js +1 -0
- package/dist/esm/components/PaginationSlider/styles.module.css +25 -0
- package/dist/esm/components/PaginationSliderItem/PaginationSliderItem.d.ts +10 -0
- package/dist/esm/components/PaginationSliderItem/PaginationSliderItem.js +6 -0
- package/dist/esm/components/PaginationSliderItem/index.d.ts +1 -0
- package/dist/esm/components/PaginationSliderItem/index.js +1 -0
- package/dist/esm/components/PaginationSliderItem/styles.module.css +102 -0
- package/dist/esm/components/index.d.ts +2 -0
- package/dist/esm/components/index.js +2 -0
- package/dist/esm/constants.d.ts +31 -0
- package/dist/esm/constants.js +31 -0
- package/dist/esm/contexts.d.ts +7 -0
- package/dist/esm/contexts.js +7 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/style.css +295 -0
- package/dist/esm/types.d.ts +5 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/utils/getPaginationEntries.d.ts +20 -0
- package/dist/esm/utils/getPaginationEntries.js +81 -0
- package/dist/esm/utils/getRange.d.ts +1 -0
- package/dist/esm/utils/getRange.js +3 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/package.json +44 -0
- package/src/components/Pagination/Pagination.tsx +162 -0
- package/src/components/Pagination/index.ts +1 -0
- package/src/components/Pagination/styles.module.scss +27 -0
- package/src/components/PaginationNumberItem/PaginationNumberItem.tsx +59 -0
- package/src/components/PaginationNumberItem/index.ts +1 -0
- package/src/components/PaginationNumberItem/styles.module.scss +82 -0
- package/src/components/PaginationSlider/PaginationSlider.tsx +50 -0
- package/src/components/PaginationSlider/index.ts +1 -0
- package/src/components/PaginationSlider/styles.module.scss +31 -0
- package/src/components/PaginationSliderItem/PaginationSliderItem.tsx +29 -0
- package/src/components/PaginationSliderItem/index.ts +1 -0
- package/src/components/PaginationSliderItem/styles.module.scss +137 -0
- package/src/components/index.ts +2 -0
- package/src/constants.ts +35 -0
- package/src/contexts.ts +16 -0
- package/src/index.ts +4 -0
- package/src/types.ts +7 -0
- package/src/utils/getPaginationEntries.ts +118 -0
- package/src/utils/getRange.ts +3 -0
- package/src/utils/index.ts +2 -0
package/src/contexts.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { PAGINATION_SIZE, VARIANT } from './constants';
|
|
4
|
+
import { PaginationSize, Variant } from './types';
|
|
5
|
+
|
|
6
|
+
export type PaginationContextValue = {
|
|
7
|
+
size: PaginationSize;
|
|
8
|
+
variant: Variant;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const PaginationContext = createContext<PaginationContextValue>({
|
|
12
|
+
size: PAGINATION_SIZE.S,
|
|
13
|
+
variant: VARIANT.Button,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const usePaginationContext = () => useContext(PaginationContext);
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValueOf } from '@cloud-ru/ds-utils';
|
|
2
|
+
|
|
3
|
+
import { PAGINATION_SIZE, PAGINATION_SLIDER_SIZE, VARIANT } from './constants';
|
|
4
|
+
|
|
5
|
+
export type PaginationSize = ValueOf<typeof PAGINATION_SIZE>;
|
|
6
|
+
export type PaginationSliderSize = ValueOf<typeof PAGINATION_SLIDER_SIZE>;
|
|
7
|
+
export type Variant = ValueOf<typeof VARIANT>;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { getRange } from './getRange';
|
|
2
|
+
|
|
3
|
+
export enum PaginationEntryKind {
|
|
4
|
+
Page = 'page',
|
|
5
|
+
Break = 'break',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type PaginationEntry =
|
|
9
|
+
| { kind: PaginationEntryKind.Page; page: number }
|
|
10
|
+
| { kind: PaginationEntryKind.Break; start: number; end: number };
|
|
11
|
+
|
|
12
|
+
function createPageEntry(page: number): PaginationEntry {
|
|
13
|
+
return { kind: PaginationEntryKind.Page, page };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createBreakEntry(start: number, end: number): PaginationEntry {
|
|
17
|
+
return { kind: PaginationEntryKind.Break, start, end };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type PaginationOptions = {
|
|
21
|
+
firstPage: number;
|
|
22
|
+
lastPage: number;
|
|
23
|
+
currentPage: number;
|
|
24
|
+
maxLength: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
enum GroupPosition {
|
|
28
|
+
Left = 'left',
|
|
29
|
+
Middle = 'middle',
|
|
30
|
+
Right = 'right',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type PaginationState = {
|
|
34
|
+
firstPage: number;
|
|
35
|
+
lastPage: number;
|
|
36
|
+
currentPage: number;
|
|
37
|
+
currentPageSiblingsCount: number;
|
|
38
|
+
displayedPagesCount: number;
|
|
39
|
+
groupPosition: GroupPosition | null;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function getPaginationState(options: PaginationOptions): PaginationState {
|
|
43
|
+
const { firstPage, lastPage, currentPage, maxLength } = options;
|
|
44
|
+
const currentPageSiblingsCount = maxLength - 5; /* First page, break, current page, break and last page. */
|
|
45
|
+
const displayedPagesCount = maxLength - 2; /* First or last page and break. */
|
|
46
|
+
|
|
47
|
+
function getGroupPosition() {
|
|
48
|
+
if (lastPage <= maxLength) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (currentPage < displayedPagesCount) {
|
|
53
|
+
return GroupPosition.Left;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (lastPage + firstPage - currentPage < displayedPagesCount) {
|
|
57
|
+
return GroupPosition.Right;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return GroupPosition.Middle;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
firstPage,
|
|
65
|
+
lastPage,
|
|
66
|
+
currentPage,
|
|
67
|
+
currentPageSiblingsCount,
|
|
68
|
+
displayedPagesCount,
|
|
69
|
+
groupPosition: getGroupPosition(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getEntriesGroupedOnTheLeft(state: PaginationState) {
|
|
74
|
+
const { firstPage, lastPage, displayedPagesCount } = state;
|
|
75
|
+
const groupFirstPage = firstPage;
|
|
76
|
+
const groupLastPage = displayedPagesCount;
|
|
77
|
+
const group = getRange(groupFirstPage, groupLastPage).map(createPageEntry);
|
|
78
|
+
|
|
79
|
+
return [...group, createBreakEntry(groupLastPage + 1, lastPage - 1), createPageEntry(lastPage)];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getEntriesGroupedInTheMiddle(state: PaginationState) {
|
|
83
|
+
const { firstPage, lastPage, currentPage, currentPageSiblingsCount } = state;
|
|
84
|
+
const groupFirstPage = Math.floor(currentPage - currentPageSiblingsCount / 2);
|
|
85
|
+
const groupLastPage = Math.floor(currentPage + currentPageSiblingsCount / 2);
|
|
86
|
+
const group = getRange(groupFirstPage, groupLastPage).map(createPageEntry);
|
|
87
|
+
|
|
88
|
+
return [
|
|
89
|
+
createPageEntry(firstPage),
|
|
90
|
+
createBreakEntry(firstPage + 1, groupFirstPage - 1),
|
|
91
|
+
...group,
|
|
92
|
+
createBreakEntry(groupLastPage + 1, lastPage - 1),
|
|
93
|
+
createPageEntry(lastPage),
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getEntriesGroupedOnTheRight(state: PaginationState) {
|
|
98
|
+
const { firstPage, lastPage, displayedPagesCount } = state;
|
|
99
|
+
const groupFirstPage = lastPage + firstPage - displayedPagesCount;
|
|
100
|
+
const groupLastPage = lastPage;
|
|
101
|
+
const group = getRange(groupFirstPage, groupLastPage).map(createPageEntry);
|
|
102
|
+
|
|
103
|
+
return [createPageEntry(firstPage), createBreakEntry(firstPage + 1, groupFirstPage - 1), ...group];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const getGroupedEntriesByGroupPosition: Record<GroupPosition, (state: PaginationState) => PaginationEntry[]> = {
|
|
107
|
+
[GroupPosition.Left]: getEntriesGroupedOnTheLeft,
|
|
108
|
+
[GroupPosition.Middle]: getEntriesGroupedInTheMiddle,
|
|
109
|
+
[GroupPosition.Right]: getEntriesGroupedOnTheRight,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export function getPaginationEntries(options: PaginationOptions) {
|
|
113
|
+
const state = getPaginationState(options);
|
|
114
|
+
|
|
115
|
+
return state.groupPosition === null
|
|
116
|
+
? getRange(state.firstPage, state.lastPage).map(createPageEntry)
|
|
117
|
+
: getGroupedEntriesByGroupPosition[state.groupPosition](state);
|
|
118
|
+
}
|