mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 11:32:52 +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-selector').simulate('mousedown');
|
||||||
wrapper.find('.ant-select-item').last().simulate('click');
|
wrapper.find('.ant-select-item').last().simulate('click');
|
||||||
expect(scrollTo).toHaveBeenCalledTimes(3);
|
expect(scrollTo).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires change event', () => {
|
it('fires change event', () => {
|
||||||
@ -334,6 +334,24 @@ describe('Table.pagination', () => {
|
|||||||
expect(wrapper.render()).toMatchSnapshot();
|
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', () => {
|
it('dynamic warning', () => {
|
||||||
resetWarned();
|
resetWarned();
|
||||||
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
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({
|
setInnerPagination({
|
||||||
...mergedPagination,
|
...mergedPagination,
|
||||||
current,
|
current,
|
||||||
|
pageSize: pageSize || mergedPagination.pageSize,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const onInternalChange: PaginationProps['onChange'] = (...args) => {
|
const onInternalChange: PaginationProps['onChange'] = (current, pageSize) => {
|
||||||
const [current] = args;
|
const paginationPageSize = mergedPagination?.pageSize;
|
||||||
refreshPagination(current);
|
if (pageSize && pageSize !== paginationPageSize) {
|
||||||
|
current = 1;
|
||||||
onChange(current, args[1] || mergedPagination.pageSize!);
|
if (pagination && pagination.onShowSizeChange) pagination.onShowSizeChange(current, pageSize);
|
||||||
|
|
||||||
if (pagination && pagination.onChange) {
|
|
||||||
pagination.onChange(...args);
|
|
||||||
}
|
}
|
||||||
};
|
if (pagination && pagination.onChange) pagination.onChange(current, pageSize);
|
||||||
|
|
||||||
const onInternalShowSizeChange: PaginationProps['onShowSizeChange'] = (...args) => {
|
refreshPagination(current, pageSize);
|
||||||
const [, pageSize] = args;
|
onChange(current, pageSize || paginationPageSize!);
|
||||||
setInnerPagination({
|
|
||||||
...mergedPagination,
|
|
||||||
current: 1,
|
|
||||||
pageSize,
|
|
||||||
});
|
|
||||||
|
|
||||||
onChange(1, pageSize);
|
|
||||||
|
|
||||||
if (pagination && pagination.onShowSizeChange) {
|
|
||||||
pagination.onShowSizeChange(...args);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (pagination === false) {
|
if (pagination === false) {
|
||||||
@ -116,7 +103,6 @@ export default function usePagination(
|
|||||||
{
|
{
|
||||||
...mergedPagination,
|
...mergedPagination,
|
||||||
onChange: onInternalChange,
|
onChange: onInternalChange,
|
||||||
onShowSizeChange: onInternalShowSizeChange,
|
|
||||||
},
|
},
|
||||||
refreshPagination,
|
refreshPagination,
|
||||||
];
|
];
|
||||||
|
Loading…
Reference in New Issue
Block a user