From c3822f0fe0d579dc1b534c6e9c6845dc6ca3f0f1 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 8 May 2021 16:11:52 +0800 Subject: [PATCH] feat(usePagination): add `changePagination` to modify both `current` and `pageSize` #43 --- src/__tests__/pagination.test.tsx | 64 +++++++++++++++++++++++++++++++ src/usePagination.ts | 7 ++++ 2 files changed, 71 insertions(+) diff --git a/src/__tests__/pagination.test.tsx b/src/__tests__/pagination.test.tsx index dd12e09..4f66f62 100644 --- a/src/__tests__/pagination.test.tsx +++ b/src/__tests__/pagination.test.tsx @@ -497,6 +497,70 @@ describe('usePagination', () => { } }); + test('changePagination should work', async () => { + let _current = 1; + let _pageSize = 1; + const wrapper = shallowMount( + defineComponent({ + setup() { + const { + total, + params, + current, + pageSize, + totalPage, + changePagination, + } = usePagination(normalApi, { + manual: true, + }); + return () => ( +
+ +
{total.value}
+
{current.value}
+
{pageSize.value}
+
{totalPage.value}
+
+ ); + }, + }), + ); + + const paramsEl = wrapper.find('.params'); + const totalEl = wrapper.find('.total'); + const currentEl = wrapper.find('.current'); + const pageSizeEl = wrapper.find('.pageSize'); + const totalPageEl = wrapper.find('.totalPage'); + + expect(paramsEl.text()).toBe('[]'); + expect(totalEl.text()).toBe('0'); + expect(currentEl.text()).toBe(`${_current}`); + expect(pageSizeEl.text()).toBe('10'); + expect(totalPageEl.text()).toBe('0'); + + for (let index = 0; index < 100; index++) { + await paramsEl.trigger('click'); + await waitForTime(1000); + + expect(paramsEl.text()).toBe( + `[{"current":${_current},"pageSize":${_pageSize}}]`, + ); + expect(totalEl.text()).toBe('100'); + expect(currentEl.text()).toBe(`${_current}`); + expect(pageSizeEl.text()).toBe(`${_pageSize}`); + expect(totalPageEl.text()).toBe(`${Math.ceil(100 / _pageSize)}`); + } + }); + test('manual should work with defaltParams', async () => { let _current = 1; const wrapper = shallowMount( diff --git a/src/usePagination.ts b/src/usePagination.ts index c8287c1..dd79f25 100644 --- a/src/usePagination.ts +++ b/src/usePagination.ts @@ -22,6 +22,7 @@ export interface PaginationResult reloading: Ref; changeCurrent: (current: number) => void; changePageSize: (pageSize: number) => void; + changePagination: (current: number, pageSize: number) => void; reload: () => void; } @@ -130,6 +131,11 @@ function usePagination( paging({ [pageSizeKey]: pageSize }); }; + // changePagination change current and pageSize (current: number, pageSize: number) => void + const changePagination = (current: number, pageSize: number) => { + paging({ [currentKey]: current, [pageSizeKey]: pageSize }); + }; + const reloading = ref(false); const reload = async () => { const { defaultParams, manual } = finallyOptions; @@ -173,6 +179,7 @@ function usePagination( run, changeCurrent, changePageSize, + changePagination, reload, ...rest, };