Skip to content

Commit

Permalink
feat: Allow user to configure the Tooltip in the Table header (ant-de…
Browse files Browse the repository at this point in the history
…sign#29002)

* feat: Table header supports tooltipPlacement

* docs: add Table tooltipPlacement

* feat: Allow user to configure the Tooltip in the Table header

* fix: fix jsx and use old code style

* fix: replace if blocks with ternary operator

* docs: fix url

Co-authored-by: 偏右 <[email protected]>

* docs: fix url

Co-authored-by: harrisoff <[email protected]>
Co-authored-by: 偏右 <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2021
1 parent 4cf059a commit 755669a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
10 changes: 8 additions & 2 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TableProps as RcTableProps, INTERNAL_HOOKS } from 'rc-table/lib/Table';
import { convertChildrenToColumns } from 'rc-table/lib/hooks/useColumns';
import Spin, { SpinProps } from '../spin';
import Pagination from '../pagination';
import { TooltipProps } from '../tooltip';
import { ConfigContext } from '../config-provider/context';
import usePagination, { DEFAULT_PAGE_SIZE, getPaginationParam } from './hooks/usePagination';
import useLazyKVMap from './hooks/useLazyKVMap';
Expand Down Expand Up @@ -95,7 +96,7 @@ export interface TableProps<RecordType>
scrollToFirstRowOnChange?: boolean;
};
sortDirections?: SortOrder[];
showSorterTooltip?: boolean;
showSorterTooltip?: boolean | TooltipProps;
}

function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
Expand Down Expand Up @@ -236,7 +237,12 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
}
};

/** Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to read state out and then put it back to title render. Move these code into `hooks` but still too complex. We should provides Table props like `sorter` & `filter` to handle control in next big version. */
/**
* Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to
* read state out and then put it back to title render. Move these code into `hooks` but still
* too complex. We should provides Table props like `sorter` & `filter` to handle control in next
* big version.
*/

