feature: [Table] Pagination on the top in addition to the bottom. fix… (#9357)

* feature: [Table] Pagination on the top in addition to the bottom. fix (#9346)

* refactor pagination of table (#9357)

* specify the position of pagination when using table  - add test

* specify the position of pagination when using table - update API docs

* update api docs

* fix error

* add TablePaginationConfig extends PaginationProps
This commit is contained in:
Camol 2018-03-02 12:00:28 +08:00 committed by Wei Zhu
parent bea95bf77c
commit ae2990e241
6 changed files with 52 additions and 11 deletions

View File

@ -3,7 +3,7 @@ import * as ReactDOM from 'react-dom';
import RcTable from 'rc-table'; import RcTable from 'rc-table';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import Pagination, { PaginationProps } from '../pagination'; import Pagination from '../pagination';
import Icon from '../icon'; import Icon from '../icon';
import Spin from '../spin'; import Spin from '../spin';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
@ -28,6 +28,7 @@ import {
CompareFn, CompareFn,
TableStateFilters, TableStateFilters,
SelectionItemSelectFn, SelectionItemSelectFn,
TablePaginationConfig,
} from './interface'; } from './interface';
function noop() { function noop() {
@ -147,7 +148,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
} }
getDefaultPagination(props: TableProps<T>) { getDefaultPagination(props: TableProps<T>) {
const pagination: PaginationProps = props.pagination || {}; const pagination: TablePaginationConfig = props.pagination || {};
return this.hasPagination(props) ? return this.hasPagination(props) ?
{ {
...defaultPagination, ...defaultPagination,
@ -778,7 +779,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
} }
} }
renderPagination() { renderPagination(paginationPosition: string) {
// 强制不需要分页 // 强制不需要分页
if (!this.hasPagination()) { if (!this.hasPagination()) {
return null; return null;
@ -790,10 +791,11 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
} else if (this.props.size as string === 'middle' || this.props.size === 'small') { } else if (this.props.size as string === 'middle' || this.props.size === 'small') {
size = 'small'; size = 'small';
} }
let position = pagination.position || 'bottom';
let total = pagination.total || this.getLocalData().length; let total = pagination.total || this.getLocalData().length;
return (total > 0) ? ( return (total > 0 && (position === paginationPosition || position === 'both')) ? (
<Pagination <Pagination
key="pagination" key={`pagination-${paginationPosition}`}
{...pagination} {...pagination}
className={classNames(pagination.className, `${this.props.prefixCls}-pagination`)} className={classNames(pagination.className, `${this.props.prefixCls}-pagination`)}
onChange={this.handlePageChange} onChange={this.handlePageChange}
@ -993,8 +995,9 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
{...loading} {...loading}
className={loading.spinning ? `${paginationPatchClass} ${prefixCls}-spin-holder` : ''} className={loading.spinning ? `${paginationPatchClass} ${prefixCls}-spin-holder` : ''}
> >
{this.renderPagination('top')}
{table} {table}
{this.renderPagination()} {this.renderPagination('bottom')}
</Spin> </Spin>
</div> </div>
); );

View File

@ -131,4 +131,17 @@ describe('Table.pagination', () => {
wrapper.setProps({ dataSource: [data[0]] }); wrapper.setProps({ dataSource: [data[0]] });
expect(wrapper.find('.ant-pagination-item-1').hasClass('ant-pagination-item-active')).toBe(true); expect(wrapper.find('.ant-pagination-item-1').hasClass('ant-pagination-item-active')).toBe(true);
}); });
it('specify the position of pagination', () => {
const wrapper = mount(createTable({ pagination: { position: 'top' } }));
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(2);
expect(wrapper.find('.ant-spin-container').childAt(0).find('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { position: 'bottom' } });
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(2);
expect(wrapper.find('.ant-spin-container').childAt(1).find('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { position: 'both' } });
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(3);
expect(wrapper.find('.ant-spin-container').childAt(0).find('.ant-pagination')).toHaveLength(1);
expect(wrapper.find('.ant-spin-container').childAt(2).find('.ant-pagination')).toHaveLength(1);
});
}); });

View File

@ -65,7 +65,7 @@ const columns = [{
| indentSize | Indent size in pixels of tree data | number | 15 | | indentSize | Indent size in pixels of tree data | number | 15 |
| loading | Loading status of table | boolean\|[object](https://ant.design/components/spin-cn/#API) ([more](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | `false` | | loading | Loading status of table | boolean\|[object](https://ant.design/components/spin-cn/#API) ([more](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | `false` |
| locale | i18n text including filter, sort, empty text, etc | object | filterConfirm: 'Ok' <br> filterReset: 'Reset' <br> emptyText: 'No Data' <br> [Default](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) | | locale | i18n text including filter, sort, empty text, etc | object | filterConfirm: 'Ok' <br> filterReset: 'Reset' <br> emptyText: 'No Data' <br> [Default](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) |
| pagination | Pagination [config](/components/pagination/), hide it by setting it to `false` | object | | | pagination | Pagination [config](#pagination) or [`Pagination`] (/components/pagination/), hide it by setting it to `false` | object | |
| rowClassName | Row's className | Function(record, index):string | - | | rowClassName | Row's className | Function(record, index):string | - |
| rowKey | Row's unique key, could be a string or function that returns a string | string\|Function(record):string | `key` | | rowKey | Row's unique key, could be a string or function that returns a string | string\|Function(record):string | `key` |
| rowSelection | Row selection [config](#rowSelection) | object | null | | rowSelection | Row selection [config](#rowSelection) | object | null |
@ -135,6 +135,16 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| -------- | ----------- | ---- | ------- | | -------- | ----------- | ---- | ------- |
| title | Title of the column group | string\|ReactNode | - | | title | Title of the column group | string\|ReactNode | - |
### pagination
Properties for pagination.
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| position | specify the position of `Pagination` | 'top' \| 'bottom' \| 'both' | 'bottom' |
More about pagination, please check [`Pagination`](/components/pagination/).
### rowSelection ### rowSelection
Properties for row selection. Properties for row selection.

View File

@ -66,7 +66,7 @@ const columns = [{
| indentSize | 展示树形数据时,每层缩进的宽度,以 px 为单位 | number | 15 | | indentSize | 展示树形数据时,每层缩进的宽度,以 px 为单位 | number | 15 |
| loading | 页面是否加载中 | boolean\|[object](https://ant.design/components/spin-cn/#API) ([更多](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | false | | loading | 页面是否加载中 | boolean\|[object](https://ant.design/components/spin-cn/#API) ([更多](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | false |
| locale | 默认文案设置,目前包括排序、过滤、空数据文案 | object | filterConfirm: '确定' <br> filterReset: '重置' <br> emptyText: '暂无数据' <br> [默认值](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) | | locale | 默认文案设置,目前包括排序、过滤、空数据文案 | object | filterConfirm: '确定' <br> filterReset: '重置' <br> emptyText: '暂无数据' <br> [默认值](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) |
| pagination | 分页器,配置项参考 [pagination](/components/pagination/),设为 false 时不展示和进行分页 | object | | | pagination | 分页器,参考[配置项](#pagination)或 [pagination](/components/pagination/),设为 false 时不展示和进行分页 | object | |
| rowClassName | 表格行的类名 | Function(record, index):string | - | | rowClassName | 表格行的类名 | Function(record, index):string | - |
| rowKey | 表格行 key 的取值,可以是字符串或一个函数 | string\|Function(record):string | 'key' | | rowKey | 表格行 key 的取值,可以是字符串或一个函数 | string\|Function(record):string | 'key' |
| rowSelection | 列表项是否可选择,[配置项](#rowSelection) | object | null | | rowSelection | 列表项是否可选择,[配置项](#rowSelection) | object | null |
@ -136,6 +136,16 @@ const columns = [{
| --- | --- | --- | --- | | --- | --- | --- | --- |
| title | 列头显示文字 | string\|ReactNode | - | | title | 列头显示文字 | string\|ReactNode | - |
### pagination
分页的配置项。
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| position | 指定分页显示的位置 | 'top' \| 'bottom' \| 'both' | 'bottom' |
更多配置项,请查看 [`Pagination`](/components/pagination/)。
### rowSelection ### rowSelection
选择功能的配置。 选择功能的配置。

View File

@ -58,6 +58,10 @@ export interface TableLocale {
export type RowSelectionType = 'checkbox' | 'radio'; export type RowSelectionType = 'checkbox' | 'radio';
export type SelectionSelectFn<T> = (record: T, selected: boolean, selectedRows: Object[]) => any; export type SelectionSelectFn<T> = (record: T, selected: boolean, selectedRows: Object[]) => any;
export interface TablePaginationConfig extends PaginationProps {
position?: 'top' | 'bottom' | 'both';
}
export interface TableRowSelection<T> { export interface TableRowSelection<T> {
type?: RowSelectionType; type?: RowSelectionType;
selectedRowKeys?: string[] | number[]; selectedRowKeys?: string[] | number[];
@ -75,7 +79,7 @@ export interface TableProps<T> {
prefixCls?: string; prefixCls?: string;
dropdownPrefixCls?: string; dropdownPrefixCls?: string;
rowSelection?: TableRowSelection<T>; rowSelection?: TableRowSelection<T>;
pagination?: PaginationProps | false; pagination?: TablePaginationConfig | false;
size?: 'default' | 'middle' | 'small'; size?: 'default' | 'middle' | 'small';
dataSource?: T[]; dataSource?: T[];
components?: TableComponents; components?: TableComponents;
@ -91,7 +95,7 @@ export interface TableProps<T> {
expandRowByClick?: boolean; expandRowByClick?: boolean;
onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void; onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void;
onExpand?: (expanded: boolean, record: T) => void; onExpand?: (expanded: boolean, record: T) => void;
onChange?: (pagination: PaginationProps | boolean, filters: string[], sorter: Object) => any; onChange?: (pagination: TablePaginationConfig | boolean, filters: string[], sorter: Object) => any;
loading?: boolean | SpinProps; loading?: boolean | SpinProps;
locale?: Object; locale?: Object;
indentSize?: number; indentSize?: number;
@ -115,7 +119,7 @@ export interface TableStateFilters {
} }
export interface TableState<T> { export interface TableState<T> {
pagination: PaginationProps; pagination: TablePaginationConfig;
filters: TableStateFilters; filters: TableStateFilters;
sortColumn: ColumnProps<T> | null; sortColumn: ColumnProps<T> | null;
sortOrder: string; sortOrder: string;

View File

@ -12,6 +12,7 @@
.reset-component; .reset-component;
position: relative; position: relative;
border-radius: @border-radius-base @border-radius-base 0 0; border-radius: @border-radius-base @border-radius-base 0 0;
clear: both;
&-body { &-body {
transition: opacity .3s; transition: opacity .3s;