Skip to content

enable tooltip when content overflows table cell #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions components/table/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const ColumnProps = {
filteredValue: PropTypes.array,
sortOrder: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['ascend', 'descend'])]),
sortDirections: PropTypes.array,
showOverflowTooltip: PropTypes.bool,
// children?: ColumnProps<T>[];
// onCellClick?: (record: T, event: any) => void;
// onCell?: (record: T) => any;
Expand Down
10 changes: 10 additions & 0 deletions components/table/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@
&-tbody > tr > td {
border-bottom: @border-width-base @border-style-base @border-color-split;
transition: all 0.3s, border 0s;

&.has-tooltip {

> div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

}
}

&-thead > tr,
Expand Down
74 changes: 62 additions & 12 deletions components/vc-table/src/TableCell.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from '../../_util/vue-types';
import get from 'lodash/get';
import { isValidElement, mergeProps } from '../../_util/props-util';
import Tooltip from '../../tooltip';

function isInvalidRenderCellText(text) {
return (
Expand All @@ -20,6 +21,33 @@ export default {
expandIcon: PropTypes.any,
component: PropTypes.any,
},
data() {
return {
tooltipContent: '',
notShowTooltip: false,
tooltipVisible: false,
};
},
mounted() {
if (this.column.showOverflowTooltip) {
let cell = this.$el;
let cellWidth = cell.getBoundingClientRect().width;

// get real text width
let textWrapper = document.createElement('span');
textWrapper.innerText = this.tooltipContent;
document.body.appendChild(textWrapper);
let range = document.createRange();
range.setStart(textWrapper, 0);
range.setEnd(textWrapper, 1);
const textWidth = range.getBoundingClientRect().width;
document.body.removeChild(textWrapper);

if (cellWidth > textWidth) {
this.notShowTooltip = true;
}
}
},
methods: {
handleClick(e) {
const {
Expand All @@ -30,6 +58,20 @@ export default {
onCellClick(record, e);
}
},
handleVisibilityChange(visible) {
this.tooltipVisible = this.notShowTooltip ? false : visible;
},
getRawText(record, dataIndex) {
let text;
if (typeof dataIndex === 'number') {
text = get(record, dataIndex);
} else if (!dataIndex || dataIndex.length === 0) {
text = record;
} else {
text = get(record, dataIndex);
}
return text;
},
},

render() {
Expand All @@ -47,14 +89,9 @@ export default {
const cls = className || column.class;
// We should return undefined if no dataIndex is specified, but in order to
// be compatible with object-path's behavior, we return the record object instead.
let text;
if (typeof dataIndex === 'number') {
text = get(record, dataIndex);
} else if (!dataIndex || dataIndex.length === 0) {
text = record;
} else {
text = get(record, dataIndex);
}
let text = this.getRawText(record, dataIndex);
this.tooltipContent = text;

let tdProps = {
props: {},
attrs: {},
Expand Down Expand Up @@ -100,11 +137,24 @@ export default {
tdProps.style = { ...tdProps.style, textAlign: column.align };
}

return (
const cellContent = [
indentText,
expandIcon,
text,
];

return column.showOverflowTooltip ? (
<BodyCell {...tdProps} class="has-tooltip" style={{maxWidth: 0}}>
<Tooltip
onVisibleChange={this.handleVisibilityChange}
visible={this.tooltipVisible}
title={this.tooltipContent}>
<div>{cellContent}</div>
</Tooltip>
</BodyCell>
) : (
<BodyCell {...tdProps}>
{indentText}
{expandIcon}
{text}
{cellContent}
</BodyCell>
);
},
Expand Down