fix: Table onChange event is triggered twice (#25520)

* fix: Table onChange event is triggered twice

* fix: Table onChange event is triggered twice

* test: should call onChange when change pagination size

* update: code style
This commit is contained in:
zhangchen 2020-07-10 15:17:15 +08:00 committed by GitHub
parent 6de959ce1a
commit d04bd2fabc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 25 deletions

View File

@ -100,7 +100,7 @@ describe('Table.pagination', () => {
wrapper.find('.ant-select-selector').simulate('mousedown');
wrapper.find('.ant-select-item').last().simulate('click');
expect(scrollTo).toHaveBeenCalledTimes(3);
expect(scrollTo).toHaveBeenCalledTimes(2);
});
it('fires change event', () => {
@ -334,6 +334,24 @@ describe('Table.pagination', () => {
expect(wrapper.render()).toMatchSnapshot();
});
it('should call onChange when change pagination size', () => {
const onChange = jest.fn();
const wrapper = mount(
createTable({
pagination: {
total: 200,
showSizeChanger: true,
},
onChange,
}),
);
wrapper.find('.ant-select-selector').simulate('mousedown');
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
dropdownWrapper.find('.ant-select-item-option').at(2).simulate('click');
expect(onChange).toBeCalledTimes(1)
});
it('dynamic warning', () => {
resetWarned();
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

View File

@ -75,37 +75,24 @@ export default function usePagination(
}
}
const refreshPagination = (current: number = 1) => {
const refreshPagination = (current: number = 1, pageSize?: number) => {
setInnerPagination({
...mergedPagination,
current,
pageSize: pageSize || mergedPagination.pageSize,
});
};
const onInternalChange: PaginationProps['onChange'] = (...args) => {
const [current] = args;
refreshPagination(current);
onChange(current, args[1] || mergedPagination.pageSize!);
if (pagination && pagination.onChange) {
pagination.onChange(...args);
const onInternalChange: PaginationProps['onChange'] = (current, pageSize) => {
const paginationPageSize = mergedPagination?.pageSize;
if (pageSize && pageSize !== paginationPageSize) {
current = 1;
if (pagination && pagination.onShowSizeChange) pagination.onShowSizeChange(current, pageSize);
}
};
if (pagination && pagination.onChange) pagination.onChange(current, pageSize);
const onInternalShowSizeChange: PaginationProps['onShowSizeChange'] = (...args) => {
const [, pageSize] = args;
setInnerPagination({
...mergedPagination,
current: 1,
pageSize,
});
onChange(1, pageSize);
if (pagination && pagination.onShowSizeChange) {
pagination.onShowSizeChange(...args);
}
refreshPagination(current, pageSize);
onChange(current, pageSize || paginationPageSize!);
};
if (pagination === false) {
@ -116,7 +103,6 @@ export default function usePagination(
{
...mergedPagination,
onChange: onInternalChange,
onShowSizeChange: onInternalShowSizeChange,
},
refreshPagination,
];