Skip to content

Commit

Permalink
Revert "Add support for table default sort order" (ant-design#7414)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck authored Sep 1, 2017
1 parent bac4d04 commit 1547b88
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 69 deletions.
33 changes: 6 additions & 27 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ export interface TableRowSelection<T> {
hideDefaultSelections?: boolean;
}

export interface DefaultColumnSortOrder {
columnTitle: string;
sortOrder: 'ascend' | 'descend';
}

export interface TableProps<T> {
prefixCls?: string;
dropdownPrefixCls?: string;
Expand All @@ -76,7 +71,6 @@ export interface TableProps<T> {
rowClassName?: (record: T, index: number) => string;
expandedRowRender?: any;
defaultExpandedRowKeys?: string[] | number[];
defaultSortOrder?: DefaultColumnSortOrder;
expandedRowKeys?: string[] | number[];
expandIconAsCell?: boolean;
expandIconColumnIndex?: number;
Expand Down Expand Up @@ -163,7 +157,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
this.columns = props.columns || normalizeColumns(props.children);

this.state = {
...this.getDefaultSortOrder(this.columns),
...this.getSortStateFromColumns(),
// 减少状态
filters: this.getFiltersFromColumns(),
pagination: this.getDefaultPagination(props),
Expand Down Expand Up @@ -339,32 +333,16 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
return filters;
}

getDefaultSortOrder(columns?) {
const definedSortState = this.getSortStateFromColumns(columns);

if (this.props.defaultSortOrder && !definedSortState.sortColumn) {
let columnTitle = this.props.defaultSortOrder.columnTitle;
return {
sortColumn: flatFilter(columns || this.columns || [], column => column.title === columnTitle)[0],
sortOrder: this.props.defaultSortOrder.sortOrder,
};
}

return definedSortState;
}

getSortStateFromColumns(columns?) {
// return first column which sortOrder is not falsy
// return fisrt column which sortOrder is not falsy
const sortedColumn =
this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];

if (sortedColumn) {
return {
sortColumn: sortedColumn,
sortOrder: sortedColumn.sortOrder,
};
}

return {
sortColumn: null,
sortOrder: null,
Expand Down Expand Up @@ -755,9 +733,10 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
if (column.sorter) {
let isSortColumn = this.isSortColumn(column);
if (isSortColumn) {
column.className = classNames(column.className, {
[`${prefixCls}-column-sort`]: sortOrder,
});
column.className = column.className || '';
if (sortOrder) {
column.className += ` ${prefixCls}-column-sort`;
}
}
const isAscend = isSortColumn && sortOrder === 'ascend';
const isDescend = isSortColumn && sortOrder === 'descend';
Expand Down
22 changes: 0 additions & 22 deletions components/table/__tests__/Table.sorter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,6 @@ describe('Table.sorter', () => {
expect(wrapper.find('thead')).toMatchSnapshot();
});

it('default sort order ascend', () => {
const wrapper = mount(createTable({
defaultSortOrder: {
columnTitle: 'Name',
sortOrder: 'ascend',
},
}));

expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
});

it('default sort order descend', () => {
const wrapper = mount(createTable({
defaultSortOrder: {
columnTitle: 'Name',
sortOrder: 'descend',
},
}));

expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
});

it('sort records', () => {
const wrapper = mount(createTable());

Expand Down
24 changes: 12 additions & 12 deletions components/table/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7867,7 +7867,7 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
</span>
</th>
<th
class="ant-table-column-sort"
class=""
>
<span>
Age
Expand All @@ -7883,7 +7883,7 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
/>
</span>
<span
class="ant-table-column-sorter-down on"
class="ant-table-column-sorter-down off"
title=""
>
<i
Expand Down Expand Up @@ -7939,17 +7939,17 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
class="ant-table-row-indent indent-level-0"
style="padding-left:0px;"
/>
Jim Green
John Brown
</td>
<td
class="ant-table-column-sort"
class=""
>
42
32
</td>
<td
class=""
>
London No. 1 Lake Park
New York No. 1 Lake Park
</td>
</tr>
<tr
Expand All @@ -7962,17 +7962,17 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
class="ant-table-row-indent indent-level-0"
style="padding-left:0px;"
/>
John Brown
Jim Green
</td>
<td
class="ant-table-column-sort"
class=""
>
32
42
</td>
<td
class=""
>
New York No. 1 Lake Park
London No. 1 Lake Park
</td>
</tr>
<tr
Expand All @@ -7988,7 +7988,7 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
Joe Black
</td>
<td
class="ant-table-column-sort"
class=""
>
32
</td>
Expand All @@ -8011,7 +8011,7 @@ exports[`renders ./components/table/demo/head.md correctly 1`] = `
Jim Red
</td>
<td
class="ant-table-column-sort"
class=""
>
32
</td>
Expand Down
7 changes: 1 addition & 6 deletions components/table/demo/head.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,5 @@ function onChange(pagination, filters, sorter) {
console.log('params', pagination, filters, sorter);
}

ReactDOM.render(<Table
columns={columns}
dataSource={data}
onChange={onChange}
defaultSortOrder={{ columnTitle: 'Age', sortOrder: 'descend' }}
/>, mountNode);
ReactDOM.render(<Table columns={columns} dataSource={data} onChange={onChange} />, mountNode);
````
3 changes: 1 addition & 2 deletions components/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ const columns = [{
| rowKey | get row's key, could be a string or function | string\|Function(record):string | 'key' |
| rowClassName | get row's className | Function(record, index):string | - |
| expandedRowRender | expanded container render for each row | Function | - |
| defaultSortOrder | default column sorting | Object({columnTitle, sortOrder}) | - |
| defaultExpandedRowKeys | initial expanded row keys | | - |
| defaultExpandedRowKeys | initial expanded row keys | string[] | - |
| expandedRowKeys | current expanded rows keys | string[] | - |
| defaultExpandAllRows | expand all rows initially | boolean | false |
| onExpandedRowsChange | function to call when the expanded rows change | Function(expandedRows) | |
Expand Down

0 comments on commit 1547b88

Please sign in to comment.