@bodynarf/react.components 1.1.6 → 1.2.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/components/anchor/components/anchorWithIcon/index.d.ts +0 -1
- package/components/anchor/components/anchorWithIcon/index.d.ts.map +1 -1
- package/components/button/components/buttonWithIcon/index.d.ts +0 -1
- package/components/button/components/buttonWithIcon/index.d.ts.map +1 -1
- package/components/button/index.d.ts +2 -2
- package/components/button/index.d.ts.map +1 -1
- package/components/dropdown/index.d.ts +0 -1
- package/components/dropdown/index.d.ts.map +1 -1
- package/components/icon/index.d.ts +2 -2
- package/components/icon/index.d.ts.map +1 -1
- package/components/paginator/index.d.ts +29 -0
- package/components/paginator/index.d.ts.map +1 -0
- package/components/paginator/index.js +47 -0
- package/components/paginator/utils.d.ts +10 -0
- package/components/paginator/utils.d.ts.map +1 -0
- package/components/paginator/utils.js +18 -0
- package/components/types.d.ts +5 -3
- package/components/types.d.ts.map +1 -1
- package/hooks/index.d.ts +1 -0
- package/hooks/index.d.ts.map +1 -1
- package/hooks/index.js +1 -0
- package/hooks/usePagination.d.ts +18 -0
- package/hooks/usePagination.d.ts.map +1 -0
- package/hooks/usePagination.js +26 -0
- package/package.json +8 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/components/anchor/components/anchorWithIcon/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/components/anchor/components/anchorWithIcon/index.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,iCAAiC;AACjC,eAAO,MAAM,cAAc,+DAAgE,mBAAmB,KAAG,WAqChH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/components/button/components/buttonWithIcon/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/components/button/components/buttonWithIcon/index.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,iCAAiC;AACjC,eAAO,MAAM,cAAc,2DAA4D,mBAAmB,KAAG,WAsC5G,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import './button.scss';
|
|
3
|
-
import { ElementIcon,
|
|
3
|
+
import { ElementIcon, ElementSize } from '../types';
|
|
4
4
|
import { ButtonType } from './types';
|
|
5
5
|
export declare type ButtonProps = {
|
|
6
6
|
/** Button displaying text */
|
|
@@ -10,7 +10,7 @@ export declare type ButtonProps = {
|
|
|
10
10
|
/** Configuration of inner icon */
|
|
11
11
|
icon?: ElementIcon;
|
|
12
12
|
/** Button size */
|
|
13
|
-
size?:
|
|
13
|
+
size?: ElementSize;
|
|
14
14
|
/** Title on hover */
|
|
15
15
|
title?: string;
|
|
16
16
|
/** Is button uses light version of color */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/button/index.tsx"],"names":[],"mappings":";AAEA,OAAO,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/button/index.tsx"],"names":[],"mappings":";AAEA,OAAO,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEpD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAIrC,oBAAY,WAAW,GAAG;IACtB,6BAA6B;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,8BAA8B;IAC9B,IAAI,EAAE,UAAU,CAAC;IAEjB,kCAAkC;IAClC,IAAI,CAAC,EAAE,WAAW,CAAC;IAEnB,mBAAmB;IACnB,IAAI,CAAC,EAAE,WAAW,CAAC;IAEnB,qBAAqB;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,6CAA6C;IAC7C,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB,yBAAyB;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,wCAAwC;IACxC,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,2BAA2B;IAC3B,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,yBAAyB;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,CAAC,OAAO,CAkC9D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/dropdown/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/dropdown/index.tsx"],"names":[],"mappings":"AAIA,OAAO,iBAAiB,CAAC;AAIzB,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAK5C,oBAAY,aAAa,GAAG,gBAAgB,GAAG;IAC3C,kCAAkC;IAClC,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;IAE7B;;;MAGE;IACF,KAAK,CAAC,EAAE,cAAc,CAAC;IAEvB;;;OAGG;IACH,QAAQ,EAAE,CAAC,IAAI,CAAC,EAAE,cAAc,KAAK,IAAI,CAAC;IAE1C;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB,gEAAgE;IAChE,gBAAgB,EAAE,OAAO,CAAC;IAE1B,wBAAwB;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAA;AAED,yBAAyB;AACzB,QAAA,MAAM,QAAQ,mFAAoF,aAAa,KAAG,WA2FjH,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import './icon.scss';
|
|
3
|
-
import {
|
|
3
|
+
import { ElementSize } from '../types';
|
|
4
4
|
/** Icon component props */
|
|
5
5
|
export declare type IconProps = {
|
|
6
6
|
/**
|
|
@@ -9,7 +9,7 @@ export declare type IconProps = {
|
|
|
9
9
|
*/
|
|
10
10
|
className: string;
|
|
11
11
|
/** Icon size */
|
|
12
|
-
size?:
|
|
12
|
+
size?: ElementSize;
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
15
|
* Icon component. Based on bootstrap icons
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/icon/index.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAC;AAErB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/icon/index.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAC;AAErB,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,2BAA2B;AAC3B,oBAAY,SAAS,GAAG;IACpB;;;MAGE;IACF,SAAS,EAAE,MAAM,CAAC;IAElB,gBAAgB;IAChB,IAAI,CAAC,EAAE,WAAW,CAAC;CACtB,CAAA;AAQD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAAC,OAAO,CAQ1D"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { ElementSize } from '../types';
|
|
3
|
+
export declare type PaginatorProps = {
|
|
4
|
+
/** Amount of pages */
|
|
5
|
+
count: number;
|
|
6
|
+
/** Page change handler */
|
|
7
|
+
onPageChange: (page: number) => void;
|
|
8
|
+
/** Current page. Starts from 1 */
|
|
9
|
+
currentPage?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Page numbers position.
|
|
12
|
+
* Usefull with showNextButtons = true
|
|
13
|
+
*/
|
|
14
|
+
position?: 'left' | 'center' | 'right';
|
|
15
|
+
/** Buttons should have rounded borders */
|
|
16
|
+
rounded?: boolean;
|
|
17
|
+
/** Size of paginator component elements */
|
|
18
|
+
size?: ElementSize;
|
|
19
|
+
/** Additional class names */
|
|
20
|
+
className?: string;
|
|
21
|
+
/** Display "Previous" \ "Next" buttons */
|
|
22
|
+
showNextButtons?: boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Paginator component.
|
|
26
|
+
* Used for visualization of pagging configuration
|
|
27
|
+
*/
|
|
28
|
+
export default function Paginator({ count, onPageChange, currentPage, position, rounded, size, className, showNextButtons, }: PaginatorProps): JSX.Element;
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/paginator/index.tsx"],"names":[],"mappings":";AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAIvC,oBAAY,cAAc,GAAG;IACzB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAC;IAEd,0BAA0B;IAC1B,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAErC,kCAAkC;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;MAGE;IACF,QAAQ,CAAC,EACP,MAAM,GACN,QAAQ,GACR,OAAO,CACR;IAED,0CAA0C;IAC1C,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,2CAA2C;IAC3C,IAAI,CAAC,EAAE,WAAW,CAAC;IAEnB,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,0CAA0C;IAC1C,eAAe,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAA;AAED;;;EAGE;AACF,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,EAC9B,KAAK,EAAE,YAAY,EAAE,WAAW,EAChC,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAClC,eAAe,GAClB,EAAE,cAAc,GAAG,GAAG,CAAC,OAAO,CA4G9B"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useMemo } from 'react';
|
|
3
|
+
import { getClassName, isNullOrEmpty } from '@bodynarf/utils';
|
|
4
|
+
import { generatePageNumbers } from './utils';
|
|
5
|
+
/**
|
|
6
|
+
* Paginator component.
|
|
7
|
+
* Used for visualization of pagging configuration
|
|
8
|
+
*/
|
|
9
|
+
export default function Paginator({ count, onPageChange, currentPage, position, rounded, size, className, showNextButtons, }) {
|
|
10
|
+
const page = currentPage || 0;
|
|
11
|
+
const pageChange = useCallback((event) => {
|
|
12
|
+
const target = event.target;
|
|
13
|
+
const pageRaw = target.dataset['page'];
|
|
14
|
+
if (isNullOrEmpty(pageRaw)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const page = +pageRaw;
|
|
18
|
+
if (page !== currentPage
|
|
19
|
+
&& page > 0 && page <= count) {
|
|
20
|
+
onPageChange(page);
|
|
21
|
+
}
|
|
22
|
+
}, [onPageChange, currentPage, count]);
|
|
23
|
+
const pageNumbers = useMemo(() => generatePageNumbers(page, count), [page, count]);
|
|
24
|
+
const canGoBack = useMemo(() => page > 1, [page]);
|
|
25
|
+
const canGoForward = useMemo(() => page < count, [page, count]);
|
|
26
|
+
if (pageNumbers.length === 0) {
|
|
27
|
+
return _jsx(_Fragment, {});
|
|
28
|
+
}
|
|
29
|
+
const classNames = getClassName([
|
|
30
|
+
"pagination",
|
|
31
|
+
paginationPositionToClassMap.has(position || "") ? paginationPositionToClassMap.get(position || "") : "",
|
|
32
|
+
rounded == true ? "is-rounded" : "",
|
|
33
|
+
isNullOrEmpty(size) ? "" : `is-${size}`,
|
|
34
|
+
className
|
|
35
|
+
]);
|
|
36
|
+
return (_jsxs("nav", { className: classNames, role: "navigation", "aria-label": "pagination", children: [showNextButtons === true &&
|
|
37
|
+
_jsxs(_Fragment, { children: [_jsx("a", { className: `pagination-previous${canGoBack ? "" : " is-disabled"}`, "data-page": page - 1, onClick: pageChange, children: "Previous" }), _jsx("a", { className: `pagination-next${canGoForward ? "" : " is-disabled"}`, "data-page": page + 1, onClick: pageChange, children: "Next page" })] }), _jsxs("ul", { className: "pagination-list", children: [page !== 1 && !pageNumbers.includes(1) &&
|
|
38
|
+
_jsxs(_Fragment, { children: [_jsx("li", { children: _jsx("a", { className: "pagination-link", "aria-label": "Goto page 1", "data-page": 1, onClick: pageChange, children: "1" }) }), _jsx("li", { children: _jsx("span", { className: "pagination-ellipsis", children: "\u2026" }) })] }), pageNumbers.map(x => _jsx("li", { children: _jsx("a", { className: `pagination-link${page === x ? " is-current" : ""}`, "aria-label": `Goto page ${x}`, "data-page": x, onClick: pageChange, children: x }) }, x)), page != count && !pageNumbers.includes(count) &&
|
|
39
|
+
_jsxs(_Fragment, { children: [_jsx("li", { children: _jsx("span", { className: "pagination-ellipsis", children: "\u2026" }) }), _jsx("li", { children: _jsx("a", { className: "pagination-link", "aria-label": `Goto page ${count}`, "data-page": count, onClick: pageChange, children: count }) })] })] })] }));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Position setting to css class name map
|
|
43
|
+
*/
|
|
44
|
+
const paginationPositionToClassMap = new Map([
|
|
45
|
+
["center", "is-centered"],
|
|
46
|
+
["right", "is-right"]
|
|
47
|
+
]);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get nearest numbers from each side (left & right)
|
|
3
|
+
* @param page Number of current page
|
|
4
|
+
* @param count Amount of pages
|
|
5
|
+
* @param size Amount of pages from left & right to current page. Default is 3
|
|
6
|
+
* @throws Current page is greater than pages amount
|
|
7
|
+
* @returns Array of nearest numbers to current page
|
|
8
|
+
*/
|
|
9
|
+
export declare const generatePageNumbers: (page: number, count: number, size?: number) => Array<number>;
|
|
10
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/components/paginator/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,SAAU,MAAM,SAAS,MAAM,SAAQ,MAAM,KAAO,MAAM,MAAM,CAU/F,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get nearest numbers from each side (left & right)
|
|
3
|
+
* @param page Number of current page
|
|
4
|
+
* @param count Amount of pages
|
|
5
|
+
* @param size Amount of pages from left & right to current page. Default is 3
|
|
6
|
+
* @throws Current page is greater than pages amount
|
|
7
|
+
* @returns Array of nearest numbers to current page
|
|
8
|
+
*/
|
|
9
|
+
export const generatePageNumbers = (page, count, size = 3) => {
|
|
10
|
+
if (page < 0 || count <= 0 || page > count || size > count) {
|
|
11
|
+
throw new Error("getPageNumbers has ivalid config");
|
|
12
|
+
}
|
|
13
|
+
return [
|
|
14
|
+
...new Array(size).fill(page).map((_, i) => page - i - 1).filter(x => x > 0 && x < page).reverse(),
|
|
15
|
+
page,
|
|
16
|
+
...new Array(size).fill(page).map((_, i) => page + i + 1).filter(x => x > 0 && x > page && x <= count)
|
|
17
|
+
];
|
|
18
|
+
};
|
package/components/types.d.ts
CHANGED
|
@@ -10,9 +10,11 @@ export declare type BaseElementProps = {
|
|
|
10
10
|
[key: string]: any;
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
/** Allowed element sizes */
|
|
14
|
+
export declare type ElementSize = 'small' | 'medium' /** default size */ | 'large';
|
|
15
|
+
/** Allowed icon position */
|
|
14
16
|
export declare type IconPosition = 'left' | 'right';
|
|
15
|
-
/** Icon for component
|
|
17
|
+
/** Icon for component */
|
|
16
18
|
export declare type ElementIcon = {
|
|
17
19
|
/**
|
|
18
20
|
* Class name for icon.
|
|
@@ -20,7 +22,7 @@ export declare type ElementIcon = {
|
|
|
20
22
|
*/
|
|
21
23
|
className: string;
|
|
22
24
|
/** Icon size */
|
|
23
|
-
size?:
|
|
25
|
+
size?: ElementSize;
|
|
24
26
|
/**
|
|
25
27
|
* Position
|
|
26
28
|
* Works only with other content
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/components/types.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,oBAAY,gBAAgB,GAAG;IAC3B,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,8BAA8B;IAC9B,IAAI,CAAC,EAAE;QACH,8CAA8C;QAC9C,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACtB,CAAC;CACL,CAAC;AAEF,oBAAY,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/components/types.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,oBAAY,gBAAgB,GAAG;IAC3B,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,8BAA8B;IAC9B,IAAI,CAAC,EAAE;QACH,8CAA8C;QAC9C,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACtB,CAAC;CACL,CAAC;AAEF,4BAA4B;AAC5B,oBAAY,WAAW,GACjB,OAAO,GACP,QAAQ,CAAC,mBAAmB,GAC5B,OAAO,CAAC;AAEd,4BAA4B;AAC5B,oBAAY,YAAY,GAClB,MAAM,GACN,OAAO,CAAC;AAEd,0BAA0B;AAC1B,oBAAY,WAAW,GAAG;IACtB;;;MAGE;IACF,SAAS,EAAE,MAAM,CAAC;IAElB,gBAAgB;IAChB,IAAI,CAAC,EAAE,WAAW,CAAC;IAEnB;;;MAGE;IACF,QAAQ,CAAC,EAAE,YAAY,CAAC;CAC3B,CAAC"}
|
package/hooks/index.d.ts
CHANGED
package/hooks/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iBAAiB,CAAC"}
|
package/hooks/index.js
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Paginator hook state */
|
|
2
|
+
export interface PaginationState {
|
|
3
|
+
/** Number of current page */
|
|
4
|
+
currentPage: number;
|
|
5
|
+
/** Amount of pages */
|
|
6
|
+
pagesCount: number;
|
|
7
|
+
/** Handler of page change */
|
|
8
|
+
onPageChange: (page: number) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hook to pagination state, return hooked values and handler for pagination
|
|
12
|
+
* @param length Pagionation items count
|
|
13
|
+
* @param size Page size. Default is 30
|
|
14
|
+
* @param initPage Initial page. Default is 1
|
|
15
|
+
* @returns Pair of hook-stored state and handler for slicing current page items
|
|
16
|
+
*/
|
|
17
|
+
export declare const usePagination: (length: number, size?: number, initPage?: number) => [PaginationState, (data: Array<any>) => Array<any>];
|
|
18
|
+
//# sourceMappingURL=usePagination.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePagination.d.ts","sourceRoot":"","sources":["../../src/hooks/usePagination.ts"],"names":[],"mappings":"AAEA,2BAA2B;AAC3B,MAAM,WAAW,eAAe;IAC5B,6BAA6B;IAC7B,WAAW,EAAE,MAAM,CAAC;IAEpB,sBAAsB;IACtB,UAAU,EAAE,MAAM,CAAC;IAEnB,6BAA6B;IAC7B,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CACxC;AAKD;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,WAAY,MAAM,SAAQ,MAAM,aAA8B,MAAM,8BAAgC,MAAM,GAAG,CAAC,KAAK,MAAM,GAAG,CAAC,CAsBtJ,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useCallback, useMemo, useState } from "react";
|
|
2
|
+
/** Default page size */
|
|
3
|
+
const defaultPageSize = 30;
|
|
4
|
+
/**
|
|
5
|
+
* Hook to pagination state, return hooked values and handler for pagination
|
|
6
|
+
* @param length Pagionation items count
|
|
7
|
+
* @param size Page size. Default is 30
|
|
8
|
+
* @param initPage Initial page. Default is 1
|
|
9
|
+
* @returns Pair of hook-stored state and handler for slicing current page items
|
|
10
|
+
*/
|
|
11
|
+
export const usePagination = (length, size = defaultPageSize, initPage = 1) => {
|
|
12
|
+
const [currentPage, setCurrentPage] = useState(initPage);
|
|
13
|
+
const count = useMemo(() => Math.ceil(length / size), [size, length]);
|
|
14
|
+
const onChange = useCallback((page) => setCurrentPage(page), [setCurrentPage]);
|
|
15
|
+
const paginate = useCallback((data) => {
|
|
16
|
+
const limit = size;
|
|
17
|
+
const offstet = (currentPage - 1) * size;
|
|
18
|
+
return data.slice(offstet, offstet + limit);
|
|
19
|
+
}, [size, currentPage]);
|
|
20
|
+
const state = useMemo(() => ({
|
|
21
|
+
currentPage: currentPage,
|
|
22
|
+
pagesCount: count,
|
|
23
|
+
onPageChange: onChange
|
|
24
|
+
}), [currentPage, count, onChange]);
|
|
25
|
+
return [state, paginate];
|
|
26
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bodynarf/react.components",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Artem",
|
|
6
6
|
"email": "bodynar@gmail.com"
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"react component",
|
|
21
21
|
"bulma"
|
|
22
22
|
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "npx tsc",
|
|
25
|
+
"publish": "npx tsc && copy package.json dist && cd dist && npm publish --access=public"
|
|
26
|
+
},
|
|
23
27
|
"devDependencies": {
|
|
24
28
|
"@types/react": "^18.0.11",
|
|
25
29
|
"@types/react-dom": "^18.0.5",
|
|
@@ -27,8 +31,8 @@
|
|
|
27
31
|
},
|
|
28
32
|
"dependencies": {
|
|
29
33
|
"@bodynarf/utils": "^1.0.10",
|
|
34
|
+
"bulma": "^0.9.4",
|
|
30
35
|
"react": "^18.1.0",
|
|
31
|
-
"react-dom": "^18.1.0"
|
|
32
|
-
"bulma": "^0.9.4"
|
|
36
|
+
"react-dom": "^18.1.0"
|
|
33
37
|
}
|
|
34
|
-
}
|
|
38
|
+
}
|