Skip to content

Commit abb8df7

Browse files
committed
feat: 添加 BetondHiding组件
1 parent 7b11370 commit abb8df7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/components/BeyondHiding.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Tooltip } from 'antd';
2+
import type { TooltipProps } from 'antd/lib/tooltip';
3+
import React from 'react';
4+
5+
type BeyondHidingProps = Omit<TooltipProps, 'open' | 'trigger'> & {
6+
className?: string;
7+
style?: React.CSSProperties;
8+
title: React.ReactNode;
9+
};
10+
11+
const BeyondHiding = ({ className, style, title, ...props }: BeyondHidingProps) => {
12+
const [isShow, setIsShow] = useState(false);
13+
14+
const contentRef = useRef<HTMLSpanElement>(null);
15+
16+
const isShowTooltip = (): void => {
17+
// 计算span标签的offsetWidth与盒子元素的offsetWidth,给isShow赋值
18+
if (contentRef.current && contentRef.current.parentElement) {
19+
const spanWidth = contentRef.current.offsetWidth;
20+
const parentWidth = contentRef.current.parentElement.offsetWidth;
21+
22+
if (spanWidth > parentWidth) {
23+
setIsShow(true);
24+
}
25+
}
26+
};
27+
return (
28+
<Tooltip
29+
open={isShow}
30+
title={title}
31+
{...props}
32+
>
33+
<span
34+
className={className}
35+
ref={contentRef}
36+
style={style}
37+
onMouseLeave={() => setIsShow(false)}
38+
onMouseOver={isShowTooltip}
39+
>
40+
{title}
41+
</span>
42+
</Tooltip>
43+
);
44+
};
45+
46+
export default BeyondHiding;

0 commit comments

Comments
 (0)