Skip to content

Commit e6d0086

Browse files
committed
refactor(Tooltip) 重构文字提示 uiwjs#845
1 parent a094e40 commit e6d0086

File tree

2 files changed

+188
-8
lines changed

2 files changed

+188
-8
lines changed

packages/react-tooltip/src/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import OverlayTrigger, { OverlayTriggerProps } from '@uiw/react-overlay-trigger';
33
import { IProps } from '@uiw/utils';
4-
import './style/index.less';
5-
4+
import { TooltipWarp, TooltipContainerWarp, TooltipArrowWarp, TooltipInnerWarp } from './style';
65
export interface TooltipProps extends IProps, OverlayTriggerProps {
76
visibleArrow?: boolean;
87
content?: React.ReactNode;
@@ -23,7 +22,8 @@ export default (props: TooltipProps = {}) => {
2322
} = props;
2423
const cls = [prefixCls, className, !visibleArrow ? 'no-arrow' : null].filter(Boolean).join(' ').trim();
2524
return (
26-
<OverlayTrigger
25+
<TooltipWarp
26+
as={OverlayTrigger}
2727
usePortal={usePortal}
2828
isOpen={isOpen}
2929
trigger={trigger}
@@ -32,13 +32,13 @@ export default (props: TooltipProps = {}) => {
3232
placement={placement}
3333
{...other}
3434
overlay={
35-
<div className={cls}>
36-
{visibleArrow && <div className={`${prefixCls}-arrow`} />}
37-
<div className={`${prefixCls}-inner`}>{props.content}</div>
38-
</div>
35+
<TooltipContainerWarp placement={placement} visibleArrow={visibleArrow} className={cls}>
36+
{visibleArrow && <TooltipArrowWarp placement={placement} className={`${prefixCls}-arrow`} />}
37+
<TooltipInnerWarp className={`${prefixCls}-inner`}>{props.content}</TooltipInnerWarp>
38+
</TooltipContainerWarp>
3939
}
4040
>
4141
{typeof props.children === 'object' ? props.children : <span>{props.children}</span>}
42-
</OverlayTrigger>
42+
</TooltipWarp>
4343
);
4444
};
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import styled, { css } from 'styled-components';
2+
import { getThemeVariantValue, ThemeVariantValueOptions } from '@uiw/utils';
3+
import { TooltipProps } from '../';
4+
5+
export interface TooltipStyleProps extends TooltipProps, ThemeVariantValueOptions {}
6+
7+
interface TooltipContainerProps {
8+
visibleArrow: boolean;
9+
placement: TooltipStyleProps['placement'];
10+
}
11+
12+
interface TooltipArrowProps extends ThemeVariantValueOptions {
13+
placement: TooltipStyleProps['placement'];
14+
}
15+
16+
function tooltipHandle(placement: TooltipStyleProps['placement']) {
17+
const obj = {
18+
right: 'right',
19+
rightTop: 'right',
20+
rightBottom: 'right',
21+
22+
left: 'left',
23+
leftTop: 'left',
24+
leftBottom: 'left',
25+
26+
top: 'top',
27+
topLeft: 'top',
28+
topRight: 'top',
29+
30+
bottom: 'bottom',
31+
bottomLeft: 'bottom',
32+
bottomRight: 'bottom',
33+
};
34+
switch (obj[placement!]) {
35+
case 'bottom':
36+
return css`
37+
padding-top: 5px;
38+
`;
39+
case 'top':
40+
return css`
41+
padding-bottom: 5px;
42+
`;
43+
case 'right':
44+
return css`
45+
padding-left: 5px;
46+
`;
47+
case 'left':
48+
return css`
49+
padding-right: 5px;
50+
`;
51+
default:
52+
return css``;
53+
}
54+
}
55+
56+
function tooltipArrowHandle(placement: TooltipStyleProps['placement']) {
57+
const obj = {
58+
right: 'right',
59+
rightTop: 'right',
60+
rightBottom: 'right',
61+
62+
left: 'left',
63+
leftTop: 'left',
64+
leftBottom: 'left',
65+
66+
top: 'top',
67+
topLeft: 'top',
68+
topRight: 'top',
69+
70+
bottom: 'bottom',
71+
bottomLeft: 'bottom',
72+
bottomRight: 'bottom',
73+
};
74+
switch (obj[placement!]) {
75+
case 'right':
76+
return css`
77+
border-right-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')};
78+
border-width: 5px 5px 5px 0;
79+
left: 0;
80+
margin-top: -5px;
81+
top: 50%;
82+
`;
83+
case 'left':
84+
return css`
85+
border-left-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')};
86+
border-width: 5px 0 5px 5px;
87+
margin-top: -5px;
88+
right: 0;
89+
top: 50%;
90+
`;
91+
case 'top':
92+
return css`
93+
border-top-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')};
94+
border-width: 5px 5px 0;
95+
bottom: 0;
96+
left: 50%;
97+
margin-left: -5px;
98+
`;
99+
case 'bottom':
100+
return css`
101+
border-bottom-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')};
102+
border-width: 0 5px 5px;
103+
left: 50%;
104+
margin-left: -5px;
105+
top: 0;
106+
`;
107+
default:
108+
return css``;
109+
}
110+
}
111+
112+
export const TooltipWarp = styled.div<TooltipStyleProps>``;
113+
export const TooltipContainerWarp = styled.div<TooltipContainerProps>`
114+
position: relative;
115+
display: inline-block;
116+
${(props) =>
117+
!props.visibleArrow &&
118+
css`
119+
padding: 0 !important;
120+
`}
121+
${(props) => tooltipHandle(props.placement)}
122+
`;
123+
export const TooltipArrowWarp = styled.div<TooltipArrowProps>`
124+
position: absolute;
125+
width: 0;
126+
height: 0;
127+
border-color: transparent;
128+
border-style: solid;
129+
${(props) => tooltipArrowHandle(props.placement)}
130+
${({ placement }) => {
131+
if (['leftTop', 'rightTop'].includes(placement!)) {
132+
return css`
133+
top: 15px;
134+
`;
135+
}
136+
if (['leftBottom', 'rightBottom'].includes(placement!)) {
137+
return css`
138+
bottom: 10px;
139+
top: auto;
140+
`;
141+
}
142+
if (['bottomLeft', 'topLeft'].includes(placement!)) {
143+
return css`
144+
left: 15px;
145+
`;
146+
}
147+
if (['bottomRight', 'topRight'].includes(placement!)) {
148+
return css`
149+
right: 10px;
150+
left: auto;
151+
`;
152+
}
153+
return css``;
154+
}}
155+
`;
156+
export const TooltipInnerWarp = styled.div<ThemeVariantValueOptions>`
157+
font-size: 12px;
158+
max-width: 250px;
159+
padding: 5px 10px;
160+
display: block;
161+
color: #fff;
162+
text-align: left;
163+
text-decoration: none;
164+
border-radius: 4px;
165+
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
166+
word-break: break-all;
167+
background-color: ${(props) => getThemeVariantValue(props, 'backgroundColorDefault')};
168+
`;
169+
170+
TooltipInnerWarp.defaultProps = {
171+
defaultTheme: {
172+
backgroundColorDefault: 'rgba(0, 0, 0, 0.75)',
173+
borderColorDefault: 'rgba(0, 0, 0, 0.75)',
174+
},
175+
};
176+
TooltipArrowWarp.defaultProps = {
177+
defaultTheme: {
178+
borderColorDefault: 'rgba(0, 0, 0, 0.75)',
179+
},
180+
};

0 commit comments

Comments
 (0)