Skip to content

Commit

Permalink
Merge pull request #285 from qoretechnologies/foxhoundn/bug-reqorecon…
Browse files Browse the repository at this point in the history
…tainer-does-284

`onPageChange` paging container callback fixed
  • Loading branch information
Foxhoundn authored Feb 24, 2023
2 parents fca3123 + 57545ed commit 4f1e408
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions __tests__/collection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ test('<Collection /> has infinite paging', () => {
});

test('<Collection /> has custom paging', () => {
const onPageChange = jest.fn();
render(
<ReqoreUIProvider>
<ReqoreLayoutContent>
Expand All @@ -222,6 +223,7 @@ test('<Collection /> has custom paging', () => {
autoLoadMore: true,
showLabels: true,
loadMoreLabel: 'Scroll to load more',
onPageChange,
}}
/>
</ReqoreLayoutContent>
Expand All @@ -236,6 +238,7 @@ test('<Collection /> has custom paging', () => {

mockAllIsIntersecting(true);

expect(onPageChange).toHaveBeenCalledWith(2, { isFirst: false, isLast: true });
expect(document.querySelectorAll('.reqore-collection-item').length).toBe(100);
});

Expand Down
32 changes: 32 additions & 0 deletions __tests__/containers/paging.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,35 @@ test('Renders default <PaginationContainer /> with both control wrappers, one in
expect(document.querySelectorAll('.reqore-button')[101]?.getAttribute('disabled')).toBe('');
expect(document.querySelectorAll('.reqore-button')[203]?.getAttribute('disabled')).toBe('');
});

test('Renders default <PaginationContainer /> with onPageChange callback', () => {
const fn = jest.fn();

render(
<ReqoreUIProvider>
<ReqoreLayoutContent>
<ReqoreContent>
<ReqorePaginationContainer<any>
items={data}
type={{ onPageChange: fn, infinite: true }}
>
{(items) => (
<ReqoreControlGroup vertical>
{items.map((item) => (
<ReqoreTag label={`${item.firstName} ${item.lastName}`} />
))}
</ReqoreControlGroup>
)}
</ReqorePaginationContainer>
</ReqoreContent>
</ReqoreLayoutContent>
</ReqoreUIProvider>
);

expect(document.querySelectorAll('.reqore-pagination-wrapper').length).toBe(1);

fireEvent.click(document.querySelectorAll('.reqore-button')[0]);

expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(2, { isFirst: false, isLast: false });
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qoretechnologies/reqore",
"version": "0.34.5",
"version": "0.34.6",
"description": "ReQore is a highly theme-able and modular UI library for React",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion src/constants/paging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ export function getPaginationOptionsFromType<T>(
itemsPerPage,
pagesToShow,
startPage,
enabled,
onPageChange,
pageControlsPosition = 'bottom',
includeBottomControls,
includeTopControls,

...componentOptions
} = type;

return {
pagingOptions: { infinite, itemsPerPage, pagesToShow, startPage },
pagingOptions: { infinite, itemsPerPage, pagesToShow, startPage, enabled, onPageChange },
pageControlsPosition,
componentOptions,
includeBottomControls,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/usePaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const useReqorePaging = <T>(
useUpdateEffect(() => {
setPage(1);
setTotalPage(Math.ceil(size(items) / itemsPerPage));
}, [size(items), itemsPerPage]);
}, [size(items)]);

const slicedItems: T[] = useMemo(() => {
return items.slice(infinite ? 0 : (currentPage - 1) * itemsPerPage, currentPage * itemsPerPage);
Expand Down

0 comments on commit 4f1e408

Please sign in to comment.