2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2016-06-19 10:56:42 +08:00
|
|
|
import RcTable from 'rc-table';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-06-19 10:56:42 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-17 15:23:25 +08:00
|
|
|
import Pagination, { PaginationProps } from '../pagination';
|
|
|
|
import Icon from '../icon';
|
2017-11-21 13:48:37 +08:00
|
|
|
import Spin from '../spin';
|
2017-10-20 14:54:38 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2017-10-26 10:50:18 +08:00
|
|
|
import defaultLocale from '../locale-provider/default';
|
2016-11-01 11:10:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2017-03-17 15:23:25 +08:00
|
|
|
import FilterDropdown from './filterDropdown';
|
2016-11-11 15:26:51 +08:00
|
|
|
import createStore, { Store } from './createStore';
|
|
|
|
import SelectionBox from './SelectionBox';
|
2017-11-21 13:48:37 +08:00
|
|
|
import SelectionCheckboxAll from './SelectionCheckboxAll';
|
|
|
|
import Column from './Column';
|
2016-11-22 10:11:12 +08:00
|
|
|
import ColumnGroup from './ColumnGroup';
|
2017-11-18 00:36:22 +08:00
|
|
|
import createBodyRow from './createBodyRow';
|
2017-03-17 15:23:25 +08:00
|
|
|
import { flatArray, treeMap, flatFilter, normalizeColumns } from './util';
|
2017-11-21 13:48:37 +08:00
|
|
|
import {
|
|
|
|
TableProps,
|
|
|
|
TableState,
|
|
|
|
TableComponents,
|
|
|
|
RowSelectionType,
|
|
|
|
TableLocale,
|
|
|
|
ColumnProps,
|
|
|
|
CompareFn,
|
|
|
|
TableStateFilters,
|
|
|
|
SelectionItemSelectFn,
|
|
|
|
} from './interface';
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
function stopPropagation(e: React.SyntheticEvent<any>) {
|
2016-06-19 10:56:42 +08:00
|
|
|
e.stopPropagation();
|
2016-06-23 16:00:30 +08:00
|
|
|
if (e.nativeEvent.stopImmediatePropagation) {
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultPagination = {
|
|
|
|
onChange: noop,
|
|
|
|
onShowSizeChange: noop,
|
|
|
|
};
|
|
|
|
|
2017-03-14 19:51:28 +08:00
|
|
|
/**
|
|
|
|
* Avoid creating new object, so that parent component's shouldComponentUpdate
|
|
|
|
* can works appropriately。
|
|
|
|
*/
|
|
|
|
const emptyObject = {};
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
export default class Table<T> extends React.Component<TableProps<T>, TableState<T>> {
|
2016-11-22 10:11:12 +08:00
|
|
|
static Column = Column;
|
|
|
|
static ColumnGroup = ColumnGroup;
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
static propTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
dataSource: PropTypes.array,
|
|
|
|
columns: PropTypes.array,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
useFixedHeader: PropTypes.bool,
|
|
|
|
rowSelection: PropTypes.object,
|
|
|
|
className: PropTypes.string,
|
|
|
|
size: PropTypes.string,
|
|
|
|
loading: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.object,
|
2017-02-13 15:48:08 +08:00
|
|
|
]),
|
2017-04-12 04:49:08 +08:00
|
|
|
bordered: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
locale: PropTypes.object,
|
|
|
|
dropdownPrefixCls: PropTypes.string,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
dataSource: [],
|
|
|
|
prefixCls: 'ant-table',
|
|
|
|
useFixedHeader: false,
|
|
|
|
rowSelection: null,
|
|
|
|
className: '',
|
|
|
|
size: 'large',
|
|
|
|
loading: false,
|
|
|
|
bordered: false,
|
|
|
|
indentSize: 20,
|
|
|
|
locale: {},
|
2016-07-26 21:14:13 +08:00
|
|
|
rowKey: 'key',
|
2017-01-20 11:33:27 +08:00
|
|
|
showHeader: true,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
CheckboxPropsCache: {
|
|
|
|
[key: string]: any;
|
|
|
|
};
|
2016-11-11 15:26:51 +08:00
|
|
|
store: Store;
|
2016-11-22 10:11:12 +08:00
|
|
|
columns: ColumnProps<T>[];
|
2017-11-18 00:03:30 +08:00
|
|
|
components: TableComponents;
|
2016-08-11 15:38:03 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
constructor(props: TableProps<T>) {
|
2016-06-19 10:56:42 +08:00
|
|
|
super(props);
|
|
|
|
|
2016-09-14 11:42:06 +08:00
|
|
|
warning(
|
|
|
|
!('columnsPageRange' in props || 'columnsPageSize' in props),
|
|
|
|
'`columnsPageRange` and `columnsPageSize` are removed, please use ' +
|
2017-07-19 13:59:53 +08:00
|
|
|
'fixed columns instead, see: https://u.ant.design/fixed-columns.',
|
2016-09-14 11:42:06 +08:00
|
|
|
);
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
this.columns = props.columns || normalizeColumns(props.children as React.ReactChildren);
|
2016-11-22 10:11:12 +08:00
|
|
|
|
2017-11-18 00:03:30 +08:00
|
|
|
this.createComponents(props.components);
|
|
|
|
|
2017-07-03 16:57:11 +08:00
|
|
|
this.state = {
|
2017-11-09 19:30:24 +08:00
|
|
|
...this.getDefaultSortOrder(this.columns),
|
2016-06-19 10:56:42 +08:00
|
|
|
// 减少状态
|
|
|
|
filters: this.getFiltersFromColumns(),
|
2017-01-09 20:49:00 +08:00
|
|
|
pagination: this.getDefaultPagination(props),
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
this.CheckboxPropsCache = {};
|
2016-11-11 15:26:51 +08:00
|
|
|
|
|
|
|
this.store = createStore({
|
|
|
|
selectedRowKeys: (props.rowSelection || {}).selectedRowKeys || [],
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getCheckboxPropsByItem = (item: T, index: number) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const { rowSelection = {} } = this.props;
|
|
|
|
if (!rowSelection.getCheckboxProps) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-12-16 17:53:08 +08:00
|
|
|
const key = this.getRecordKey(item, index);
|
2016-06-19 10:56:42 +08:00
|
|
|
// Cache checkboxProps
|
|
|
|
if (!this.CheckboxPropsCache[key]) {
|
|
|
|
this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item);
|
|
|
|
}
|
|
|
|
return this.CheckboxPropsCache[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultSelection() {
|
|
|
|
const { rowSelection = {} } = this.props;
|
|
|
|
if (!rowSelection.getCheckboxProps) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-08-03 21:27:45 +08:00
|
|
|
return this.getFlatData()
|
2017-11-21 13:48:37 +08:00
|
|
|
.filter((item: T, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
|
2016-06-19 10:56:42 +08:00
|
|
|
.map((record, rowIndex) => this.getRecordKey(record, rowIndex));
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getDefaultPagination(props: TableProps<T>) {
|
|
|
|
const pagination: PaginationProps = props.pagination || {};
|
2017-01-09 20:49:00 +08:00
|
|
|
return this.hasPagination(props) ?
|
2017-07-03 16:57:11 +08:00
|
|
|
{
|
|
|
|
...defaultPagination,
|
|
|
|
...pagination,
|
2017-01-09 20:49:00 +08:00
|
|
|
current: pagination.defaultCurrent || pagination.current || 1,
|
|
|
|
pageSize: pagination.defaultPageSize || pagination.pageSize || 10,
|
2017-07-03 16:57:11 +08:00
|
|
|
} : {};
|
2017-01-09 20:49:00 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
componentWillReceiveProps(nextProps: TableProps<T>) {
|
|
|
|
this.columns = nextProps.columns || normalizeColumns(nextProps.children as React.ReactChildren);
|
2017-02-07 21:09:51 +08:00
|
|
|
if ('pagination' in nextProps || 'pagination' in this.props) {
|
2016-06-19 10:56:42 +08:00
|
|
|
this.setState(previousState => {
|
2017-07-03 16:57:11 +08:00
|
|
|
const newPagination = {
|
|
|
|
...defaultPagination,
|
|
|
|
...previousState.pagination,
|
|
|
|
...nextProps.pagination,
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
newPagination.current = newPagination.current || 1;
|
2017-01-09 20:49:00 +08:00
|
|
|
newPagination.pageSize = newPagination.pageSize || 10;
|
2017-03-14 19:51:28 +08:00
|
|
|
return { pagination: nextProps.pagination !== false ? newPagination : emptyObject };
|
2017-01-09 20:49:00 +08:00
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
if (nextProps.rowSelection &&
|
|
|
|
'selectedRowKeys' in nextProps.rowSelection) {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectedRowKeys: nextProps.rowSelection.selectedRowKeys || [],
|
|
|
|
});
|
2016-06-21 19:35:29 +08:00
|
|
|
const { rowSelection } = this.props;
|
|
|
|
if (rowSelection && (
|
|
|
|
nextProps.rowSelection.getCheckboxProps !== rowSelection.getCheckboxProps
|
|
|
|
)) {
|
2016-06-19 10:56:42 +08:00
|
|
|
this.CheckboxPropsCache = {};
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 21:10:06 +08:00
|
|
|
if ('dataSource' in nextProps &&
|
|
|
|
nextProps.dataSource !== this.props.dataSource) {
|
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
this.CheckboxPropsCache = {};
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2017-02-08 14:43:00 +08:00
|
|
|
if (this.getSortOrderColumns(this.columns).length > 0) {
|
|
|
|
const sortState = this.getSortStateFromColumns(this.columns);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (sortState.sortColumn !== this.state.sortColumn ||
|
|
|
|
sortState.sortOrder !== this.state.sortOrder) {
|
|
|
|
this.setState(sortState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:43:00 +08:00
|
|
|
const filteredValueColumns = this.getFilteredValueColumns(this.columns);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (filteredValueColumns.length > 0) {
|
2017-02-08 14:43:00 +08:00
|
|
|
const filtersFromColumns = this.getFiltersFromColumns(this.columns);
|
2017-07-03 16:57:11 +08:00
|
|
|
const newFilters = { ...this.state.filters };
|
2016-06-19 10:56:42 +08:00
|
|
|
Object.keys(filtersFromColumns).forEach(key => {
|
|
|
|
newFilters[key] = filtersFromColumns[key];
|
|
|
|
});
|
|
|
|
if (this.isFiltersChanged(newFilters)) {
|
|
|
|
this.setState({ filters: newFilters });
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 00:03:30 +08:00
|
|
|
|
|
|
|
this.createComponents(nextProps.components, this.props.components);
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
onRow = (record: T, index: number) => {
|
2017-11-18 00:03:30 +08:00
|
|
|
const { onRow, prefixCls } = this.props;
|
|
|
|
const custom = onRow ? onRow(record, index) : {};
|
|
|
|
return {
|
|
|
|
...custom,
|
|
|
|
prefixCls,
|
|
|
|
store: this.store,
|
|
|
|
rowKey: this.getRecordKey(record, index),
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
setSelectedRowKeys(selectedRowKeys: string[], { selectWay, record, checked, changeRowKeys }: any) {
|
|
|
|
const { rowSelection = {} as any } = this.props;
|
2016-08-03 21:08:13 +08:00
|
|
|
if (rowSelection && !('selectedRowKeys' in rowSelection)) {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({ selectedRowKeys });
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2016-08-03 21:27:45 +08:00
|
|
|
const data = this.getFlatData();
|
2016-08-03 21:08:13 +08:00
|
|
|
if (!rowSelection.onChange && !rowSelection[selectWay]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selectedRows = data.filter(
|
2017-03-23 21:15:49 +08:00
|
|
|
(row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2016-08-03 21:08:13 +08:00
|
|
|
);
|
|
|
|
if (rowSelection.onChange) {
|
|
|
|
rowSelection.onChange(selectedRowKeys, selectedRows);
|
|
|
|
}
|
|
|
|
if (selectWay === 'onSelect' && rowSelection.onSelect) {
|
|
|
|
rowSelection.onSelect(record, checked, selectedRows);
|
|
|
|
} else if (selectWay === 'onSelectAll' && rowSelection.onSelectAll) {
|
|
|
|
const changeRows = data.filter(
|
2017-03-23 21:15:49 +08:00
|
|
|
(row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2016-06-19 10:56:42 +08:00
|
|
|
);
|
2016-08-03 21:08:13 +08:00
|
|
|
rowSelection.onSelectAll(checked, selectedRows, changeRows);
|
2017-02-23 19:29:47 +08:00
|
|
|
} else if (selectWay === 'onSelectInvert' && rowSelection.onSelectInvert) {
|
|
|
|
rowSelection.onSelectInvert(selectedRowKeys);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:49:00 +08:00
|
|
|
hasPagination(props?: any) {
|
|
|
|
return (props || this.props).pagination !== false;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
isFiltersChanged(filters: TableStateFilters) {
|
2016-06-19 10:56:42 +08:00
|
|
|
let filtersChanged = false;
|
|
|
|
if (Object.keys(filters).length !== Object.keys(this.state.filters).length) {
|
|
|
|
filtersChanged = true;
|
|
|
|
} else {
|
|
|
|
Object.keys(filters).forEach(columnKey => {
|
|
|
|
if (filters[columnKey] !== this.state.filters[columnKey]) {
|
|
|
|
filtersChanged = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return filtersChanged;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getSortOrderColumns(columns?: ColumnProps<T>[]) {
|
|
|
|
return flatFilter(
|
|
|
|
columns || this.columns || [],
|
|
|
|
(column: ColumnProps<T>) => 'sortOrder' in column,
|
|
|
|
);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getFilteredValueColumns(columns?: ColumnProps<T>[]) {
|
|
|
|
return flatFilter(
|
|
|
|
columns || this.columns || [],
|
|
|
|
(column: ColumnProps<T>) => typeof column.filteredValue !== 'undefined',
|
|
|
|
);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getFiltersFromColumns(columns?: ColumnProps<T>[]) {
|
|
|
|
let filters: any = {};
|
|
|
|
this.getFilteredValueColumns(columns).forEach((col: ColumnProps<T>) => {
|
|
|
|
const colKey = this.getColumnKey(col) as string;
|
|
|
|
filters[colKey] = col.filteredValue;
|
2016-06-19 10:56:42 +08:00
|
|
|
});
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getDefaultSortOrder(columns?: ColumnProps<T>[]) {
|
2017-11-09 19:30:24 +08:00
|
|
|
const definedSortState = this.getSortStateFromColumns(columns);
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
let defaultSortedColumn = flatFilter(columns || [], (column: ColumnProps<T>) => column.defaultSortOrder != null)[0];
|
2017-11-09 19:30:24 +08:00
|
|
|
|
|
|
|
if (defaultSortedColumn && !definedSortState.sortColumn) {
|
|
|
|
return {
|
|
|
|
sortColumn: defaultSortedColumn,
|
|
|
|
sortOrder: defaultSortedColumn.defaultSortOrder,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return definedSortState;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getSortStateFromColumns(columns?: ColumnProps<T>[]) {
|
2017-11-09 19:30:24 +08:00
|
|
|
// return first column which sortOrder is not falsy
|
2016-06-19 10:56:42 +08:00
|
|
|
const sortedColumn =
|
2017-11-21 13:48:37 +08:00
|
|
|
this.getSortOrderColumns(columns).filter((col: ColumnProps<T>) => col.sortOrder)[0];
|
2017-11-09 19:30:24 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
if (sortedColumn) {
|
|
|
|
return {
|
|
|
|
sortColumn: sortedColumn,
|
|
|
|
sortOrder: sortedColumn.sortOrder,
|
|
|
|
};
|
|
|
|
}
|
2017-11-09 19:30:24 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
return {
|
|
|
|
sortColumn: null,
|
|
|
|
sortOrder: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getSorterFn() {
|
|
|
|
const { sortOrder, sortColumn } = this.state;
|
|
|
|
if (!sortOrder || !sortColumn ||
|
|
|
|
typeof sortColumn.sorter !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 13:48:37 +08:00
|
|
|
|
|
|
|
return (a: T, b: T) => {
|
|
|
|
const result = (sortColumn!.sorter as CompareFn<T>)(a, b);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (result !== 0) {
|
|
|
|
return (sortOrder === 'descend') ? -result : result;
|
|
|
|
}
|
2016-08-23 18:00:01 +08:00
|
|
|
return 0;
|
2016-06-19 10:56:42 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
toggleSortOrder(order: string, column: ColumnProps<T>) {
|
2016-06-19 10:56:42 +08:00
|
|
|
let { sortColumn, sortOrder } = this.state;
|
|
|
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (!isSortColumn) { // 当前列未排序
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn = column;
|
|
|
|
} else { // 当前列已排序
|
|
|
|
if (sortOrder === order) { // 切换为未排序状态
|
|
|
|
sortOrder = '';
|
|
|
|
sortColumn = null;
|
|
|
|
} else { // 切换为排序状态
|
|
|
|
sortOrder = order;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newState = {
|
|
|
|
sortOrder,
|
|
|
|
sortColumn,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Controlled
|
|
|
|
if (this.getSortOrderColumns().length === 0) {
|
|
|
|
this.setState(newState);
|
|
|
|
}
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
|
|
|
...newState,
|
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handleFilter = (column: ColumnProps<T>, nextFilters: string[]) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const props = this.props;
|
2017-07-03 16:57:11 +08:00
|
|
|
let pagination = { ...this.state.pagination };
|
|
|
|
const filters = {
|
|
|
|
...this.state.filters,
|
2017-11-21 13:48:37 +08:00
|
|
|
[this.getColumnKey(column) as string]: nextFilters,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
// Remove filters not in current columns
|
2016-12-03 15:13:10 +08:00
|
|
|
const currentColumnKeys: string[] = [];
|
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (!c.children) {
|
2017-11-21 13:48:37 +08:00
|
|
|
currentColumnKeys.push(this.getColumnKey(c) as string);
|
2016-12-03 15:13:10 +08:00
|
|
|
}
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
Object.keys(filters).forEach((columnKey) => {
|
|
|
|
if (currentColumnKeys.indexOf(columnKey) < 0) {
|
|
|
|
delete filters[columnKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (props.pagination) {
|
|
|
|
// Reset current prop
|
|
|
|
pagination.current = 1;
|
2017-11-21 13:48:37 +08:00
|
|
|
pagination.onChange!(pagination.current);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const newState = {
|
|
|
|
pagination,
|
2016-09-09 13:55:41 +08:00
|
|
|
filters: {},
|
2016-06-19 10:56:42 +08:00
|
|
|
};
|
2017-07-03 16:57:11 +08:00
|
|
|
const filtersToSetState = { ...filters };
|
2016-06-19 10:56:42 +08:00
|
|
|
// Remove filters which is controlled
|
2017-11-21 13:48:37 +08:00
|
|
|
this.getFilteredValueColumns().forEach((col: ColumnProps<T>) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const columnKey = this.getColumnKey(col);
|
|
|
|
if (columnKey) {
|
|
|
|
delete filtersToSetState[columnKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (Object.keys(filtersToSetState).length > 0) {
|
|
|
|
newState.filters = filtersToSetState;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controlled current prop will not respond user interaction
|
2017-01-09 20:49:00 +08:00
|
|
|
if (typeof props.pagination === 'object' && 'current' in (props.pagination as Object)) {
|
2017-07-03 16:57:11 +08:00
|
|
|
newState.pagination = {
|
|
|
|
...pagination,
|
2016-06-19 10:56:42 +08:00
|
|
|
current: this.state.pagination.current,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(newState, () => {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
selectionDirty: false,
|
|
|
|
filters,
|
|
|
|
pagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handleSelect = (record: T, rowIndex: number, e: React.ChangeEvent<HTMLInputElement>) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const checked = e.target.checked;
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2016-06-19 10:56:42 +08:00
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
if (checked) {
|
|
|
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
|
|
|
} else {
|
2017-11-21 13:48:37 +08:00
|
|
|
selectedRowKeys = selectedRowKeys.filter((i: string) => key !== i);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handleRadioSelect = (record: T, rowIndex: number, e: React.ChangeEvent<HTMLInputElement>) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const checked = e.target.checked;
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2016-06-19 10:56:42 +08:00
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
selectedRowKeys = [key];
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handleSelectRow = (selectionKey: string, index: number, onSelectFunc: SelectionItemSelectFn) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const data = this.getFlatCurrentPageData();
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2017-02-23 19:29:47 +08:00
|
|
|
const changeableRowKeys = data
|
2016-12-16 17:53:08 +08:00
|
|
|
.filter((item, i) => !this.getCheckboxPropsByItem(item, i).disabled)
|
2016-06-19 10:56:42 +08:00
|
|
|
.map((item, i) => this.getRecordKey(item, i));
|
|
|
|
|
2017-02-23 19:29:47 +08:00
|
|
|
let changeRowKeys: string[] = [];
|
|
|
|
let selectWay = '';
|
|
|
|
let checked;
|
|
|
|
// handle default selection
|
|
|
|
switch (selectionKey) {
|
|
|
|
case 'all':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
|
|
|
selectedRowKeys.push(key);
|
|
|
|
changeRowKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = true;
|
|
|
|
break;
|
|
|
|
case 'removeAll':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) >= 0) {
|
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
|
|
|
changeRowKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = false;
|
|
|
|
break;
|
|
|
|
case 'invert':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
|
|
|
selectedRowKeys.push(key);
|
2017-10-21 20:06:12 +08:00
|
|
|
} else {
|
2017-02-23 19:29:47 +08:00
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
changeRowKeys.push(key);
|
2017-02-23 19:29:47 +08:00
|
|
|
selectWay = 'onSelectInvert';
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2017-02-23 19:29:47 +08:00
|
|
|
// when select custom selection, callback selections[n].onSelect
|
2017-09-19 09:56:19 +08:00
|
|
|
const { rowSelection } = this.props;
|
|
|
|
let customSelectionStartIndex = 2;
|
|
|
|
if (rowSelection && rowSelection.hideDefaultSelections) {
|
|
|
|
customSelectionStartIndex = 0;
|
|
|
|
}
|
|
|
|
if (index >= customSelectionStartIndex && typeof onSelectFunc === 'function') {
|
2017-02-23 19:29:47 +08:00
|
|
|
return onSelectFunc(changeableRowKeys);
|
|
|
|
}
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
2017-02-23 19:29:47 +08:00
|
|
|
selectWay: selectWay,
|
2016-08-03 21:08:13 +08:00
|
|
|
checked,
|
|
|
|
changeRowKeys,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handlePageChange = (current: number, ...otherArguments: any[]) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const props = this.props;
|
2017-07-03 16:57:11 +08:00
|
|
|
let pagination = { ...this.state.pagination };
|
2016-06-19 10:56:42 +08:00
|
|
|
if (current) {
|
|
|
|
pagination.current = current;
|
|
|
|
} else {
|
|
|
|
pagination.current = pagination.current || 1;
|
|
|
|
}
|
2017-11-21 13:48:37 +08:00
|
|
|
pagination.onChange!(pagination.current, ...otherArguments);
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
const newState = {
|
|
|
|
pagination,
|
|
|
|
};
|
|
|
|
// Controlled current prop will not respond user interaction
|
2017-03-02 11:30:16 +08:00
|
|
|
if (props.pagination &&
|
|
|
|
typeof props.pagination === 'object' &&
|
|
|
|
'current' in (props.pagination as Object)) {
|
2017-07-03 16:57:11 +08:00
|
|
|
newState.pagination = {
|
|
|
|
...pagination,
|
2016-06-19 10:56:42 +08:00
|
|
|
current: this.state.pagination.current,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
this.setState(newState);
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
selectionDirty: false,
|
|
|
|
pagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
renderSelectionBox = (type: RowSelectionType | undefined) => {
|
|
|
|
return (_: any, record: T, index: number) => {
|
2016-11-11 15:26:51 +08:00
|
|
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
2016-12-16 17:53:08 +08:00
|
|
|
const props = this.getCheckboxPropsByItem(record, index);
|
2017-11-21 13:48:37 +08:00
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2016-11-11 15:26:51 +08:00
|
|
|
type === 'radio' ? this.handleRadioSelect(record, rowIndex, e) :
|
|
|
|
this.handleSelect(record, rowIndex, e);
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
return (
|
|
|
|
<span onClick={stopPropagation}>
|
|
|
|
<SelectionBox
|
|
|
|
type={type}
|
|
|
|
store={this.store}
|
|
|
|
rowIndex={rowIndex}
|
|
|
|
disabled={props.disabled}
|
|
|
|
onChange={handleChange}
|
|
|
|
defaultSelection={this.getDefaultSelection()}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getRecordKey = (record: T, index: number) => {
|
2016-08-11 15:38:03 +08:00
|
|
|
const rowKey = this.props.rowKey;
|
2016-12-16 17:53:08 +08:00
|
|
|
const recordKey = (typeof rowKey === 'function') ?
|
2017-11-21 13:48:37 +08:00
|
|
|
rowKey(record, index) : (record as any)[rowKey as string];
|
2016-09-27 13:41:57 +08:00
|
|
|
warning(recordKey !== undefined,
|
2017-03-05 18:35:49 +08:00
|
|
|
'Each record in dataSource of table should have a unique `key` prop, or set `rowKey` to an unique primary key,' +
|
2017-07-19 13:59:53 +08:00
|
|
|
'see https://u.ant.design/table-row-key',
|
2016-09-27 13:41:57 +08:00
|
|
|
);
|
2016-12-30 13:50:11 +08:00
|
|
|
return recordKey === undefined ? index : recordKey;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer = () => {
|
2017-11-17 14:38:54 +08:00
|
|
|
return ReactDOM.findDOMNode(this) as HTMLElement;
|
2017-05-24 12:17:40 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
renderRowSelection(locale: TableLocale) {
|
2016-10-24 16:30:38 +08:00
|
|
|
const { prefixCls, rowSelection } = this.props;
|
2016-11-22 10:11:12 +08:00
|
|
|
const columns = this.columns.concat();
|
2016-10-24 16:30:38 +08:00
|
|
|
if (rowSelection) {
|
2016-12-16 17:53:08 +08:00
|
|
|
const data = this.getFlatCurrentPageData().filter((item, index) => {
|
2016-10-24 16:30:38 +08:00
|
|
|
if (rowSelection.getCheckboxProps) {
|
2016-12-16 17:53:08 +08:00
|
|
|
return !this.getCheckboxPropsByItem(item, index).disabled;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2017-03-24 11:44:36 +08:00
|
|
|
let selectionColumnClass = classNames(`${prefixCls}-selection-column`, {
|
|
|
|
[`${prefixCls}-selection-column-custom`]: rowSelection.selections,
|
|
|
|
});
|
2016-11-22 10:11:12 +08:00
|
|
|
const selectionColumn: ColumnProps<any> = {
|
2016-11-11 15:26:51 +08:00
|
|
|
key: 'selection-column',
|
|
|
|
render: this.renderSelectionBox(rowSelection.type),
|
2017-03-24 11:44:36 +08:00
|
|
|
className: selectionColumnClass,
|
2017-11-18 23:46:24 +08:00
|
|
|
fixed: rowSelection.fixed,
|
2016-11-11 15:26:51 +08:00
|
|
|
};
|
|
|
|
if (rowSelection.type !== 'radio') {
|
2016-12-16 17:53:08 +08:00
|
|
|
const checkboxAllDisabled = data.every((item, index) => this.getCheckboxPropsByItem(item, index).disabled);
|
2016-11-11 15:26:51 +08:00
|
|
|
selectionColumn.title = (
|
|
|
|
<SelectionCheckboxAll
|
|
|
|
store={this.store}
|
2017-10-20 14:54:38 +08:00
|
|
|
locale={locale}
|
2016-11-11 15:26:51 +08:00
|
|
|
data={data}
|
|
|
|
getCheckboxPropsByItem={this.getCheckboxPropsByItem}
|
|
|
|
getRecordKey={this.getRecordKey}
|
2016-06-19 10:56:42 +08:00
|
|
|
disabled={checkboxAllDisabled}
|
2017-02-23 19:29:47 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
onSelect={this.handleSelectRow}
|
2017-03-24 11:44:36 +08:00
|
|
|
selections={rowSelection.selections}
|
2017-08-27 15:44:22 +08:00
|
|
|
hideDefaultSelections={rowSelection.hideDefaultSelections}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={this.getPopupContainer}
|
2016-06-19 10:56:42 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2017-11-18 23:46:24 +08:00
|
|
|
if ('fixed' in rowSelection) {
|
|
|
|
selectionColumn.fixed = rowSelection.fixed;
|
|
|
|
} else if (columns.some(column => column.fixed === 'left' || column.fixed === true)) {
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionColumn.fixed = 'left';
|
|
|
|
}
|
|
|
|
if (columns[0] && columns[0].key === 'selection-column') {
|
|
|
|
columns[0] = selectionColumn;
|
|
|
|
} else {
|
|
|
|
columns.unshift(selectionColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getColumnKey(column: ColumnProps<T>, index?: number) {
|
2016-06-19 10:56:42 +08:00
|
|
|
return column.key || column.dataIndex || index;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getMaxCurrent(total: number) {
|
2016-07-12 18:45:45 +08:00
|
|
|
const { current, pageSize } = this.state.pagination;
|
2017-11-21 13:48:37 +08:00
|
|
|
if ((current! - 1) * pageSize! >= total) {
|
|
|
|
return Math.floor((total - 1) / pageSize!) + 1;
|
2016-07-12 18:45:45 +08:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
isSortColumn(column: ColumnProps<T>) {
|
2016-06-19 10:56:42 +08:00
|
|
|
const { sortColumn } = this.state;
|
|
|
|
if (!column || !sortColumn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
renderColumnsDropdown(columns: ColumnProps<T>[], locale: TableLocale) {
|
2016-09-14 16:18:33 +08:00
|
|
|
const { prefixCls, dropdownPrefixCls } = this.props;
|
2016-06-19 10:56:42 +08:00
|
|
|
const { sortOrder } = this.state;
|
2016-09-09 13:55:41 +08:00
|
|
|
return treeMap(columns, (originColumn, i) => {
|
2017-07-03 16:57:11 +08:00
|
|
|
let column = { ...originColumn };
|
2017-11-21 13:48:37 +08:00
|
|
|
let key = this.getColumnKey(column, i) as string;
|
2016-06-19 10:56:42 +08:00
|
|
|
let filterDropdown;
|
|
|
|
let sortButton;
|
2016-06-20 16:36:37 +08:00
|
|
|
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
|
2016-06-19 10:56:42 +08:00
|
|
|
let colFilters = this.state.filters[key] || [];
|
|
|
|
filterDropdown = (
|
2016-06-20 16:25:35 +08:00
|
|
|
<FilterDropdown
|
|
|
|
locale={locale}
|
|
|
|
column={column}
|
2016-06-19 10:56:42 +08:00
|
|
|
selectedKeys={colFilters}
|
|
|
|
confirmFilter={this.handleFilter}
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls={`${prefixCls}-filter`}
|
2016-10-24 16:30:38 +08:00
|
|
|
dropdownPrefixCls={dropdownPrefixCls || 'ant-dropdown'}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={this.getPopupContainer}
|
2016-06-19 10:56:42 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (column.sorter) {
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (isSortColumn) {
|
2017-11-09 19:30:24 +08:00
|
|
|
column.className = classNames(column.className, {
|
|
|
|
[`${prefixCls}-column-sort`]: sortOrder,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
const isAscend = isSortColumn && sortOrder === 'ascend';
|
|
|
|
const isDescend = isSortColumn && sortOrder === 'descend';
|
|
|
|
sortButton = (
|
2016-09-14 16:18:33 +08:00
|
|
|
<div className={`${prefixCls}-column-sorter`}>
|
2016-11-23 17:53:10 +08:00
|
|
|
<span
|
|
|
|
className={`${prefixCls}-column-sorter-up ${isAscend ? 'on' : 'off'}`}
|
2016-06-19 10:56:42 +08:00
|
|
|
title="↑"
|
|
|
|
onClick={() => this.toggleSortOrder('ascend', column)}
|
|
|
|
>
|
|
|
|
<Icon type="caret-up" />
|
|
|
|
</span>
|
2016-11-23 17:53:10 +08:00
|
|
|
<span
|
|
|
|
className={`${prefixCls}-column-sorter-down ${isDescend ? 'on' : 'off'}`}
|
2016-06-19 10:56:42 +08:00
|
|
|
title="↓"
|
|
|
|
onClick={() => this.toggleSortOrder('descend', column)}
|
|
|
|
>
|
|
|
|
<Icon type="caret-down" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
column.title = (
|
|
|
|
<span>
|
|
|
|
{column.title}
|
|
|
|
{sortButton}
|
|
|
|
{filterDropdown}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
return column;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
handleShowSizeChange = (current: number, pageSize: number) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const pagination = this.state.pagination;
|
2017-11-21 13:48:37 +08:00
|
|
|
pagination.onShowSizeChange!(current, pageSize);
|
2017-07-03 16:57:11 +08:00
|
|
|
const nextPagination = {
|
|
|
|
...pagination,
|
|
|
|
pageSize,
|
|
|
|
current,
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
this.setState({ pagination: nextPagination });
|
2016-10-24 16:30:38 +08:00
|
|
|
|
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
pagination: nextPagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderPagination() {
|
|
|
|
// 强制不需要分页
|
|
|
|
if (!this.hasPagination()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let size = 'default';
|
2016-07-12 18:24:16 +08:00
|
|
|
const { pagination } = this.state;
|
|
|
|
if (pagination.size) {
|
|
|
|
size = pagination.size;
|
2016-10-18 11:55:00 +08:00
|
|
|
} else if (this.props.size as string === 'middle' || this.props.size === 'small') {
|
2016-06-19 10:56:42 +08:00
|
|
|
size = 'small';
|
|
|
|
}
|
2016-07-12 18:24:16 +08:00
|
|
|
let total = pagination.total || this.getLocalData().length;
|
2017-03-23 21:15:49 +08:00
|
|
|
return (total > 0) ? (
|
2016-07-12 18:24:16 +08:00
|
|
|
<Pagination
|
2017-02-16 15:36:34 +08:00
|
|
|
key="pagination"
|
2016-07-12 18:24:16 +08:00
|
|
|
{...pagination}
|
2017-06-12 11:35:29 +08:00
|
|
|
className={classNames(pagination.className, `${this.props.prefixCls}-pagination`)}
|
2016-06-19 10:56:42 +08:00
|
|
|
onChange={this.handlePageChange}
|
|
|
|
total={total}
|
|
|
|
size={size}
|
2016-07-12 18:45:45 +08:00
|
|
|
current={this.getMaxCurrent(total)}
|
2016-06-19 10:56:42 +08:00
|
|
|
onShowSizeChange={this.handleShowSizeChange}
|
2017-03-23 21:15:49 +08:00
|
|
|
/>
|
|
|
|
) : null;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 13:55:36 +08:00
|
|
|
// Get pagination, filters, sorter
|
2016-08-11 15:38:03 +08:00
|
|
|
prepareParamsArguments(state: any): [any, string[], Object] {
|
2017-02-19 13:55:36 +08:00
|
|
|
const pagination = { ...state.pagination };
|
|
|
|
// remove useless handle function in Table.onChange
|
|
|
|
delete pagination.onChange;
|
|
|
|
delete pagination.onShowSizeChange;
|
2016-06-19 10:56:42 +08:00
|
|
|
const filters = state.filters;
|
2016-08-11 15:38:03 +08:00
|
|
|
const sorter: any = {};
|
2016-06-19 10:56:42 +08:00
|
|
|
if (state.sortColumn && state.sortOrder) {
|
|
|
|
sorter.column = state.sortColumn;
|
|
|
|
sorter.order = state.sortOrder;
|
|
|
|
sorter.field = state.sortColumn.dataIndex;
|
|
|
|
sorter.columnKey = this.getColumnKey(state.sortColumn);
|
|
|
|
}
|
|
|
|
return [pagination, filters, sorter];
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
findColumn(myKey: string | number) {
|
2016-12-03 15:13:10 +08:00
|
|
|
let column;
|
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (this.getColumnKey(c) === myKey) {
|
|
|
|
column = c;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return column;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentPageData() {
|
|
|
|
let data = this.getLocalData();
|
2017-11-21 13:48:37 +08:00
|
|
|
let current: number;
|
|
|
|
let pageSize: number;
|
2016-06-19 10:56:42 +08:00
|
|
|
let state = this.state;
|
|
|
|
// 如果没有分页的话,默认全部展示
|
|
|
|
if (!this.hasPagination()) {
|
|
|
|
pageSize = Number.MAX_VALUE;
|
|
|
|
current = 1;
|
|
|
|
} else {
|
2017-11-21 13:48:37 +08:00
|
|
|
pageSize = state.pagination.pageSize as number;
|
|
|
|
current = this.getMaxCurrent(state.pagination.total || data.length) as number;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2016-07-12 18:45:45 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
// 分页
|
|
|
|
// ---
|
|
|
|
// 当数据量少于等于每页数量时,直接设置数据
|
|
|
|
// 否则进行读取分页数据
|
|
|
|
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
|
2016-10-20 22:10:46 +08:00
|
|
|
data = data.filter((_, i) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
return i >= (current - 1) * pageSize && i < current * pageSize;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:27:45 +08:00
|
|
|
getFlatData() {
|
|
|
|
return flatArray(this.getLocalData());
|
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
getFlatCurrentPageData() {
|
|
|
|
return flatArray(this.getCurrentPageData());
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
recursiveSort(data: T[], sorterFn: (a: any, b: any) => number): T[] {
|
2016-10-24 16:30:38 +08:00
|
|
|
const { childrenColumnName = 'children' } = this.props;
|
2017-11-21 13:48:37 +08:00
|
|
|
return data.sort(sorterFn).map((item: any) => (item[childrenColumnName] ? {
|
2017-07-03 16:57:11 +08:00
|
|
|
...item,
|
2017-10-08 17:58:42 +08:00
|
|
|
[childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn),
|
|
|
|
} : item));
|
2016-08-26 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
getLocalData() {
|
|
|
|
const state = this.state;
|
2016-08-26 15:04:19 +08:00
|
|
|
const { dataSource } = this.props;
|
2016-08-25 21:12:23 +08:00
|
|
|
let data = dataSource || [];
|
2016-06-19 10:56:42 +08:00
|
|
|
// 优化本地排序
|
|
|
|
data = data.slice(0);
|
|
|
|
const sorterFn = this.getSorterFn();
|
|
|
|
if (sorterFn) {
|
2016-08-26 15:04:19 +08:00
|
|
|
data = this.recursiveSort(data, sorterFn);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
// 筛选
|
|
|
|
if (state.filters) {
|
|
|
|
Object.keys(state.filters).forEach((columnKey) => {
|
2016-12-03 15:51:44 +08:00
|
|
|
let col = this.findColumn(columnKey) as any;
|
2016-06-19 10:56:42 +08:00
|
|
|
if (!col) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let values = state.filters[columnKey] || [];
|
|
|
|
if (values.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 16:30:38 +08:00
|
|
|
const onFilter = col.onFilter;
|
|
|
|
data = onFilter ? data.filter(record => {
|
|
|
|
return values.some(v => onFilter(v, record));
|
2016-06-19 10:56:42 +08:00
|
|
|
}) : data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-11-18 00:03:30 +08:00
|
|
|
createComponents(components: TableComponents = {}, prevComponents?: TableComponents) {
|
|
|
|
const bodyRow = components && components.body && components.body.row;
|
|
|
|
const preBodyRow = prevComponents && prevComponents.body && prevComponents.body.row;
|
2017-11-22 17:25:14 +08:00
|
|
|
if (!this.components || bodyRow !== preBodyRow) {
|
|
|
|
this.components = { ...components };
|
2017-11-18 00:03:30 +08:00
|
|
|
this.components.body = {
|
|
|
|
...components.body,
|
2017-11-18 00:36:22 +08:00
|
|
|
row: createBodyRow(bodyRow),
|
2017-11-18 00:03:30 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
renderTable = (contextLocale: TableLocale) => {
|
2017-10-20 14:54:38 +08:00
|
|
|
const locale = { ...contextLocale, ...this.props.locale };
|
2016-12-27 18:24:30 +08:00
|
|
|
const { style, className, prefixCls, showHeader, ...restProps } = this.props;
|
2016-08-30 17:01:51 +08:00
|
|
|
const data = this.getCurrentPageData();
|
2016-06-19 10:56:42 +08:00
|
|
|
const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
|
|
|
|
|
|
|
|
const classString = classNames({
|
2016-09-14 16:18:33 +08:00
|
|
|
[`${prefixCls}-${this.props.size}`]: true,
|
|
|
|
[`${prefixCls}-bordered`]: this.props.bordered,
|
|
|
|
[`${prefixCls}-empty`]: !data.length,
|
2016-12-27 18:24:30 +08:00
|
|
|
[`${prefixCls}-without-column-header`]: !showHeader,
|
2016-06-19 10:56:42 +08:00
|
|
|
});
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
let columns = this.renderRowSelection(locale);
|
|
|
|
columns = this.renderColumnsDropdown(columns, locale);
|
2016-06-19 10:56:42 +08:00
|
|
|
columns = columns.map((column, i) => {
|
2017-07-03 16:57:11 +08:00
|
|
|
const newColumn = { ...column };
|
2016-06-19 10:56:42 +08:00
|
|
|
newColumn.key = this.getColumnKey(newColumn, i);
|
|
|
|
return newColumn;
|
|
|
|
});
|
2016-09-20 13:16:58 +08:00
|
|
|
let expandIconColumnIndex = (columns[0] && columns[0].key === 'selection-column') ? 1 : 0;
|
|
|
|
if ('expandIconColumnIndex' in restProps) {
|
2016-12-19 15:19:15 +08:00
|
|
|
expandIconColumnIndex = restProps.expandIconColumnIndex as number;
|
2016-09-20 13:16:58 +08:00
|
|
|
}
|
2017-11-18 00:03:30 +08:00
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2016-09-20 13:16:58 +08:00
|
|
|
<RcTable
|
2017-02-16 15:36:34 +08:00
|
|
|
key="table"
|
2016-09-20 13:16:58 +08:00
|
|
|
{...restProps}
|
2017-11-18 00:03:30 +08:00
|
|
|
onRow={this.onRow}
|
|
|
|
components={this.components}
|
2016-09-14 16:59:45 +08:00
|
|
|
prefixCls={prefixCls}
|
2016-08-14 15:38:43 +08:00
|
|
|
data={data}
|
|
|
|
columns={columns}
|
2016-12-27 18:24:30 +08:00
|
|
|
showHeader={showHeader}
|
2016-08-14 15:38:43 +08:00
|
|
|
className={classString}
|
2016-09-20 13:16:58 +08:00
|
|
|
expandIconColumnIndex={expandIconColumnIndex}
|
2016-08-14 15:38:43 +08:00
|
|
|
expandIconAsCell={expandIconAsCell}
|
2016-08-30 17:01:51 +08:00
|
|
|
emptyText={() => locale.emptyText}
|
2016-08-14 15:38:43 +08:00
|
|
|
/>
|
2016-06-19 10:56:42 +08:00
|
|
|
);
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { style, className, prefixCls } = this.props;
|
|
|
|
const data = this.getCurrentPageData();
|
|
|
|
|
|
|
|
const table = (
|
|
|
|
<LocaleReceiver
|
|
|
|
componentName="Table"
|
2017-10-26 10:50:18 +08:00
|
|
|
defaultLocale={defaultLocale.Table}
|
2017-10-20 14:54:38 +08:00
|
|
|
>
|
|
|
|
{this.renderTable}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
2017-02-16 15:36:34 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
// if there is no pagination or no data,
|
|
|
|
// the height of spin should decrease by half of pagination
|
|
|
|
const paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
|
2017-02-16 15:36:34 +08:00
|
|
|
? `${prefixCls}-with-pagination` : `${prefixCls}-without-pagination`;
|
|
|
|
|
2017-02-13 15:48:08 +08:00
|
|
|
let loading = this.props.loading;
|
2017-02-20 12:18:41 +08:00
|
|
|
if (typeof loading === 'boolean') {
|
2017-02-13 15:48:08 +08:00
|
|
|
loading = {
|
|
|
|
spinning: loading,
|
|
|
|
};
|
|
|
|
}
|
2017-02-20 12:18:41 +08:00
|
|
|
|
2017-02-16 15:36:34 +08:00
|
|
|
return (
|
2017-05-24 12:17:40 +08:00
|
|
|
<div
|
|
|
|
className={classNames(`${prefixCls}-wrapper`, className)}
|
|
|
|
style={style}
|
|
|
|
>
|
2017-02-18 14:58:47 +08:00
|
|
|
<Spin
|
2017-02-20 12:18:41 +08:00
|
|
|
{...loading}
|
2017-02-18 14:58:47 +08:00
|
|
|
className={loading ? `${paginationPatchClass} ${prefixCls}-spin-holder` : ''}
|
|
|
|
>
|
|
|
|
{table}
|
|
|
|
{this.renderPagination()}
|
|
|
|
</Spin>
|
2016-06-19 10:56:42 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|