Get the filtered data after executing the filter function on the dataSource (#12369)

* update testcase

* rename as currentDataSource
This commit is contained in:
zombieJ 2018-09-30 11:17:39 +08:00 committed by Wei Zhu
parent 2d8f9ccf63
commit 2cd0ca3225
6 changed files with 45 additions and 14 deletions

View File

@ -33,6 +33,7 @@ import {
TableSelectWay,
TableRowSelection,
PaginationConfig,
PrepareParamsArgumentsReturn,
} from './interface';
import { RadioChangeEvent } from '../radio';
import { CheckboxChangeEvent } from '../checkbox';
@ -340,8 +341,8 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
};
}
getSorterFn() {
const { sortOrder, sortColumn } = this.state;
getSorterFn(state: TableState<T>) {
const { sortOrder, sortColumn } = state || this.state;
if (!sortOrder || !sortColumn ||
typeof sortColumn.sorter !== 'function') {
return;
@ -887,7 +888,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}
// Get pagination, filters, sorter
prepareParamsArguments(state: any): [any, string[], Object] {
prepareParamsArguments(state: any): PrepareParamsArgumentsReturn<T> {
const pagination = { ...state.pagination };
// remove useless handle function in Table.onChange
delete pagination.onChange;
@ -900,7 +901,12 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
sorter.field = state.sortColumn.dataIndex;
sorter.columnKey = this.getColumnKey(state.sortColumn);
}
return [pagination, filters, sorter];
const extra = {
currentDataSource: this.getLocalData(state),
};
return [pagination, filters, sorter, extra];
}
findColumn(myKey: string | number) {
@ -955,24 +961,24 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
} : item));
}
getLocalData() {
const state = this.state;
getLocalData(state?: TableState<T>) {
const currentState: TableState<T> = state || this.state;
const { dataSource } = this.props;
let data = dataSource || [];
// 优化本地排序
data = data.slice(0);
const sorterFn = this.getSorterFn();
const sorterFn = this.getSorterFn(currentState);
if (sorterFn) {
data = this.recursiveSort(data, sorterFn);
}
// 筛选
if (state.filters) {
Object.keys(state.filters).forEach((columnKey) => {
if (currentState.filters) {
Object.keys(currentState.filters).forEach((columnKey) => {
let col = this.findColumn(columnKey) as any;
if (!col) {
return;
}
let values = state.filters[columnKey] || [];
let values = currentState.filters[columnKey] || [];
if (values.length === 0) {
return;
}

View File

@ -220,7 +220,14 @@ describe('Table.filter', () => {
dropdownWrapper.find('MenuItem').first().simulate('click');
dropdownWrapper.find('.confirm').simulate('click');
expect(handleChange).toBeCalledWith({}, { name: ['boy'] }, {});
expect(handleChange).toBeCalledWith(
{},
{ name: ['boy'] },
{},
{
currentDataSource: [],
}
);
});
it('should not fire change event on close filterDropdown without changing anything', () => {
@ -377,6 +384,7 @@ describe('Table.filter', () => {
wrapper.find('.ant-dropdown-trigger').first().simulate('click');
expect(handleChange).toBeCalled();
expect(handleChange.mock.calls[0][3].currentDataSource.length).toBe(1);
});
it('renders custom filter icon correctly', () => {

View File

@ -86,7 +86,15 @@ describe('Table.pagination', () => {
pageSize: 2,
},
{},
{}
{},
{
currentDataSource: [
{ key: 0, name: 'Jack' },
{ key: 1, name: 'Lucy' },
{ key: 2, name: 'Tom' },
{ key: 3, name: 'Jerry' },
],
},
);
expect(handlePaginationChange).toBeCalledWith(2, 2);

View File

@ -74,7 +74,7 @@ const columns = [{
| showHeader | Whether to show table header | boolean | `true` |
| size | Size of table | `default` \| `middle` \| `small` | `default` |
| title | Table title renderer | Function(currentPageData) | |
| onChange | Callback executed when pagination, filters or sorter is changed | Function(pagination, filters, sorter) | |
| onChange | Callback executed when pagination, filters or sorter is changed | Function(pagination, filters, sorter, extra: { currentDataSource: [] }) | |
| onExpand | Callback executed when the row expand icon is clicked | Function(expanded, record) | |
| onExpandedRowsChange | Callback executed when the expanded rows change | Function(expandedRows) | |
| onHeaderRow | Set props on per header row | Function(column, index) | - |

View File

@ -79,7 +79,7 @@ const columns = [{
| showHeader | 是否显示表头 | boolean | true |
| size | 正常或迷你类型,`default` or `small` | string | default |
| title | 表格标题 | Function(currentPageData) | |
| onChange | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter) | |
| onChange | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter, extra: { currentDataSource: [] }) | |
| onExpand | 点击展开图标时触发 | Function(expanded, record) | |
| onExpandedRowsChange | 展开的行变化时触发 | Function(expandedRows) | |
| onHeaderRow | 设置头部行属性 | Function(column, index) | - |

View File

@ -212,3 +212,12 @@ export interface FilterMenuState {
keyPathOfSelectedItem: { [key: string]: string };
visible?: boolean;
}
export type PrepareParamsArgumentsReturn<T> = [
any,
string[],
Object,
{
currentDataSource: T[],
}
];