@patternfly/react-data-view 6.1.0-prerelease.1 → 7.0.0-prerelease.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/cjs/DataView/DataView.test.js +4 -3
- package/dist/cjs/Hooks/pagination.d.ts +9 -1
- package/dist/cjs/Hooks/pagination.js +36 -4
- package/dist/cjs/Hooks/pagination.test.js +53 -1
- package/dist/esm/DataView/DataView.test.js +4 -3
- package/dist/esm/Hooks/pagination.d.ts +9 -1
- package/dist/esm/Hooks/pagination.js +36 -4
- package/dist/esm/Hooks/pagination.test.js +53 -1
- package/package.json +8 -9
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +9 -2
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/PaginationExample.tsx +12 -4
- package/patternfly-docs/content/extensions/data-view/examples/Layout/AbstractLayoutExample.tsx +4 -3
- package/patternfly-docs/pages/index.js +1 -1
- package/release.config.js +5 -1
- package/src/DataView/DataView.test.tsx +4 -3
- package/src/DataView/__snapshots__/DataView.test.tsx.snap +14 -14
- package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +166 -208
- package/src/Hooks/pagination.test.tsx +81 -1
- package/src/Hooks/pagination.ts +65 -15
|
@@ -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,81 @@
|
|
|
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;
|
|
8
17
|
}
|
|
9
18
|
|
|
10
19
|
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
|
|
11
20
|
/** Current page number */
|
|
12
|
-
|
|
21
|
+
page: number;
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
export const useDataViewPagination = ({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
export const useDataViewPagination = ({
|
|
25
|
+
page = 1,
|
|
26
|
+
perPage,
|
|
27
|
+
searchParams,
|
|
28
|
+
setSearchParams,
|
|
29
|
+
}: UseDataViewPaginationProps) => {
|
|
30
|
+
const [ state, setState ] = useState({
|
|
31
|
+
page: parseInt(searchParams?.get(PaginationParams.PAGE) || `${page}`),
|
|
32
|
+
perPage: parseInt(searchParams?.get(PaginationParams.PER_PAGE) || `${perPage}`),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const updateSearchParams = (page: number, perPage: number) => {
|
|
36
|
+
if (searchParams && setSearchParams) {
|
|
37
|
+
const params = new URLSearchParams(searchParams);
|
|
38
|
+
params.set(PaginationParams.PAGE, `${page}`);
|
|
39
|
+
params.set(PaginationParams.PER_PAGE, `${perPage}`);
|
|
40
|
+
setSearchParams(params);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
// Make sure search params are loaded or set if not present on mount
|
|
46
|
+
updateSearchParams(state.page, state.perPage);
|
|
47
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
48
|
+
}, []);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
// Listen on URL params changes
|
|
52
|
+
const currentPage = parseInt(searchParams?.get(PaginationParams.PAGE) || `${state.page}`);
|
|
53
|
+
const currentPerPage = parseInt(searchParams?.get(PaginationParams.PER_PAGE) || `${state.perPage}`);
|
|
54
|
+
if (currentPage !== state.page || currentPerPage !== state.perPage) {
|
|
55
|
+
setState({ page: currentPage, perPage: currentPerPage });
|
|
56
|
+
}
|
|
57
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
58
|
+
}, [ searchParams ]);
|
|
59
|
+
|
|
60
|
+
const onPerPageSelect = (
|
|
61
|
+
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined,
|
|
62
|
+
newPerPage: number
|
|
63
|
+
) => {
|
|
64
|
+
updateSearchParams(1, newPerPage);
|
|
65
|
+
setState({ perPage: newPerPage, page: 1 });
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const onSetPage = (
|
|
69
|
+
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined,
|
|
70
|
+
newPage: number
|
|
71
|
+
) => {
|
|
72
|
+
updateSearchParams(newPage, state.perPage);
|
|
23
73
|
setState(prev => ({ ...prev, page: newPage }));
|
|
24
|
-
}
|
|
25
|
-
|
|
74
|
+
};
|
|
75
|
+
|
|
26
76
|
return {
|
|
27
77
|
...state,
|
|
28
78
|
onPerPageSelect,
|
|
29
|
-
onSetPage
|
|
30
|
-
}
|
|
31
|
-
}
|
|
79
|
+
onSetPage,
|
|
80
|
+
};
|
|
81
|
+
};
|