Skip to content

Commit 28032be

Browse files
committed
fix(pagination): Enable custom pagination params
1 parent 772beab commit 28032be

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

packages/module/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ The `useDataViewPagination` hook manages the pagination state of the data view.
4040

4141
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.
4242

43+
You can also pass custom `pageParam` or `perPageParam` names, renaming the pagination parameters in the URL.
44+
4345
The retrieved values are named to match the PatternFly [pagination](/components/pagination) component props, so you can easily spread them to the component.
4446

4547
**Return values:**

packages/module/src/Hooks/pagination.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export interface UseDataViewPaginationProps {
1414
searchParams?: URLSearchParams;
1515
/** Function to set search parameters */
1616
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;
1721
}
1822

1923
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
@@ -26,17 +30,19 @@ export const useDataViewPagination = ({
2630
perPage,
2731
searchParams,
2832
setSearchParams,
33+
pageParam = PaginationParams.PAGE,
34+
perPageParam = PaginationParams.PER_PAGE,
2935
}: UseDataViewPaginationProps) => {
3036
const [ state, setState ] = useState({
31-
page: parseInt(searchParams?.get(PaginationParams.PAGE) || `${page}`),
32-
perPage: parseInt(searchParams?.get(PaginationParams.PER_PAGE) || `${perPage}`),
37+
page: parseInt(searchParams?.get(pageParam) || `${page}`),
38+
perPage: parseInt(searchParams?.get(perPageParam) || `${perPage}`),
3339
});
3440

3541
const updateSearchParams = (page: number, perPage: number) => {
3642
if (searchParams && setSearchParams) {
3743
const params = new URLSearchParams(searchParams);
38-
params.set(PaginationParams.PAGE, `${page}`);
39-
params.set(PaginationParams.PER_PAGE, `${perPage}`);
44+
params.set(pageParam, `${page}`);
45+
params.set(perPageParam, `${perPage}`);
4046
setSearchParams(params);
4147
}
4248
};
@@ -49,8 +55,8 @@ export const useDataViewPagination = ({
4955

5056
useEffect(() => {
5157
// 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}`);
58+
const currentPage = parseInt(searchParams?.get(pageParam) || `${state.page}`);
59+
const currentPerPage = parseInt(searchParams?.get(perPageParam) || `${state.perPage}`);
5460
if (currentPage !== state.page || currentPerPage !== state.perPage) {
5561
setState({ page: currentPage, perPage: currentPerPage });
5662
}

0 commit comments

Comments
 (0)