mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 09:26:06 +08:00
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:
parent
6de959ce1a
commit
d04bd2fabc
@ -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(() => {});
|
||||
|
@ -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,
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user