Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pagination type in Table and Grid #1042

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions packages/zent/src/grid/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ import { PureComponent } from 'react';
import classnames from 'classnames';
import size from 'lodash-es/size';
import Pagination from '../pagination';
import LitePagination from '../pagination/LitePagination';
import { IGridPageInfo, GridPaginationType } from './Grid';
import { IGridOnChangeConfig } from './types';
import { PaginationChangeHandler } from '../pagination/impl/BasePagination';

const defaultPageInfo = {
current: 1,
pageSize: 10,
};

class Footer extends PureComponent<any> {
hasPagination(props) {
export interface IGridFooterProps {
prefix: string;
pageInfo: IGridPageInfo;
paginationType: GridPaginationType;
onChange: (conf: IGridOnChangeConfig) => any;
onPaginationChange: (pageSize: number, current: number) => any;
}

class Footer extends PureComponent<IGridFooterProps> {
hasPagination(props: IGridFooterProps) {
const { pageInfo } = props || this.props;
return pageInfo && size(pageInfo);
}

getDefaultPagination(props?: any) {
getDefaultPagination(props?: IGridFooterProps) {
const { pageInfo } = props || this.props;

return this.hasPagination(props)
Expand All @@ -27,20 +39,28 @@ class Footer extends PureComponent<any> {
: null;
}

handlePageChange = ({ pageSize, current }) => {
handlePageChange: PaginationChangeHandler = ({ pageSize, current }) => {
const { onPaginationChange } = this.props;
onPaginationChange && onPaginationChange(pageSize, current);
};

render() {
const { prefix } = this.props;
const { prefix, paginationType } = this.props;
const curPageInfo = this.getDefaultPagination();

if (curPageInfo) {
return (
<div className={`${prefix}-grid-tfoot`}>
<div className={classnames(`${prefix}-grid-tfoot-page`)}>
<Pagination {...curPageInfo} onChange={this.handlePageChange} />
{paginationType === 'default' && (
<Pagination {...curPageInfo} onChange={this.handlePageChange} />
)}
{paginationType === 'lite' && (
<LitePagination
{...curPageInfo}
onChange={this.handlePageChange}
/>
)}
</div>
</div>
);
Expand Down
21 changes: 14 additions & 7 deletions packages/zent/src/grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ function stopPropagation(e) {
}
}

export type GridPaginationType = 'default' | 'lite';

export interface IGridPageInfo {
current?: number;
total?: number;
pageSize?: number;
pageSizeOptions?: PaginationPageSizeOption[];
}

export interface IGridProps {
columns: IGridColumn[];
datasets: Array<{}>;
Expand Down Expand Up @@ -74,12 +83,8 @@ export interface IGridProps {
className?: string;
rowClassName?: GridRowClassNameType;
prefix?: string;
pageInfo?: {
current?: number;
total?: number;
pageSize?: number;
pageSizeOptions?: PaginationPageSizeOption[];
};
pageInfo?: IGridPageInfo;
paginationType?: GridPaginationType;
onRowClick?: (
data: any,
index: number,
Expand Down Expand Up @@ -107,6 +112,7 @@ export class Grid extends PureComponent<IGridProps, any> {
columns: [],
loading: false,
pageInfo: false,
paginationType: 'default',
onChange: noop,
expandation: null,
selection: null,
Expand Down Expand Up @@ -801,7 +807,7 @@ export class Grid extends PureComponent<IGridProps, any> {
}

render() {
const { prefix, loading, pageInfo, bordered } = this.props;
const { prefix, loading, pageInfo, paginationType, bordered } = this.props;
let className = `${prefix}-grid`;
const borderedClassName = bordered ? `${prefix}-grid-bordered` : '';
className = classnames(className, this.props.className, borderedClassName);
Expand Down Expand Up @@ -829,6 +835,7 @@ export class Grid extends PureComponent<IGridProps, any> {
key="footer"
prefix={prefix}
pageInfo={pageInfo}
paginationType={paginationType}
onChange={this.onChange}
onPaginationChange={this.onPaginationChange}
/>,
Expand Down
73 changes: 37 additions & 36 deletions packages/zent/src/grid/README_en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ The function of the component is similar to the function of [Table](table) compo

### API

| Property | Descripition | Type | Default | Required |
| ------------ | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | ----------- | -------- |
| columns | columns | array | | Yes |
| datasets | data to be displayed | array | | Yes |
| rowKey | key for each row | string | `id` | No |
| onChange | callback fires when columns change, filtering and sorting included | (conf: any) => any | `noop` | No |
| scroll | can be scrolled in x/y direction, x or y can be a number that indicates the width and height of table body | { x?: number, y?: number } | | No |
| sortBy | the field which rows are sorted accoring to, should be one of keys for columns | string | '' | No |
| sortType | The way to sort | string | '' | No |
| emptyLabel | Text to be displayed when there's no data | string | `'No data'` | No |
| selection | the configuration for selection | object | | No |
| expandation | Expand configuration | object | | | no |
| loading | determines whether data is being loaded or not | bool | `false` | No |
| className | extra custom class name | string | `''` | No |
| rowClassName | class name for row | string \| (data: object, rowIndex: number) => string | '' | No |
| prefix | custom prefix | string | `'zent'` | No |
| pageInfo | pagination information | object | null | No |
| onRowClick | callback fires when a row is clicked | (data: any, index: number, event: Event) => any | | No |
| ellipsis | whether ellipsis should be displayed when content overflows (nowrap of columns needs to be set) | bool | false | No |
| onExpand | callback fires when the row expand icon is clicked | (data: {expanded: boolean, data: any, event: Event, index: number}) => any | | No |
| components | custom table element | object { row?: ReactNode } | | No|
| rowProps | custom row props | (data: any, index: number) => object | | No |
| bordered | whether to display the outer border and column border | bool | `false` | No |
| Property | Descripition | Type | Default | Required |
| -------------- | ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------- | -------- |
| columns | columns | array | | Yes |
| datasets | data to be displayed | array | | Yes |
| rowKey | key for each row | string | `id` | No |
| onChange | callback fires when columns change, filtering and sorting included | (conf: any) => any | `noop` | No |
| scroll | can be scrolled in x/y direction, x or y can be a number that indicates the width and height of table body | { x?: number, y?: number } | | No |
| sortBy | the field which rows are sorted accoring to, should be one of keys for columns | string | '' | No |
| sortType | The way to sort | string | '' | No |
| emptyLabel | Text to be displayed when there's no data | string | `'No data'` | No |
| selection | the configuration for selection | object | | No |
| expandation | Expand configuration | object | | | no |
| loading | determines whether data is being loaded or not | bool | `false` | No |
| className | extra custom class name | string | `''` | No |
| rowClassName | class name for row | string \| (data: object, rowIndex: number) => string | '' | No |
| prefix | custom prefix | string | `'zent'` | No |
| pageInfo | pagination information | object | null | No |
| paginationType | Pagination type, `'default'` \| `'lite'` | string | `'default'` | No |
| onRowClick | callback fires when a row is clicked | (data: any, index: number, event: Event) => any | | No |
| ellipsis | whether ellipsis should be displayed when content overflows (nowrap of columns needs to be set) | bool | false | No |
| onExpand | callback fires when the row expand icon is clicked | (data: {expanded: boolean, data: any, event: Event, index: number}) => any | | No |
| components | custom table element | object { row?: ReactNode } | | No |
| rowProps | custom row props | (data: any, index: number) => object | | No |
| bordered | whether to display the outer border and column border | bool | `false` | No |

#### onChange function declaration

Expand All @@ -59,28 +60,28 @@ onChange will throw an object, which includes parameters about the change part o
| needSort | whether to support sorting | bool | No |
| colSpan | span of columns. It won't be rendered if the value is set to 0 | number | No |
| fixed | whether columns fixed or not. The value can be `left` `right` `true` (`true` is same to `left`) | bool \| strig | No |
| onCellClick | callback fires when a cell is clicked | (data: any, event: Event) => any | No |
| onCellClick | callback fires when a cell is clicked | (data: any, event: Event) => any | No |
| textAlign | Text alignment | string | No |
| nowrap | whether to wrap, true by default | bool | No |
| defaultText | default display text | ReactNode | No |
| children | render grouping table headers | array | No |
| defaultText | default display text | ReactNode | No |
| children | render grouping table headers | array | No |

#### selection

| Property | Description | Type | Required |
| ---------------- | ------------------------------------------ | ---------------------------------------------------------------------------- | -------- |
| selectedRowKeys | keys of selected rows by default | array | No |
| Property | Description | Type | Required |
| ---------------- | ------------------------------------------ | --------------------------------------------------------------------------- | -------- |
| selectedRowKeys | keys of selected rows by default | array | No |
| onSelect | callback fires when a check changes | (selectedkeys: string, selectedRows: Array<any>, currentRow: number) => any | No |
| getCheckboxProps | function to get properties of the checkbox | (data: object) => { disabled?: boolean } | No |
| getCheckboxProps | function to get properties of the checkbox | (data: object) => { disabled?: boolean } | No |

#### pageInfo

| Property | Description | Type | Required |
| --------- | ---------------------------------------- | ------ | -------- |
| total | Total number of items | number | No |
| pageSize | Number of items to be displayed per page | number | No |
| pageSizeOptions | Page size options | number[] | No |
| current | current page | number | No |
| Property | Description | Type | Required |
| --------------- | ---------------------------------------- | -------- | -------- |
| total | Total number of items | number | No |
| pageSize | Number of items to be displayed per page | number | No |
| pageSizeOptions | Page size options | number[] | No |
| current | current page | number | No |

### expandation

Expand Down
Loading