Table: fix showing only first page (#14724)

* Table: fix showing only first page

* correct condition of pagination in Table

* correct test file

* handle changes

* improve test case
This commit is contained in:
何乐 2019-02-05 23:11:08 +08:00 committed by zombieJ
parent 2b558af960
commit b3834e48b1
2 changed files with 10 additions and 9 deletions

View File

@ -477,7 +477,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}
// Controlled current prop will not respond user interaction
if (typeof props.pagination === 'object' && 'current' in (props.pagination as Object)) {
if (typeof props.pagination === 'object' && 'current' in props.pagination) {
newState.pagination = {
...pagination,
current: this.state.pagination.current,
@ -670,11 +670,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
pagination,
};
// Controlled current prop will not respond user interaction
if (
props.pagination &&
typeof props.pagination === 'object' &&
'current' in (props.pagination as Object)
) {
if (props.pagination && typeof props.pagination === 'object' && 'current' in props.pagination) {
newState.pagination = {
...pagination,
current: this.state.pagination.current,
@ -727,7 +723,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
getRecordKey = (record: T, index: number) => {
const { rowKey } = this.props;
const recordKey =
typeof rowKey === 'function' ? rowKey(record, index) : (record as any)[rowKey as string];
typeof rowKey === 'function' ? rowKey(record, index) : (record as any)[rowKey!];
warning(
recordKey !== undefined,
'Each record in dataSource of table should have a unique `key` prop, ' +
@ -956,7 +952,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
const { pagination } = this.state;
if (pagination.size) {
size = pagination.size;
} else if ((this.props.size as string) === 'middle' || this.props.size === 'small') {
} else if (this.props.size === 'middle' || this.props.size === 'small') {
size = 'small';
}
const position = pagination.position || 'bottom';
@ -1012,6 +1008,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
let current: number;
let pageSize: number;
const state = this.state;
const pagination = this.props.pagination || {};
// 如果没有分页的话,默认全部展示
if (!this.hasPagination()) {
pageSize = Number.MAX_VALUE;
@ -1028,7 +1025,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
if (
data.length > pageSize ||
pageSize === Number.MAX_VALUE ||
current * pageSize > data.length
(pagination.current === undefined && current * pageSize > data.length)
) {
data = data.filter((_, i) => {
return i >= (current - 1) * pageSize && i < current * pageSize;

View File

@ -190,5 +190,9 @@ describe('Table.pagination', () => {
expect(renderedNames(wrapper)[0]).toEqual('Jack');
wrapper.find('.ant-pagination-item-2').simulate('click');
expect(renderedNames(wrapper)).toEqual([]);
wrapper.setProps({ pagination: { current: 1, pageSize: 10, total: 100 } });
expect(renderedNames(wrapper)[0]).toEqual('Jack');
wrapper.setProps({ pagination: { current: 2, pageSize: 10, total: 100 } });
expect(renderedNames(wrapper)).toHaveLength(4);
});
});