Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ es/
.storybook
.doc
!tests/__mocks__/rc-util/lib
examples/debug.tsx
examples/debug.tsx
.history
71 changes: 71 additions & 0 deletions examples/ellipsis-custom-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import Tooltip from 'rc-tooltip';
import Table from '../src';
import '../assets/index.less';
import 'rc-tooltip/assets/bootstrap.css';

const createColumns = (length: number) => {
return Array.from({ length }, (_, i) => ({
title: 'description',
dataIndex: 'description',
key: `description ${i + 1}`,
ellipsis: {
showTitle: false,
},
...(i === 0 ? { width: 50 } : null),
render(description: string) {
return (
<Tooltip placement="topLeft" overlay={description}>
<span>{description}</span>
</Tooltip>
);
},
}));
};

const columns = [
{
title: 'name',
dataIndex: 'name',
width: 100,
ellipsis: {
showTitle: false,
},
render: (name: string) => (
<Tooltip placement="topLeft" overlay={name}>
<span>{name}</span>
</Tooltip>
),
},
...createColumns(10),
{
title: 'Operations',
key: 'operations',
ellipsis: {
showTitle: false,
},
render() {
return (
<Tooltip placement="topLeft" overlay="Operations">
<a href="#">Operations</a>
</Tooltip>
);
},
},
];

const data = [
{ name: 'jack', description: 'description description', key: '1' },
{ name: 'jackjackjackjackjackjack', description: 'description description', key: '2' },
{ name: 'jack ma', description: 'description description', key: '3' },
{ name: 'jack nickson', description: 'description description', key: '4' },
];

const Demo = () => (
<div>
<h2>Table ellipsis custom tooltip</h2>
<Table columns={columns} data={data} />
</div>
);

export default Demo;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"rc-animate": "^3.0.0",
"rc-dropdown": "~3.1.0",
"rc-menu": "^8.0.2",
"rc-tooltip": "^4.0.3",
"react": "^16.0.0",
"react-dnd": "^2.5.4",
"react-dnd-html5-backend": "^2.5.4",
Expand Down
9 changes: 7 additions & 2 deletions src/Cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CellType,
DefaultRecordType,
AlignType,
CellEllipsisType,
} from '../interface';
import { getPathValue } from '../utils/valueUtil';

Expand Down Expand Up @@ -38,7 +39,7 @@ export interface CellProps<RecordType extends DefaultRecordType> {
children?: React.ReactNode;
colSpan?: number;
rowSpan?: number;
ellipsis?: boolean;
ellipsis?: CellEllipsisType;
align?: AlignType;

shouldCellUpdate?: (record: RecordType) => boolean;
Expand All @@ -55,6 +56,8 @@ export interface CellProps<RecordType extends DefaultRecordType> {
/** @private Used for `expandable` with nest tree */
appendNode?: React.ReactNode;
additionalProps?: React.HTMLAttributes<HTMLElement>;

rowType?: 'header' | 'body' | 'footer';
}

function Cell<RecordType extends DefaultRecordType>(
Expand All @@ -79,6 +82,7 @@ function Cell<RecordType extends DefaultRecordType>(
additionalProps = {},
ellipsis,
align,
rowType,
}: CellProps<RecordType>,
ref: React.Ref<any>,
): React.ReactElement {
Expand Down Expand Up @@ -157,7 +161,8 @@ function Cell<RecordType extends DefaultRecordType>(

// ====================== Render ======================
let title: string;
if (ellipsis) {
const ellipsisConfig: CellEllipsisType = ellipsis === true ? { showTitle: true } : ellipsis;
if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
if (typeof childNode === 'string' || typeof childNode === 'number') {
title = childNode.toString();
} else if (React.isValidElement(childNode) && typeof childNode.props.children === 'string') {
Expand Down
1 change: 1 addition & 0 deletions src/Header/HeaderRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function HeaderRow<RecordType>({
key={columnsKey[cellIndex]}
{...fixedInfo}
additionalProps={additionalProps}
rowType="header"
/>
);
})}
Expand Down
4 changes: 3 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ export interface RenderedCell<RecordType> {

export type DataIndex = string | number | (string | number)[];

export type CellEllipsisType = { showTitle?: boolean } | boolean;

interface ColumnSharedType<RecordType> {
title?: React.ReactNode;
key?: Key;
className?: string;
fixed?: FixedType;
onHeaderCell?: GetComponentProps<ColumnsType<RecordType>[number]>;
ellipsis?: boolean;
ellipsis?: CellEllipsisType;
align?: AlignType;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,41 @@ describe('Table.Basic', () => {
});
});

it('renders ellipsis by showTitle option', () => {
const wrapper = mount(
createTable({
columns: [
{ title: 'title', ellipsis: { showTitle: true } },
{ title: 'node title', ellipsis: { showTitle: true }, render: () => <h1>233</h1> },
],
}),
);

wrapper.find('td').forEach(td => {
expect(td.hasClass('rc-table-cell-ellipsis')).toBeTruthy();
});
});

it('not renders ellipsis origin html title', () => {
const columns = [
{ title: 'title', ellipsis: { showTitle: false } },
{ title: 'node title', ellipsis: { showTitle: false }, render: () => <h1>233</h1> },
];
const wrapper = mount(
createTable({
columns,
}),
);

wrapper.find('.rc-table-thead th').forEach(td => {
expect(td.getDOMNode().attributes.getNamedItem('title')).toBeTruthy();
});

wrapper.find('.rc-table-tbody td').forEach(td => {
expect(td.getDOMNode().attributes.getNamedItem('title')).toBeFalsy();
});
});

it('renders column correctly', () => {
const columns = [
{
Expand Down