Skip to content

Commit

Permalink
move sorter logic from div to th cell (ant-design#13669)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Dec 17, 2018
1 parent 26936de commit 68ad468
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 23 additions & 2 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TableComponents,
RowSelectionType,
TableLocale,
AdditionalCellProps,
ColumnProps,
CompareFn,
TableStateFilters,
Expand Down Expand Up @@ -805,6 +806,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
const key = this.getColumnKey(column, i) as string;
let filterDropdown;
let sortButton;
let onHeaderCell = column.onHeaderCell;
const sortTitle = this.getColumnTitle(column.title, {}) || locale.sortTitle;
const isSortColumn = this.isSortColumn(column);
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
Expand Down Expand Up @@ -839,8 +841,27 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
/>
</div>
);

onHeaderCell = (col: Column<T>) => {
let colProps: AdditionalCellProps = {};
// Get original first
if (column.onHeaderCell) {
colProps = {
...column.onHeaderCell(col),
};
}
// Add sorter logic
const onHeaderCellClick = colProps.onClick;
colProps.onClick = (...args) => {
this.toggleSortOrder(column);
if (onHeaderCellClick) {
onHeaderCellClick(...args);
}
};
return colProps;
};
}
const sortTitleString = (sortButton && typeof sortTitle === 'string') ? sortTitle : undefined;
const sortTitleString = sortButton && typeof sortTitle === 'string' ? sortTitle : undefined;
return {
...column,
className: classNames(column.className, {
Expand All @@ -854,13 +875,13 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
key="title"
title={sortTitleString}
className={sortButton ? `${prefixCls}-column-sorters` : undefined}
onClick={() => this.toggleSortOrder(column)}
>
{this.renderColumnTitle(column.title)}
{sortButton}
</div>,
filterDropdown,
],
onHeaderCell,
};
});
}
Expand Down
5 changes: 5 additions & 0 deletions components/table/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface ColumnProps<T> {
onHeaderCell?: (props: ColumnProps<T>) => any;
}

export interface AdditionalCellProps {
onClick?: React.MouseEventHandler<HTMLElement>;
[name: string]: any;
}

export interface TableComponents {
table?: React.ReactType;
header?: {
Expand Down

0 comments on commit 68ad468

Please sign in to comment.