Skip to content

Commit

Permalink
fix: Table should not crash pageSize is undefined (#23724)
Browse files Browse the repository at this point in the history
* fix: Table crash when pageSize is undefined

* test case

* fix test case
  • Loading branch information
zombieJ committed Apr 28, 2020
1 parent 7790046 commit 53a76f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions components/table/__tests__/Table.pagination.test.js
Expand Up @@ -37,6 +37,12 @@ describe('Table.pagination', () => {
expect(wrapper.render()).toMatchSnapshot();
});

it('not crash when pageSize is undefined', () => {
expect(() => {
mount(createTable({ pagination: { pageSIze: undefined } }));
}).not.toThrow();
});

it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', () => {
const wrapper = mount(createTable({ pagination: { pageSize: 3, hideOnSinglePage: true } }));
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
Expand Down
29 changes: 24 additions & 5 deletions components/table/hooks/usePagination.ts
Expand Up @@ -25,6 +25,23 @@ export function getPaginationParam(
return param;
}

function extendsObject<T extends Object>(...list: T[]) {
const result: T = {} as T;

list.forEach(obj => {
if (obj) {
Object.keys(obj).forEach(key => {
const val = (obj as any)[key];
if (val !== undefined) {
(result as any)[key] = val;
}
});
}
});

return result;
}

export default function usePagination(
total: number,
pagination: TablePaginationConfig | false | undefined,
Expand All @@ -42,11 +59,13 @@ export default function usePagination(
});

// ============ Basic Pagination Config ============
const mergedPagination = {
...innerPagination,
...paginationObj,
total: paginationTotal > 0 ? paginationTotal : total,
};
const mergedPagination = extendsObject<Partial<TablePaginationConfig>>(
innerPagination,
paginationObj,
{
total: paginationTotal > 0 ? paginationTotal : total,
},
);

if (!paginationTotal) {
// Reset `current` if data length changed. Only reset when paginationObj do not have total
Expand Down

0 comments on commit 53a76f0

Please sign in to comment.