Skip to content

Commit

Permalink
feat: usePagination adds default parameter defaultPage (alibaba#1777)
Browse files Browse the repository at this point in the history
* feat: usePagination add deafultPage

* test: add test case

* fix: defaultPage -> defaultCurrent

* fix: remove pnpm-lock change

* fix: text case
  • Loading branch information
lmsccc authored Aug 1, 2022
1 parent d3f39de commit 975dfb4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/hooks/src/usePagination/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,35 @@ describe('usePagination', () => {
expect(hook.result.current.pagination.current).toEqual(1);
expect(hook.result.current.pagination.pageSize).toEqual(20);
});

it('should default params work', async () => {
queryArgs = undefined;
act(() => {
hook = setUp(asyncFn, {
defaultPageSize: 5,
defaultCurrent: 2,
});
});
expect(hook.result.current.loading).toEqual(true);
expect(queryArgs.current).toEqual(2);
expect(queryArgs.pageSize).toEqual(5);
await hook.waitForNextUpdate();

expect(hook.result.current.loading).toEqual(false);

expect(hook.result.current.pagination.current).toEqual(2);
expect(hook.result.current.pagination.pageSize).toEqual(5);
expect(hook.result.current.pagination.total).toEqual(55);
expect(hook.result.current.pagination.totalPage).toEqual(11);

act(() => {
hook.result.current.pagination.changeCurrent(3);
});
expect(hook.result.current.loading).toEqual(true);
expect(queryArgs.current).toEqual(3);
expect(queryArgs.pageSize).toEqual(5);
await hook.waitForNextUpdate();
expect(hook.result.current.pagination.current).toEqual(3);
expect(hook.result.current.pagination.pageSize).toEqual(5);
});
});
1 change: 1 addition & 0 deletions packages/hooks/src/usePagination/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ const {
| Property | Description | Type | Default |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------- | ------- |
| defaultPageSize | Default page size | `number` | - |
| defaultCurrent | Number of pages on initial request | `number` | - |
| refreshDeps | Changes in `refreshDeps` will reset current to the first page and re-initiate the request. Generally, you can put the dependent conditions here. | `React.DependencyList` | `[]` |
4 changes: 2 additions & 2 deletions packages/hooks/src/usePagination/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const usePagination = <TData extends Data, TParams extends Params>(
service: Service<TData, TParams>,
options: PaginationOptions<TData, TParams> = {},
) => {
const { defaultPageSize = 10, ...rest } = options;
const { defaultPageSize = 10, defaultCurrent = 1, ...rest } = options;

const result = useRequest(service, {
defaultParams: [{ current: 1, pageSize: defaultPageSize }],
defaultParams: [{ current: defaultCurrent, pageSize: defaultPageSize }],
refreshDepsAction: () => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
changeCurrent(1);
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/src/usePagination/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ const {
| 参数 | 说明 | 类型 | 默认值 |
| --------------- | ------------------------------------------------------------------------------------------- | ---------------------- | ------ |
| defaultPageSize | 默认分页数量 | `number` | - |
| defaultCurrent | 初次请求时的页数 | `number` | - |
| refreshDeps | `refreshDeps` 变化,会重置 current 到第一页,并重新发起请求,一般你可以把依赖的条件放这里。 | `React.DependencyList` | `[]` |
1 change: 1 addition & 0 deletions packages/hooks/src/usePagination/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export interface PaginationResult<TData extends Data, TParams extends Params>
export interface PaginationOptions<TData extends Data, TParams extends Params>
extends Options<TData, TParams> {
defaultPageSize?: number;
defaultCurrent?: number;
}

0 comments on commit 975dfb4

Please sign in to comment.