-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathTooltip.tsx
227 lines (206 loc) · 7.24 KB
/
Tooltip.tsx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { defineComponent, ExtractPropTypes, CSSProperties, onMounted, ref } from 'vue';
import VcTooltip from '../vc-tooltip';
import classNames from '../_util/classNames';
import getPlacements from './placements';
import PropTypes from '../_util/vue-types';
import { PresetColorTypes } from '../_util/colors';
import warning from '../_util/warning';
import { getPropsSlot, getStyle, filterEmpty, isValidElement } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import abstractTooltipProps, { triggerTypes, placementTypes } from './abstractTooltipProps';
import useConfigInject from '../_util/hooks/useConfigInject';
const splitObject = (obj: any, keys: string[]) => {
const picked = {};
const omitted = { ...obj };
keys.forEach((key) => {
if (obj && key in obj) {
picked[key] = obj[key];
delete omitted[key];
}
});
return { picked, omitted };
};
const props = abstractTooltipProps();
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
const tooltipProps = {
...props,
title: PropTypes.VNodeChild,
};
export type TriggerTypes = typeof triggerTypes[number];
export type PlacementTypes = typeof placementTypes[number];
export type TooltipProps = Partial<ExtractPropTypes<typeof tooltipProps>>;
export default defineComponent({
name: 'ATooltip',
inheritAttrs: false,
props: tooltipProps,
emits: ['update:visible', 'visibleChange'],
setup(props, { slots, emit, attrs, expose }) {
const { prefixCls, getTargetContainer } = useConfigInject('tooltip', props);
const visible = ref(props.visible);
const tooltip = ref();
onMounted(() => {
warning(
!('default-visible' in attrs) || !('defaultVisible' in attrs),
'Tooltip',
`'defaultVisible' is deprecated, please use 'v-model:visible'`,
);
});
const handleVisibleChange = (bool: boolean) => {
visible.value = isNoTitle() ? false : bool;
if (!isNoTitle()) {
emit('update:visible', bool);
emit('visibleChange', bool);
}
};
const isNoTitle = () => {
const title = getPropsSlot(slots, props, 'title');
return !title && title !== 0;
};
const getPopupDomNode = () => {
return tooltip.value.getPopupDomNode();
};
const getVisible = () => {
return !!visible.value;
};
expose({ getPopupDomNode, getVisible });
const getTooltipPlacements = () => {
const { builtinPlacements, arrowPointAtCenter, autoAdjustOverflow } = props;
return (
builtinPlacements ||
getPlacements({
arrowPointAtCenter,
autoAdjustOverflow,
})
);
};
const getDisabledCompatibleChildren = (ele: any) => {
if (
((typeof ele.type === 'object' &&
(ele.type.__ANT_BUTTON === true ||
ele.type.__ANT_SWITCH === true ||
ele.type.__ANT_CHECKBOX === true)) ||
ele.type === 'button') &&
ele.props &&
(ele.props.disabled || ele.props.disabled === '')
) {
// Pick some layout related style properties up to span
// Prevent layout bugs like https://github.com/ant-design/ant-design/issues/5254
const { picked, omitted } = splitObject(getStyle(ele), [
'position',
'left',
'right',
'top',
'bottom',
'float',
'display',
'zIndex',
]);
const spanStyle = {
display: 'inline-block', // default inline-block is important
...picked,
cursor: 'not-allowed',
width: ele.props && ele.props.block ? '100%' : null,
};
const buttonStyle = {
...omitted,
pointerEvents: 'none',
};
const child = cloneElement(
ele,
{
style: buttonStyle,
},
true,
);
return <span style={spanStyle}>{child}</span>;
}
return ele;
};
const getOverlay = () => {
const title = getPropsSlot(slots, props, 'title');
if (title === 0) {
return title;
}
return title || '';
};
const onPopupAlign = (domNode: HTMLElement, align: any) => {
const placements = getTooltipPlacements();
// 当前返回的位置
const placement = Object.keys(placements).filter(
(key) =>
placements[key].points[0] === align.points[0] &&
placements[key].points[1] === align.points[1],
)[0];
if (!placement) {
return;
}
// 根据当前坐标设置动画点
const rect = domNode.getBoundingClientRect();
const transformOrigin = {
top: '50%',
left: '50%',
};
if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
transformOrigin.top = `${rect.height - align.offset[1]}px`;
} else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
transformOrigin.top = `${-align.offset[1]}px`;
}
if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
transformOrigin.left = `${rect.width - align.offset[0]}px`;
} else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
transformOrigin.left = `${-align.offset[0]}px`;
}
domNode.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`;
};
return () => {
const { openClassName, getPopupContainer, color, overlayClassName } = props;
let children = filterEmpty(slots.default?.()) ?? null;
children = children.length === 1 ? children[0] : children;
// Hide tooltip when there is no title
if (!('visible' in props) && isNoTitle()) {
visible.value = false;
}
if (!children) {
return null;
}
const child = getDisabledCompatibleChildren(
isValidElement(children) ? children : <span>{children}</span>,
);
const childCls = classNames({
[openClassName || `${prefixCls.value}-open`]: visible.value,
[child.props && child.props.class]: child.props && child.props.class,
});
const customOverlayClassName = classNames(overlayClassName, {
[`${prefixCls.value}-${color}`]: color && PresetColorRegex.test(color),
});
let formattedOverlayInnerStyle: CSSProperties;
let arrowContentStyle: CSSProperties;
if (color && !PresetColorRegex.test(color)) {
formattedOverlayInnerStyle = { backgroundColor: color };
arrowContentStyle = { backgroundColor: color };
}
const vcTooltipProps = {
...attrs,
...props,
prefixCls: prefixCls.value,
getTooltipContainer: getPopupContainer || getTargetContainer.value,
builtinPlacements: getTooltipPlacements(),
overlay: getOverlay(),
visible: visible.value,
ref: tooltip,
overlayClassName: customOverlayClassName,
overlayInnerStyle: formattedOverlayInnerStyle,
arrowContent: (
<span class={`${prefixCls.value}-arrow-content`} style={arrowContentStyle}></span>
),
onVisibleChange: handleVisibleChange,
onPopupAlign,
};
return (
<VcTooltip {...vcTooltipProps}>
{visible.value ? cloneElement(child, { class: childCls }) : child}
</VcTooltip>
);
};
},
});