基于 AntD 封装的 TextEllipsis
这里基本上都是参考的 基于 ElementUI 封装的 TextEllipsis | Henry (opens new window)
就不再赘述了,直接上源码:
import React from 'react'
import { useImmer } from 'use-immer'
import { Tooltip } from 'antd'
import './index.scss'
interface Props {
title: string
lineClamp?: number
mouseEnterDelay?: number
mouseLeaveDelay?: number
}
const TextEllipsis = function (props: Props) {
const { title, lineClamp = 1, mouseEnterDelay = 300, mouseLeaveDelay = 300 } = props
const [state, updateState] = useImmer({
visible: false
})
function handleMouseEnter({ target }) {
setTimeout(() => {
const { scrollHeight, clientHeight } = target
const visible = scrollHeight - clientHeight >= clientHeight / lineClamp
updateState(draft => {
draft.visible = visible
})
}, mouseEnterDelay)
}
function handleMouseLeave() {
setTimeout(() => {
updateState(draft => {
draft.visible = false
})
}, mouseLeaveDelay)
}
return (
<Tooltip title={title} visible={state.visible}>
<div className="text-ellipsis">
<div
className="text-ellipsis-content"
style={{ WebkitLineClamp: lineClamp }}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}>
{title}
</div>
</div>
</Tooltip>
)
}
export default TextEllipsis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
编辑 (opens new window)
上次更新: 8/5/2022, 7:01:19 AM
- 01
- 搭配 Jenkins 实现自动化打包微前端多个项目09-15
- 02
- 自动化打包微前端多个项目09-15
- 03
- el-upload 直传阿里 oss 并且显示自带进度条和视频回显封面图06-05