// ============================ Sorter =============================
const onSorterChange = (
Expand Down
36 changes: 36 additions & 0 deletions components/table/__tests__/Table.sorter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,42 @@ describe('Table.sorter', () => {
expect(wrapper.find('.ant-tooltip-open')).toHaveLength(0);
});

it('should show correct tooltip when showSorterTooltip is an object', () => {
// basically copied from 'hover header show sorter tooltip'
jest.useFakeTimers();
const wrapper = mount(
createTable({ showSorterTooltip: { placement: 'bottom', title: 'static title' } }),
);
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseenter');
jest.runAllTimers();
wrapper.update();
expect(wrapper.find('.ant-tooltip-open').length).toBeTruthy();
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseout');

wrapper.setProps({ showSorterTooltip: false });
expect(wrapper.find('.ant-table-column-sorters-with-tooltip')).toHaveLength(0);
jest.runAllTimers();
wrapper.update();
expect(wrapper.find('.ant-tooltip-open')).toHaveLength(0);
wrapper.setProps({
showSorterTooltip: false,
columns: [{ ...column, showSorterTooltip: true }],
});
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseenter');
jest.runAllTimers();
wrapper.update();
expect(wrapper.find('.ant-tooltip-open').length).toBeTruthy();
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseout');
wrapper.setProps({
showSorterTooltip: true,
columns: [{ ...column, showSorterTooltip: false }],
});
expect(wrapper.find('.ant-table-column-sorters-with-tooltip')).toHaveLength(0);
jest.runAllTimers();
wrapper.update();
expect(wrapper.find('.ant-tooltip-open')).toHaveLength(0);
});

it('works with grouping columns in controlled mode', () => {
const columns = [
{
Expand Down
9 changes: 5 additions & 4 deletions components/table/hooks/useSorter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ColumnGroupType,
TableLocale,
} from '../interface';
import Tooltip from '../../tooltip';
import Tooltip, { TooltipProps } from '../../tooltip';
import { getColumnKey, getColumnPos, renderColumnTitle } from '../util';

const ASCEND = 'ascend';
Expand Down Expand Up @@ -108,7 +108,7 @@ function injectSorter<RecordType>(
triggerSorter: (sorterSates: SortState<RecordType>) => void,
defaultSortDirections: SortOrder[],
tableLocale?: TableLocale,
tableShowSorterTooltip?: boolean,
tableShowSorterTooltip?: boolean | TooltipProps,
pos?: string,
): ColumnsType<RecordType> {
return (columns || []).map((column, index) => {
Expand Down Expand Up @@ -146,6 +146,7 @@ function injectSorter<RecordType>(
} else if (nextSortOrder === ASCEND) {
sortTip = triggerAsc;
}
const tooltipProps: TooltipProps = typeof showSorterTooltip === 'object' ? showSorterTooltip : { title: sortTip };
newColumn = {
...newColumn,
className: classNames(newColumn.className, { [`${prefixCls}-column-sort`]: sorterOrder }),
Expand All @@ -166,7 +167,7 @@ function injectSorter<RecordType>(
</div>
);
return showSorterTooltip ? (
<Tooltip title={sortTip}>
<Tooltip {...tooltipProps}>
<div className={`${prefixCls}-column-sorters-with-tooltip`}>{renderSortTitle}</div>
</Tooltip>
) : (
Expand Down Expand Up @@ -306,7 +307,7 @@ interface SorterConfig<RecordType> {
) => void;
sortDirections: SortOrder[];
tableLocale?: TableLocale;
showSorterTooltip?: boolean;
showSorterTooltip?: boolean | TooltipProps;
}

export default function useFilterSorter<RecordType>({
Expand Down
4 changes: 2 additions & 2 deletions components/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const columns = [
| rowSelection | Row selection [config](#rowSelection) | object | - | |
| scroll | Whether the table can be scrollable, [config](#scroll) | object | - | |
| showHeader | Whether to show table header | boolean | true | |
| showSorterTooltip | The header show next sorter direction tooltip | boolean | true | |
| showSorterTooltip | The header show next sorter direction tooltip. It will be set as the property of Tooltip if its type is object | boolean \| [Tooltip props](/components/tooltip/#API) | true | |
| size | Size of table | `default` \| `middle` \| `small` | `default` | |
| sortDirections | Supported sort way, could be `ascend`, `descend` | Array | \[`ascend`, `descend`] | |
| sticky | Set sticky header and scroll bar | boolean \| `{offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement}` | - | 4.6.0 (getContainer: 4.7.0) |
Expand Down Expand Up @@ -134,7 +134,7 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| render | Renderer of the table cell. The return value should be a ReactNode, or an object for [colSpan/rowSpan config](#components-table-demo-colspan-rowspan) | function(text, record, index) {} | - | | |
| responsive | The list of breakpoints at which to display this column. Always visible if not set. | [Breakpoint](https://github.com/ant-design/ant-design/blob/015109b42b85c63146371b4e32b883cf97b088e8/components/_util/responsiveObserve.ts#L1)\[] | - | 4.2.0 | |
| shouldCellUpdate | Control cell render logic | (record, prevRecord) => boolean | - | 4.3.0 | |
| showSorterTooltip | If header show next sorter direction tooltip, override `showSorterTooltip` in table | boolean | true | | |
| showSorterTooltip | If header show next sorter direction tooltip, override `showSorterTooltip` in table | boolean \| [Tooltip props](/components/tooltip/) | true | | |
| sortDirections | Supported sort way, override `sortDirections` in `Table`, could be `ascend`, `descend` | Array | \[`ascend`, `descend`] | | |
| sorter | Sort function for local sort, see [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)'s compareFunction. If you need sort buttons only, set to `true` | function \| boolean | - | | |
| sortOrder | Order of sorted values: `'ascend'` `'descend'` `false` | boolean \| string | - | | |
Expand Down
4 changes: 2 additions & 2 deletions components/table/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const columns = [
| rowSelection | 表格行是否可选择,[配置项](#rowSelection) | object | - | |
| scroll | 表格是否可滚动,也可以指定滚动区域的宽、高,[配置项](#scroll) | object | - | |
| showHeader | 是否显示表头 | boolean | true | |
| showSorterTooltip | 表头是否显示下一次排序的 tooltip 提示 | boolean | true | |
| showSorterTooltip | 表头是否显示下一次排序的 tooltip 提示。当参数类型为对象时,将被设置为 Tooltip 的属性 | boolean \| [Tooltip props](/components/tooltip/) | true | |
| size | 表格大小 | `default` \| `middle` \| `small` | default | |
| sortDirections | 支持的排序方式,取值为 `ascend` `descend` | Array | \[`ascend`, `descend`] | |
| sticky | 设置粘性头部和滚动条 | boolean \| `{offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement}` | - | 4.6.0 (getContainer: 4.7.0) |
Expand Down Expand Up @@ -141,7 +141,7 @@ const columns = [
| render | 生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引,@return 里面可以设置表格[行/列合并](#components-table-demo-colspan-rowspan) | function(text, record, index) {} | - | |
| responsive | 响应式 breakpoint 配置列表。未设置则始终可见。 | [Breakpoint](https://github.com/ant-design/ant-design/blob/015109b42b85c63146371b4e32b883cf97b088e8/components/_util/responsiveObserve.ts#L1)\[] | - | 4.2.0 |
| shouldCellUpdate | 自定义单元格渲染时机 | (record, prevRecord) => boolean | - | 4.3.0 |
| showSorterTooltip | 表头显示下一次排序的 tooltip 提示, 覆盖 table 中 `showSorterTooltip` | boolean | true | |
| showSorterTooltip | 表头显示下一次排序的 tooltip 提示, 覆盖 table 中 `showSorterTooltip` | boolean \| [Tooltip props](/components/tooltip/#API) | true | |
| sortDirections | 支持的排序方式,覆盖 `Table``sortDirections`, 取值为 `ascend` `descend` | Array | \[`ascend`, `descend`] | |
| sorter | 排序函数,本地排序使用一个函数(参考 [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) 的 compareFunction),需要服务端排序可设为 true | function \| boolean | - | |
| sortOrder | 排序的受控属性,外界可用此控制列的排序,可设置为 `ascend` `descend` false | boolean \| string | - | |
Expand Down
3 changes: 2 additions & 1 deletion components/table/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RenderedCell as RcRenderedCell,
ExpandableConfig,
} from 'rc-table/lib/interface';
import { TooltipProps } from '../tooltip';
import { CheckboxProps } from '../checkbox';
import { PaginationProps } from '../pagination';
import { Breakpoint } from '../_util/responsiveObserve';
Expand Down Expand Up @@ -95,7 +96,7 @@ export interface ColumnType<RecordType> extends RcColumnType<RecordType> {
sortOrder?: SortOrder;
defaultSortOrder?: SortOrder;
sortDirections?: SortOrder[];
showSorterTooltip?: boolean;
showSorterTooltip?: boolean | TooltipProps;

// Filter
filtered?: boolean;
Expand Down

0 comments on commit 755669a

Please sign in to comment.