@availity/mui-pagination 0.2.11 → 0.3.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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [0.3.0](https://github.com/Availity/element/compare/@availity/mui-pagination@0.2.12...@availity/mui-pagination@0.3.0) (2024-09-24)
|
|
6
|
+
|
|
7
|
+
### Dependency Updates
|
|
8
|
+
|
|
9
|
+
* `mui-list` updated to version `0.2.12`
|
|
10
|
+
* `@availity/mock` updated to version `0.2.12`
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* **mui-pagination:** create useResourcePagination hook ([be908a2](https://github.com/Availity/element/commit/be908a26c621ffbfaeab9f543803b28c299a19da))
|
|
15
|
+
|
|
16
|
+
## [0.2.12](https://github.com/Availity/element/compare/@availity/mui-pagination@0.2.11...@availity/mui-pagination@0.2.12) (2024-08-08)
|
|
17
|
+
|
|
18
|
+
### Dependency Updates
|
|
19
|
+
|
|
20
|
+
* `mui-icon` updated to version `0.2.11`
|
|
5
21
|
## [0.2.11](https://github.com/Availity/element/compare/@availity/mui-pagination@0.2.10...@availity/mui-pagination@0.2.11) (2024-08-05)
|
|
6
22
|
|
|
7
23
|
### Dependency Updates
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@availity/mui-pagination",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Availity MUI Pagination Component - part of the @availity/element design system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"publish:canary": "yarn npm publish --access public --tag canary"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@availity/mui-icon": "0.10.
|
|
35
|
+
"@availity/mui-icon": "0.10.1",
|
|
36
36
|
"@mui/material": "^5.15.15",
|
|
37
37
|
"react": "18.2.0",
|
|
38
38
|
"react-dom": "18.2.0",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"typescript": "^5.4.5"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@availity/mui-icon": "0.10.
|
|
43
|
+
"@availity/mui-icon": "0.10.1",
|
|
44
44
|
"@mui/material": "^5.11.9",
|
|
45
45
|
"react": ">=16.3.0"
|
|
46
46
|
},
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Each exported component in the package should have its own stories file
|
|
2
|
+
|
|
3
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
4
|
+
import { Box } from '@availity/mui-layout';
|
|
5
|
+
import { avOrganizationsApi } from '@availity/api-axios';
|
|
6
|
+
import { List, ListItem, ListItemText } from '@availity/mui-list';
|
|
7
|
+
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
|
|
8
|
+
import { Pagination } from './Pagination';
|
|
9
|
+
import { useResourcePagination } from './ResourcePagination';
|
|
10
|
+
import { useState } from 'react';
|
|
11
|
+
|
|
12
|
+
const meta: Meta<typeof Pagination> = {
|
|
13
|
+
title: 'Components/Pagination/useResourcePagination',
|
|
14
|
+
component: Pagination,
|
|
15
|
+
tags: ['autodocs'],
|
|
16
|
+
decorators: [
|
|
17
|
+
(Story) => (
|
|
18
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
19
|
+
<Story />
|
|
20
|
+
</QueryClientProvider>
|
|
21
|
+
),
|
|
22
|
+
],
|
|
23
|
+
args: {
|
|
24
|
+
count: 10,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default meta;
|
|
29
|
+
|
|
30
|
+
export const _useResourcePagination: StoryObj<typeof Pagination> = {
|
|
31
|
+
render: () => {
|
|
32
|
+
const [page, setPage] = useState(1);
|
|
33
|
+
const itemsPerPage = 10;
|
|
34
|
+
const resp = useResourcePagination({
|
|
35
|
+
page,
|
|
36
|
+
resource: avOrganizationsApi,
|
|
37
|
+
getResult: (result) => result.organizations,
|
|
38
|
+
itemsPerPage,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (resp.isLoading) {
|
|
42
|
+
return <div>Loading...</div>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return resp.data ? (
|
|
46
|
+
<Box>
|
|
47
|
+
<List>
|
|
48
|
+
{resp.data?.items.map((item) => (
|
|
49
|
+
<ListItem>
|
|
50
|
+
<ListItemText primary={item.name} secondary={item.customerId} />
|
|
51
|
+
</ListItem>
|
|
52
|
+
))}
|
|
53
|
+
</List>
|
|
54
|
+
<Pagination
|
|
55
|
+
page={page}
|
|
56
|
+
count={Math.ceil(Math.abs(resp.data.totalCount / itemsPerPage))}
|
|
57
|
+
onChange={(event, newPage) => setPage(newPage)}
|
|
58
|
+
/>
|
|
59
|
+
</Box>
|
|
60
|
+
) : (
|
|
61
|
+
<div>Error</div>
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { render, waitForElementToBeRemoved } from '@testing-library/react';
|
|
2
|
+
import { useResourcePagination } from './ResourcePagination';
|
|
3
|
+
import { avOrganizationsApi } from '@availity/api-axios';
|
|
4
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
5
|
+
// eslint-disable-next-line @nx/enforce-module-boundaries
|
|
6
|
+
import { server } from '@availity/mock/src/lib/server';
|
|
7
|
+
// eslint-disable-next-line @nx/enforce-module-boundaries
|
|
8
|
+
import organizations from '@availity/mock/src/lib/data/organizations.json';
|
|
9
|
+
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
// Start the interception.
|
|
12
|
+
server.listen();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
// Remove any handlers you may have added
|
|
17
|
+
// in individual tests (runtime handlers).
|
|
18
|
+
server.resetHandlers();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const client = new QueryClient();
|
|
22
|
+
|
|
23
|
+
const ResourcePaginationComponent = (): JSX.Element => {
|
|
24
|
+
const resp = useResourcePagination({
|
|
25
|
+
page: 1,
|
|
26
|
+
resource: avOrganizationsApi,
|
|
27
|
+
getResult: (result) => result.organizations,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (resp.isLoading) {
|
|
31
|
+
return <div>Loading...</div>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
<div data-testid="response">{JSON.stringify(resp?.data?.items)}</div>
|
|
37
|
+
<div data-testid="totalCount">{JSON.stringify(resp?.data?.totalCount)}</div>
|
|
38
|
+
</>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe('ResourcePagination', () => {
|
|
43
|
+
test('should render successfully', async () => {
|
|
44
|
+
const { getByText, getByTestId } = render(
|
|
45
|
+
<QueryClientProvider client={client}>
|
|
46
|
+
<ResourcePaginationComponent />
|
|
47
|
+
</QueryClientProvider>
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
await waitForElementToBeRemoved(() => getByText('Loading...'));
|
|
51
|
+
|
|
52
|
+
const data = getByTestId('response').innerHTML;
|
|
53
|
+
|
|
54
|
+
expect(JSON.parse(data)).toStrictEqual(organizations.organizations.slice(0, 10));
|
|
55
|
+
|
|
56
|
+
const totalCount = getByTestId('totalCount').innerHTML;
|
|
57
|
+
|
|
58
|
+
expect(totalCount).toStrictEqual(JSON.stringify(organizations.totalCount));
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useQuery } from '@tanstack/react-query';
|
|
2
|
+
|
|
3
|
+
type Resource<TData> = {
|
|
4
|
+
postGet: (
|
|
5
|
+
request: { limit: number; offset: number } & Record<string, unknown>,
|
|
6
|
+
config: unknown
|
|
7
|
+
) => Promise<{ data: TData & { totalCount: number } }>;
|
|
8
|
+
getResult: string | ((result: TData) => unknown[]);
|
|
9
|
+
addParams: (data: Record<string, unknown>, config: Record<string, unknown>) => unknown;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type LoadPageProps<TData> = {
|
|
13
|
+
page: number;
|
|
14
|
+
/** When a function, the function is called with the response body from the API call and is expected to return an array containing the list of items for the page. When a string, the string is expected to be a simple key used to get the value from the response. ("simple" means dot notation is not supported for grabbing values from nested objects. If your result is deeply nested, provide a function) */
|
|
15
|
+
getResult: string | ((result: TData) => unknown[]);
|
|
16
|
+
/** If array, defaults `totalCount` to the length of the array, and page values are sliced from the Array. If a function, it is called with the current page as an argument and expects an array of items to be returned. */
|
|
17
|
+
itemsPerPage?: number;
|
|
18
|
+
/** Object use to create querystring parameters in the request. */
|
|
19
|
+
parameters?: { params?: Record<string, unknown> } & Record<string, unknown>;
|
|
20
|
+
/** Availity API resource (see [@availity/api-axios](https://github.com/Availity/sdk-js/tree/master/packages/api-axios)). */
|
|
21
|
+
resource: Resource<TData>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const loadPage = async <TData,>({
|
|
25
|
+
page,
|
|
26
|
+
itemsPerPage = 10,
|
|
27
|
+
parameters = {},
|
|
28
|
+
getResult,
|
|
29
|
+
resource,
|
|
30
|
+
}: LoadPageProps<TData>): Promise<{ items: TData[]; totalCount: number }> => {
|
|
31
|
+
const data = {
|
|
32
|
+
limit: itemsPerPage,
|
|
33
|
+
offset: (page - 1) * itemsPerPage,
|
|
34
|
+
...parameters.params,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const config = resource.addParams(data, parameters);
|
|
38
|
+
|
|
39
|
+
const resp = await resource.postGet(data, config);
|
|
40
|
+
|
|
41
|
+
const useGetResult = getResult || resource.getResult;
|
|
42
|
+
|
|
43
|
+
const items =
|
|
44
|
+
(typeof useGetResult === 'function'
|
|
45
|
+
? useGetResult.call(resource, resp.data)
|
|
46
|
+
: (resp.data as Record<string, unknown>)[useGetResult]) || resp.data;
|
|
47
|
+
|
|
48
|
+
if (!Array.isArray(items)) {
|
|
49
|
+
throw new TypeError(
|
|
50
|
+
`Expected data to be an array but got \`${typeof items}\`. Use the \`getResult\` prop to specify how to get the data from the API response.`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
items,
|
|
56
|
+
totalCount: resp.data.totalCount,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const useResourcePagination = <TData,>({
|
|
61
|
+
page,
|
|
62
|
+
itemsPerPage,
|
|
63
|
+
parameters,
|
|
64
|
+
getResult,
|
|
65
|
+
resource,
|
|
66
|
+
}: LoadPageProps<TData>) =>
|
|
67
|
+
useQuery(['resource', page], () => loadPage({ page, itemsPerPage, parameters, getResult, resource }));
|