@patternfly/react-data-view 5.0.0 → 5.1.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/dist/cjs/Hooks/pagination.d.ts +13 -1
- package/dist/cjs/Hooks/pagination.js +36 -4
- package/dist/cjs/Hooks/pagination.test.js +53 -1
- package/dist/esm/Hooks/pagination.d.ts +13 -1
- package/dist/esm/Hooks/pagination.js +36 -4
- package/dist/esm/Hooks/pagination.test.js +53 -1
- package/package.json +1 -1
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +11 -2
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +12 -4
- package/release.config.js +1 -1
- package/src/Hooks/pagination.test.tsx +81 -1
- package/src/Hooks/pagination.ts +71 -15
|
@@ -1,14 +1,26 @@
|
|
|
1
|
+
export declare enum PaginationParams {
|
|
2
|
+
PAGE = "page",
|
|
3
|
+
PER_PAGE = "perPage"
|
|
4
|
+
}
|
|
1
5
|
export interface UseDataViewPaginationProps {
|
|
2
6
|
/** Initial page */
|
|
3
7
|
page?: number;
|
|
4
8
|
/** Items per page */
|
|
5
9
|
perPage: number;
|
|
10
|
+
/** Current search parameters as a string */
|
|
11
|
+
searchParams?: URLSearchParams;
|
|
12
|
+
/** Function to set search parameters */
|
|
13
|
+
setSearchParams?: (params: URLSearchParams) => void;
|
|
14
|
+
/** Custom URL parameter name for page */
|
|
15
|
+
pageParam?: string;
|
|
16
|
+
/** Custom URL parameter name for per page */
|
|
17
|
+
perPageParam?: string;
|
|
6
18
|
}
|
|
7
19
|
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
|
|
8
20
|
/** Current page number */
|
|
9
21
|
page: number;
|
|
10
22
|
}
|
|
11
|
-
export declare const useDataViewPagination: ({ page, perPage }: UseDataViewPaginationProps) => {
|
|
23
|
+
export declare const useDataViewPagination: ({ page, perPage, searchParams, setSearchParams, pageParam, perPageParam, }: UseDataViewPaginationProps) => {
|
|
12
24
|
onPerPageSelect: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPerPage: number) => void;
|
|
13
25
|
onSetPage: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPage: number) => void;
|
|
14
26
|
page: number;
|
|
@@ -1,13 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useDataViewPagination = void 0;
|
|
3
|
+
exports.useDataViewPagination = exports.PaginationParams = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
var PaginationParams;
|
|
6
|
+
(function (PaginationParams) {
|
|
7
|
+
PaginationParams["PAGE"] = "page";
|
|
8
|
+
PaginationParams["PER_PAGE"] = "perPage";
|
|
9
|
+
})(PaginationParams || (exports.PaginationParams = PaginationParams = {}));
|
|
10
|
+
const useDataViewPagination = ({ page = 1, perPage, searchParams, setSearchParams, pageParam = PaginationParams.PAGE, perPageParam = PaginationParams.PER_PAGE, }) => {
|
|
11
|
+
const [state, setState] = (0, react_1.useState)({
|
|
12
|
+
page: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${page}`),
|
|
13
|
+
perPage: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${perPage}`),
|
|
14
|
+
});
|
|
15
|
+
const updateSearchParams = (page, perPage) => {
|
|
16
|
+
if (searchParams && setSearchParams) {
|
|
17
|
+
const params = new URLSearchParams(searchParams);
|
|
18
|
+
params.set(pageParam, `${page}`);
|
|
19
|
+
params.set(perPageParam, `${perPage}`);
|
|
20
|
+
setSearchParams(params);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
(0, react_1.useEffect)(() => {
|
|
24
|
+
// Make sure search params are loaded or set if not present on mount
|
|
25
|
+
updateSearchParams(state.page, state.perPage);
|
|
26
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
+
}, []);
|
|
28
|
+
(0, react_1.useEffect)(() => {
|
|
29
|
+
// Listen on URL params changes
|
|
30
|
+
const currentPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${state.page}`);
|
|
31
|
+
const currentPerPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${state.perPage}`);
|
|
32
|
+
if (currentPage !== state.page || currentPerPage !== state.perPage) {
|
|
33
|
+
setState({ page: currentPage, perPage: currentPerPage });
|
|
34
|
+
}
|
|
35
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
+
}, [searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
|
|
7
37
|
const onPerPageSelect = (_event, newPerPage) => {
|
|
8
|
-
|
|
38
|
+
updateSearchParams(1, newPerPage);
|
|
39
|
+
setState({ perPage: newPerPage, page: 1 });
|
|
9
40
|
};
|
|
10
41
|
const onSetPage = (_event, newPage) => {
|
|
42
|
+
updateSearchParams(newPage, state.perPage);
|
|
11
43
|
setState(prev => (Object.assign(Object.assign({}, prev), { page: newPage })));
|
|
12
44
|
};
|
|
13
45
|
return Object.assign(Object.assign({}, state), { onPerPageSelect,
|
|
@@ -42,8 +42,60 @@ describe('useDataViewPagination', () => {
|
|
|
42
42
|
expect(result.current).toEqual({
|
|
43
43
|
onPerPageSelect: expect.any(Function),
|
|
44
44
|
onSetPage: expect.any(Function),
|
|
45
|
-
page:
|
|
45
|
+
page: 1,
|
|
46
46
|
perPage: 50
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
|
+
it('should read pagination state from URL', () => {
|
|
50
|
+
const mockSearchParams = new URLSearchParams('page=2&perPage=10');
|
|
51
|
+
const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
|
|
52
|
+
searchParams: mockSearchParams,
|
|
53
|
+
setSearchParams: jest.fn(),
|
|
54
|
+
page: 1,
|
|
55
|
+
perPage: 5,
|
|
56
|
+
}));
|
|
57
|
+
expect(result.current).toEqual({
|
|
58
|
+
onPerPageSelect: expect.any(Function),
|
|
59
|
+
onSetPage: expect.any(Function),
|
|
60
|
+
page: 2,
|
|
61
|
+
perPage: 10,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
it('should set pagination state in URL when page changes', () => {
|
|
65
|
+
const mockSetSearchParams = jest.fn();
|
|
66
|
+
const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
|
|
67
|
+
searchParams: new URLSearchParams(),
|
|
68
|
+
setSearchParams: mockSetSearchParams,
|
|
69
|
+
page: 1,
|
|
70
|
+
perPage: 5,
|
|
71
|
+
}));
|
|
72
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(1, new URLSearchParams('page=1&perPage=5'));
|
|
73
|
+
(0, react_1.act)(() => {
|
|
74
|
+
result.current.onSetPage(undefined, 4);
|
|
75
|
+
});
|
|
76
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(2, new URLSearchParams('page=4&perPage=5'));
|
|
77
|
+
});
|
|
78
|
+
it('should set pagination state in URL when perPage changes', () => {
|
|
79
|
+
const mockSetSearchParams = jest.fn();
|
|
80
|
+
const { result } = (0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
|
|
81
|
+
searchParams: new URLSearchParams('page=2'),
|
|
82
|
+
setSearchParams: mockSetSearchParams,
|
|
83
|
+
page: 1,
|
|
84
|
+
perPage: 5,
|
|
85
|
+
}));
|
|
86
|
+
(0, react_1.act)(() => {
|
|
87
|
+
result.current.onPerPageSelect(undefined, 20);
|
|
88
|
+
});
|
|
89
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=20'));
|
|
90
|
+
});
|
|
91
|
+
it('should initialize URL with default values if not present', () => {
|
|
92
|
+
const mockSetSearchParams = jest.fn();
|
|
93
|
+
(0, react_1.renderHook)(() => (0, pagination_1.useDataViewPagination)({
|
|
94
|
+
searchParams: new URLSearchParams(),
|
|
95
|
+
setSearchParams: mockSetSearchParams,
|
|
96
|
+
page: 1,
|
|
97
|
+
perPage: 5,
|
|
98
|
+
}));
|
|
99
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=5'));
|
|
100
|
+
});
|
|
49
101
|
});
|
|
@@ -1,14 +1,26 @@
|
|
|
1
|
+
export declare enum PaginationParams {
|
|
2
|
+
PAGE = "page",
|
|
3
|
+
PER_PAGE = "perPage"
|
|
4
|
+
}
|
|
1
5
|
export interface UseDataViewPaginationProps {
|
|
2
6
|
/** Initial page */
|
|
3
7
|
page?: number;
|
|
4
8
|
/** Items per page */
|
|
5
9
|
perPage: number;
|
|
10
|
+
/** Current search parameters as a string */
|
|
11
|
+
searchParams?: URLSearchParams;
|
|
12
|
+
/** Function to set search parameters */
|
|
13
|
+
setSearchParams?: (params: URLSearchParams) => void;
|
|
14
|
+
/** Custom URL parameter name for page */
|
|
15
|
+
pageParam?: string;
|
|
16
|
+
/** Custom URL parameter name for per page */
|
|
17
|
+
perPageParam?: string;
|
|
6
18
|
}
|
|
7
19
|
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
|
|
8
20
|
/** Current page number */
|
|
9
21
|
page: number;
|
|
10
22
|
}
|
|
11
|
-
export declare const useDataViewPagination: ({ page, perPage }: UseDataViewPaginationProps) => {
|
|
23
|
+
export declare const useDataViewPagination: ({ page, perPage, searchParams, setSearchParams, pageParam, perPageParam, }: UseDataViewPaginationProps) => {
|
|
12
24
|
onPerPageSelect: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPerPage: number) => void;
|
|
13
25
|
onSetPage: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPage: number) => void;
|
|
14
26
|
page: number;
|
|
@@ -1,10 +1,42 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
|
-
export
|
|
3
|
-
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
export var PaginationParams;
|
|
3
|
+
(function (PaginationParams) {
|
|
4
|
+
PaginationParams["PAGE"] = "page";
|
|
5
|
+
PaginationParams["PER_PAGE"] = "perPage";
|
|
6
|
+
})(PaginationParams || (PaginationParams = {}));
|
|
7
|
+
export const useDataViewPagination = ({ page = 1, perPage, searchParams, setSearchParams, pageParam = PaginationParams.PAGE, perPageParam = PaginationParams.PER_PAGE, }) => {
|
|
8
|
+
const [state, setState] = useState({
|
|
9
|
+
page: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${page}`),
|
|
10
|
+
perPage: parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${perPage}`),
|
|
11
|
+
});
|
|
12
|
+
const updateSearchParams = (page, perPage) => {
|
|
13
|
+
if (searchParams && setSearchParams) {
|
|
14
|
+
const params = new URLSearchParams(searchParams);
|
|
15
|
+
params.set(pageParam, `${page}`);
|
|
16
|
+
params.set(perPageParam, `${perPage}`);
|
|
17
|
+
setSearchParams(params);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
// Make sure search params are loaded or set if not present on mount
|
|
22
|
+
updateSearchParams(state.page, state.perPage);
|
|
23
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
24
|
+
}, []);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
// Listen on URL params changes
|
|
27
|
+
const currentPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(pageParam)) || `${state.page}`);
|
|
28
|
+
const currentPerPage = parseInt((searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(perPageParam)) || `${state.perPage}`);
|
|
29
|
+
if (currentPage !== state.page || currentPerPage !== state.perPage) {
|
|
30
|
+
setState({ page: currentPage, perPage: currentPerPage });
|
|
31
|
+
}
|
|
32
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
33
|
+
}, [searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
|
|
4
34
|
const onPerPageSelect = (_event, newPerPage) => {
|
|
5
|
-
|
|
35
|
+
updateSearchParams(1, newPerPage);
|
|
36
|
+
setState({ perPage: newPerPage, page: 1 });
|
|
6
37
|
};
|
|
7
38
|
const onSetPage = (_event, newPage) => {
|
|
39
|
+
updateSearchParams(newPage, state.perPage);
|
|
8
40
|
setState(prev => (Object.assign(Object.assign({}, prev), { page: newPage })));
|
|
9
41
|
};
|
|
10
42
|
return Object.assign(Object.assign({}, state), { onPerPageSelect,
|
|
@@ -40,8 +40,60 @@ describe('useDataViewPagination', () => {
|
|
|
40
40
|
expect(result.current).toEqual({
|
|
41
41
|
onPerPageSelect: expect.any(Function),
|
|
42
42
|
onSetPage: expect.any(Function),
|
|
43
|
-
page:
|
|
43
|
+
page: 1,
|
|
44
44
|
perPage: 50
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
|
+
it('should read pagination state from URL', () => {
|
|
48
|
+
const mockSearchParams = new URLSearchParams('page=2&perPage=10');
|
|
49
|
+
const { result } = renderHook(() => useDataViewPagination({
|
|
50
|
+
searchParams: mockSearchParams,
|
|
51
|
+
setSearchParams: jest.fn(),
|
|
52
|
+
page: 1,
|
|
53
|
+
perPage: 5,
|
|
54
|
+
}));
|
|
55
|
+
expect(result.current).toEqual({
|
|
56
|
+
onPerPageSelect: expect.any(Function),
|
|
57
|
+
onSetPage: expect.any(Function),
|
|
58
|
+
page: 2,
|
|
59
|
+
perPage: 10,
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it('should set pagination state in URL when page changes', () => {
|
|
63
|
+
const mockSetSearchParams = jest.fn();
|
|
64
|
+
const { result } = renderHook(() => useDataViewPagination({
|
|
65
|
+
searchParams: new URLSearchParams(),
|
|
66
|
+
setSearchParams: mockSetSearchParams,
|
|
67
|
+
page: 1,
|
|
68
|
+
perPage: 5,
|
|
69
|
+
}));
|
|
70
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(1, new URLSearchParams('page=1&perPage=5'));
|
|
71
|
+
act(() => {
|
|
72
|
+
result.current.onSetPage(undefined, 4);
|
|
73
|
+
});
|
|
74
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(2, new URLSearchParams('page=4&perPage=5'));
|
|
75
|
+
});
|
|
76
|
+
it('should set pagination state in URL when perPage changes', () => {
|
|
77
|
+
const mockSetSearchParams = jest.fn();
|
|
78
|
+
const { result } = renderHook(() => useDataViewPagination({
|
|
79
|
+
searchParams: new URLSearchParams('page=2'),
|
|
80
|
+
setSearchParams: mockSetSearchParams,
|
|
81
|
+
page: 1,
|
|
82
|
+
perPage: 5,
|
|
83
|
+
}));
|
|
84
|
+
act(() => {
|
|
85
|
+
result.current.onPerPageSelect(undefined, 20);
|
|
86
|
+
});
|
|
87
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=20'));
|
|
88
|
+
});
|
|
89
|
+
it('should initialize URL with default values if not present', () => {
|
|
90
|
+
const mockSetSearchParams = jest.fn();
|
|
91
|
+
renderHook(() => useDataViewPagination({
|
|
92
|
+
searchParams: new URLSearchParams(),
|
|
93
|
+
setSearchParams: mockSetSearchParams,
|
|
94
|
+
page: 1,
|
|
95
|
+
perPage: 5,
|
|
96
|
+
}));
|
|
97
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(new URLSearchParams('page=1&perPage=5'));
|
|
98
|
+
});
|
|
47
99
|
});
|
package/package.json
CHANGED
package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md
CHANGED
|
@@ -18,6 +18,7 @@ import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-d
|
|
|
18
18
|
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
|
|
19
19
|
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
|
|
20
20
|
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
|
|
21
|
+
import { BrowserRouter, useSearchParams } from 'react-router-dom';
|
|
21
22
|
|
|
22
23
|
This is a list of functionality you can use to manage data displayed in the **data view**.
|
|
23
24
|
|
|
@@ -25,7 +26,7 @@ This is a list of functionality you can use to manage data displayed in the **da
|
|
|
25
26
|
Allows to display data records on multiple pages and display the pagination state.
|
|
26
27
|
|
|
27
28
|
### Toolbar usage
|
|
28
|
-
Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes.
|
|
29
|
+
Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes. Additionally, it offers an option to persist pagination values in the URL, which makes it easier to share or bookmark specific pages of your data.
|
|
29
30
|
|
|
30
31
|
### Pagination state
|
|
31
32
|
|
|
@@ -34,8 +35,14 @@ The `useDataViewPagination` hook manages the pagination state of the data view.
|
|
|
34
35
|
**Initial values:**
|
|
35
36
|
- `perPage` initial value
|
|
36
37
|
- (optional) `page` initial value
|
|
38
|
+
- (optional) `searchParams` object
|
|
39
|
+
- (optional) `seSearchParams` function
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
While the hook works seamlessly with React Router library, you do not need to use it to take advantage of URL persistence. The `searchParams` and `setSearchParams` props can be managed using native browser APIs (`URLSearchParams` and `window.history.pushState`) or any other routing library of your choice. If you don't pass these two props, the pagination state will be stored internally without the URL usage.
|
|
42
|
+
|
|
43
|
+
You can also pass custom `pageParam` or `perPageParam` names, renaming the pagination parameters in the URL.
|
|
44
|
+
|
|
45
|
+
The retrieved values are named to match the PatternFly [pagination](/components/pagination) component props, so you can easily spread them to the component.
|
|
39
46
|
|
|
40
47
|
**Return values:**
|
|
41
48
|
- current `page` number
|
|
@@ -44,6 +51,8 @@ The retrieved values are named to match the PatternFly [pagination](/components/
|
|
|
44
51
|
- `onPerPageSelect` to modify per page value
|
|
45
52
|
|
|
46
53
|
### Pagination example
|
|
54
|
+
This example uses the URL for persisting the pagination state.
|
|
55
|
+
|
|
47
56
|
```js file="./PaginationExample.tsx"
|
|
48
57
|
|
|
49
58
|
```
|
package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
|
|
|
4
4
|
import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
|
|
5
5
|
import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
|
|
6
6
|
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
|
|
7
|
+
import { BrowserRouter, useSearchParams } from 'react-router-dom';
|
|
7
8
|
|
|
8
9
|
const perPageOptions = [
|
|
9
10
|
{ title: '5', value: 5 },
|
|
@@ -37,12 +38,12 @@ const cols = {
|
|
|
37
38
|
|
|
38
39
|
const ouiaId = 'LayoutExample';
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
const
|
|
41
|
+
const MyTable: React.FunctionComponent = () => {
|
|
42
|
+
const [ searchParams, setSearchParams ] = useSearchParams()
|
|
43
|
+
const pagination = useDataViewPagination({ perPage: 5, searchParams, setSearchParams });
|
|
42
44
|
const { page, perPage } = pagination;
|
|
43
45
|
|
|
44
46
|
const data = useMemo(() => repositories.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
|
|
45
|
-
|
|
46
47
|
return (
|
|
47
48
|
<DataView>
|
|
48
49
|
<DataViewToolbar ouiaId='DataViewHeader' pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
|
|
@@ -62,4 +63,11 @@ export const BasicExample: React.FunctionComponent = () => {
|
|
|
62
63
|
</Table>
|
|
63
64
|
<DataViewToolbar ouiaId='DataViewFooter' pagination={<Pagination isCompact perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
|
|
64
65
|
</DataView>
|
|
65
|
-
)
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const BasicExample: React.FunctionComponent = () => (
|
|
70
|
+
<BrowserRouter>
|
|
71
|
+
<MyTable/>
|
|
72
|
+
</BrowserRouter>
|
|
73
|
+
)
|
package/release.config.js
CHANGED
|
@@ -49,9 +49,89 @@ describe('useDataViewPagination', () => {
|
|
|
49
49
|
expect(result.current).toEqual({
|
|
50
50
|
onPerPageSelect: expect.any(Function),
|
|
51
51
|
onSetPage: expect.any(Function),
|
|
52
|
-
page:
|
|
52
|
+
page: 1,
|
|
53
53
|
perPage: 50
|
|
54
54
|
})
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
+
it('should read pagination state from URL', () => {
|
|
58
|
+
const mockSearchParams = new URLSearchParams('page=2&perPage=10');
|
|
59
|
+
const { result } = renderHook(() =>
|
|
60
|
+
useDataViewPagination({
|
|
61
|
+
searchParams: mockSearchParams,
|
|
62
|
+
setSearchParams: jest.fn(),
|
|
63
|
+
page: 1,
|
|
64
|
+
perPage: 5,
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(result.current).toEqual({
|
|
69
|
+
onPerPageSelect: expect.any(Function),
|
|
70
|
+
onSetPage: expect.any(Function),
|
|
71
|
+
page: 2,
|
|
72
|
+
perPage: 10,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should set pagination state in URL when page changes', () => {
|
|
77
|
+
const mockSetSearchParams = jest.fn();
|
|
78
|
+
const { result } = renderHook(() =>
|
|
79
|
+
useDataViewPagination({
|
|
80
|
+
searchParams: new URLSearchParams(),
|
|
81
|
+
setSearchParams: mockSetSearchParams,
|
|
82
|
+
page: 1,
|
|
83
|
+
perPage: 5,
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(
|
|
88
|
+
1,
|
|
89
|
+
new URLSearchParams('page=1&perPage=5')
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
act(() => {
|
|
93
|
+
result.current.onSetPage(undefined, 4);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(mockSetSearchParams).toHaveBeenNthCalledWith(
|
|
97
|
+
2,
|
|
98
|
+
new URLSearchParams('page=4&perPage=5')
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should set pagination state in URL when perPage changes', () => {
|
|
103
|
+
const mockSetSearchParams = jest.fn();
|
|
104
|
+
const { result } = renderHook(() =>
|
|
105
|
+
useDataViewPagination({
|
|
106
|
+
searchParams: new URLSearchParams('page=2'),
|
|
107
|
+
setSearchParams: mockSetSearchParams,
|
|
108
|
+
page: 1,
|
|
109
|
+
perPage: 5,
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
act(() => {
|
|
114
|
+
result.current.onPerPageSelect(undefined, 20);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(
|
|
118
|
+
new URLSearchParams('page=1&perPage=20')
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should initialize URL with default values if not present', () => {
|
|
123
|
+
const mockSetSearchParams = jest.fn();
|
|
124
|
+
renderHook(() =>
|
|
125
|
+
useDataViewPagination({
|
|
126
|
+
searchParams: new URLSearchParams(),
|
|
127
|
+
setSearchParams: mockSetSearchParams,
|
|
128
|
+
page: 1,
|
|
129
|
+
perPage: 5,
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(mockSetSearchParams).toHaveBeenCalledWith(
|
|
134
|
+
new URLSearchParams('page=1&perPage=5')
|
|
135
|
+
);
|
|
136
|
+
});
|
|
57
137
|
});
|
package/src/Hooks/pagination.ts
CHANGED
|
@@ -1,31 +1,87 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
export enum PaginationParams {
|
|
4
|
+
PAGE = 'page',
|
|
5
|
+
PER_PAGE = 'perPage'
|
|
6
|
+
}
|
|
2
7
|
|
|
3
8
|
export interface UseDataViewPaginationProps {
|
|
4
9
|
/** Initial page */
|
|
5
10
|
page?: number;
|
|
6
11
|
/** Items per page */
|
|
7
12
|
perPage: number;
|
|
13
|
+
/** Current search parameters as a string */
|
|
14
|
+
searchParams?: URLSearchParams;
|
|
15
|
+
/** Function to set search parameters */
|
|
16
|
+
setSearchParams?: (params: URLSearchParams) => void;
|
|
17
|
+
/** Custom URL parameter name for page */
|
|
18
|
+
pageParam?: string;
|
|
19
|
+
/** Custom URL parameter name for per page */
|
|
20
|
+
perPageParam?: string;
|
|
8
21
|
}
|
|
9
22
|
|
|
10
23
|
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
|
|
11
24
|
/** Current page number */
|
|
12
|
-
|
|
25
|
+
page: number;
|
|
13
26
|
}
|
|
14
27
|
|
|
15
|
-
export const useDataViewPagination = ({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
export const useDataViewPagination = ({
|
|
29
|
+
page = 1,
|
|
30
|
+
perPage,
|
|
31
|
+
searchParams,
|
|
32
|
+
setSearchParams,
|
|
33
|
+
pageParam = PaginationParams.PAGE,
|
|
34
|
+
perPageParam = PaginationParams.PER_PAGE,
|
|
35
|
+
}: UseDataViewPaginationProps) => {
|
|
36
|
+
const [ state, setState ] = useState({
|
|
37
|
+
page: parseInt(searchParams?.get(pageParam) || `${page}`),
|
|
38
|
+
perPage: parseInt(searchParams?.get(perPageParam) || `${perPage}`),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const updateSearchParams = (page: number, perPage: number) => {
|
|
42
|
+
if (searchParams && setSearchParams) {
|
|
43
|
+
const params = new URLSearchParams(searchParams);
|
|
44
|
+
params.set(pageParam, `${page}`);
|
|
45
|
+
params.set(perPageParam, `${perPage}`);
|
|
46
|
+
setSearchParams(params);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
// Make sure search params are loaded or set if not present on mount
|
|
52
|
+
updateSearchParams(state.page, state.perPage);
|
|
53
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
// Listen on URL params changes
|
|
58
|
+
const currentPage = parseInt(searchParams?.get(pageParam) || `${state.page}`);
|
|
59
|
+
const currentPerPage = parseInt(searchParams?.get(perPageParam) || `${state.perPage}`);
|
|
60
|
+
if (currentPage !== state.page || currentPerPage !== state.perPage) {
|
|
61
|
+
setState({ page: currentPage, perPage: currentPerPage });
|
|
62
|
+
}
|
|
63
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
64
|
+
}, [ searchParams?.toString() ]);
|
|
65
|
+
|
|
66
|
+
const onPerPageSelect = (
|
|
67
|
+
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined,
|
|
68
|
+
newPerPage: number
|
|
69
|
+
) => {
|
|
70
|
+
updateSearchParams(1, newPerPage);
|
|
71
|
+
setState({ perPage: newPerPage, page: 1 });
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const onSetPage = (
|
|
75
|
+
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined,
|
|
76
|
+
newPage: number
|
|
77
|
+
) => {
|
|
78
|
+
updateSearchParams(newPage, state.perPage);
|
|
23
79
|
setState(prev => ({ ...prev, page: newPage }));
|
|
24
|
-
}
|
|
25
|
-
|
|
80
|
+
};
|
|
81
|
+
|
|
26
82
|
return {
|
|
27
83
|
...state,
|
|
28
84
|
onPerPageSelect,
|
|
29
|
-
onSetPage
|
|
30
|
-
}
|
|
31
|
-
}
|
|
85
|
+
onSetPage,
|
|
86
|
+
};
|
|
87
|
+
};
|