Skip to content

Commit 0a178a4

Browse files
author
liyuan-meng
committed
[CTable] 解决文字超长展示省略号的计算逻辑有误的问题
1 parent 5ccc02e commit 0a178a4

7 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/components/c-table/columnTpl/link.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React, { useRef, useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { prefixCls } from '@utils';
4-
import { getTextWidth, isVoid } from '../util';
4+
import { isVoid } from '../util';
55
import Tooltip from '../../tooltip';
66
import './index.less';
77

8-
export default function LinkTpl({ value, row, linkKey, link }) {
8+
export default function LinkTpl({ value, row, linkKey, link, onClick }) {
99
const ref = useRef();
1010
const [tooltipContent, setTooltipContent] = useState('');
1111

1212
useEffect(() => {
1313
if (!isVoid(value)) {
14-
if (ref.current.clientWidth <= getTextWidth(value)) {
14+
if (ref.current.scrollWidth > ref.current.clientWidth) {
1515
setTooltipContent(value);
1616
}
1717
}
@@ -27,6 +27,10 @@ export default function LinkTpl({ value, row, linkKey, link }) {
2727
ref={ref}
2828
className={`${prefixCls}-table-column-tpl-link`}
2929
onClick={() => {
30+
if (onClick) {
31+
onClick();
32+
return;
33+
}
3034
if (link || row?.[linkKey]) {
3135
window.open(link || row?.[linkKey]);
3236
}

src/components/c-table/columnTpl/multiLink.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React, { useRef, useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { prefixCls } from '@utils';
4-
import { getTextWidth, isVoid } from '../util';
4+
import { isVoid } from '../util';
55
import Tooltip from '../../tooltip';
66
import './index.less';
77

8-
export default function MultiLinkTpl({ value, row, linkKey }) {
8+
export default function MultiLinkTpl({ value, row, linkKey, onClick }) {
99
const ref = useRef();
1010
const [tooltipContent, setTooltipContent] = useState('');
1111

1212
useEffect(() => {
1313
if (!isVoid(value)) {
14-
if (ref.current.clientWidth * 2 <= getTextWidth(value)) {
14+
if (ref.current.scrollHeight > ref.current.clientHeight) {
1515
setTooltipContent(value);
1616
}
1717
}
@@ -27,6 +27,10 @@ export default function MultiLinkTpl({ value, row, linkKey }) {
2727
ref={ref}
2828
className={`${prefixCls}-table-column-tpl-multi-link`}
2929
onClick={() => {
30+
if (onClick) {
31+
onClick();
32+
return;
33+
}
3034
if (row?.[linkKey]) {
3135
window.open(row?.[linkKey]);
3236
}

src/components/c-table/columnTpl/multiText.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useRef, useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { prefixCls } from '@utils';
4-
import { getTextWidth, isVoid } from '../util';
4+
import { isVoid } from '../util';
55
import Tooltip from '../../tooltip';
66
import './index.less';
77

@@ -11,7 +11,7 @@ export default function MultiTextTpl({ value }) {
1111

1212
useEffect(() => {
1313
if (!isVoid(value)) {
14-
if (ref.current.clientWidth * 2 <= getTextWidth(value)) {
14+
if (ref.current.scrollHeight > ref.current.clientHeight) {
1515
setTooltipContent(value);
1616
}
1717
}

src/components/c-table/columnTpl/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useRef, useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { prefixCls } from '@utils';
4-
import { getTextWidth, isVoid } from '../util';
4+
import { isVoid } from '../util';
55
import Tooltip from '../../tooltip';
66
import './index.less';
77

@@ -11,7 +11,7 @@ export default function TextTpl({ value }) {
1111

1212
useEffect(() => {
1313
if (!isVoid(value)) {
14-
if (ref.current.clientWidth <= getTextWidth(value)) {
14+
if (ref.current.scrollWidth > ref.current.clientWidth) {
1515
setTooltipContent(value);
1616
}
1717
}

src/components/c-table/demos/uniform_text.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ const data = [
2020
{ id: '121408294', name: '文本文本超长超长超长超长超长超长超长超长超长超长超长超长超长超长超长超长', url: 'https://www.taobao.com', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan' },
2121
{ id: '121407191', name: '文本文本文本文本文本3', url: 'https://www.taobao.com', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo' },
2222
{ id: '121407192', name: '文本文本4', url: '', createTime: '2021/12/13 11:03:07', creator: 'han.wu' },
23+
{ id: '121407199', name: 'koookoooko-20240520000-20240520000', url: '', createTime: '2021/12/13 11:03:07', creator: 'han.wu' },
2324
{ id: '', name: '', url: '', createTime: '', creator: '' },
2425
];
2526

2627
const columns = [
2728
{
2829
title: '单行文本',
2930
dataIndex: 'name',
30-
width: 120,
31+
width: 100,
3132
type: 'TEXT'
3233
},
3334
{
@@ -42,7 +43,7 @@ const columns = [
4243
{
4344
title: '多行文本',
4445
dataIndex: 'name',
45-
width: 200,
46+
width: 100,
4647
type: 'MULTI_TEXT'
4748
},
4849
{

src/components/c-table/demos/uniform_tpl.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ const columns = [
109109
dataIndex: 'name',
110110
width: 120,
111111
render: (val, row) => {
112-
return <CTable.LinkTpl value={val} link="https://www.taobao.com" />
112+
return <CTable.LinkTpl
113+
value={val}
114+
onClick={() => {
115+
window.open("https://www.taobao.com")
116+
}}
117+
/>
113118
}
114119
},
115120
{
@@ -125,7 +130,12 @@ const columns = [
125130
dataIndex: 'name',
126131
width: 120,
127132
render: (val, row) => {
128-
return <CTable.MultiLinkTpl value={val} link="https://www.taobao.com" />
133+
return <CTable.MultiLinkTpl
134+
value={val}
135+
onClick={() => {
136+
window.open("https://www.taobao.com")
137+
}}
138+
/>
129139
}
130140
},
131141
{

src/components/c-table/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ this.tableRef.current.setColumn(columnData, isReloadGrid?);
220220
| 属性 | 说明 | 类型 | 默认值 |
221221
| ------------------- | -------------------------------------------- | --------------------------- | ---------------- |
222222
| value | 展示值,使用组件的形式必传,使用 typeConfig 忽略次字段 | string | - |
223-
| typeConfig.linkKey | 链接 key | string | '' |
223+
| typeConfig.linkKey | 链接 key(取自 row 中的字段) | string | '' |
224224
| typeConfig.link | 链接值 | string | '' |
225+
| typeConfig.onClick | 点击链接的回调函数 | func | - |
225226

226227
**(5)type 为 标签类型-`TAG`**
227228
_可根据下表配置 typeConfig,查看 [Demo](https://cloud-react.shuyun.com/v1/cloud-react/data/c-table#标准化表格-标签类型);也可以使用组件形式 <Table.TagTpl/>,查看 [Demo](https://cloud-react.shuyun.com/v1/cloud-react/data/c-table#标准化表格-使用列模板形式)_

0 commit comments

Comments
 (0)