-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathTooltip.tsx
150 lines (136 loc) · 4.03 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
import type { ArrowType, TriggerProps, TriggerRef } from '@rc-component/trigger';
import Trigger from '@rc-component/trigger';
import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface';
import useId from '@rc-component/util/lib/hooks/useId';
import classNames from 'classnames';
import * as React from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';
import { placements } from './placements';
import Popup from './Popup';
export interface TooltipProps
extends Pick<
TriggerProps,
| 'onPopupAlign'
| 'builtinPlacements'
| 'fresh'
| 'children'
| 'mouseLeaveDelay'
| 'mouseEnterDelay'
| 'prefixCls'
| 'forceRender'
| 'popupVisible'
> {
trigger?: ActionType | ActionType[];
defaultVisible?: boolean;
visible?: boolean;
placement?: string;
/** Config popup motion */
motion?: TriggerProps['popupMotion'];
onVisibleChange?: (visible: boolean) => void;
afterVisibleChange?: (visible: boolean) => void;
overlay: (() => React.ReactNode) | React.ReactNode;
/** @deprecated Please use `styles={{ root: {} }}` */
overlayStyle?: React.CSSProperties;
/** @deprecated Please use `classNames={{ root: {} }}` */
overlayClassName?: string;
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
destroyTooltipOnHide?: boolean;
align?: AlignType;
showArrow?: boolean | ArrowType;
arrowContent?: React.ReactNode;
id?: string;
/** @deprecated Please use `styles={{ body: {} }}` */
overlayInnerStyle?: React.CSSProperties;
zIndex?: number;
styles?: TooltipStyles;
classNames?: TooltipClassNames;
}
export interface TooltipStyles {
root?: React.CSSProperties;
body?: React.CSSProperties;
}
export interface TooltipClassNames {
root?: string;
body?: string;
}
export interface TooltipRef extends TriggerRef {}
const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
const {
overlayClassName,
trigger = ['hover'],
mouseEnterDelay = 0,
mouseLeaveDelay = 0.1,
overlayStyle,
prefixCls = 'rc-tooltip',
children,
onVisibleChange,
afterVisibleChange,
motion,
placement = 'right',
align = {},
destroyTooltipOnHide = false,
defaultVisible,
getTooltipContainer,
overlayInnerStyle,
arrowContent,
overlay,
id,
showArrow = true,
classNames: tooltipClassNames,
styles: tooltipStyles,
...restProps
} = props;
const mergedId = useId(id);
const triggerRef = useRef<TriggerRef>(null);
useImperativeHandle(ref, () => triggerRef.current);
const extraProps: Partial<TooltipProps & TriggerProps> = { ...restProps };
if ('visible' in props) {
extraProps.popupVisible = props.visible;
}
const getPopupElement = () => (
<Popup
key="content"
prefixCls={prefixCls}
id={mergedId}
bodyClassName={tooltipClassNames?.body}
overlayInnerStyle={{ ...overlayInnerStyle, ...tooltipStyles?.body }}
>
{overlay}
</Popup>
);
const getChildren = () => {
const child = React.Children.only(children);
const originalProps = child?.props || {};
const childProps = {
...originalProps,
'aria-describedby': overlay ? mergedId : null,
};
return React.cloneElement(children, childProps);
};
return (
<Trigger
popupClassName={classNames(overlayClassName, tooltipClassNames?.root)}
prefixCls={prefixCls}
popup={getPopupElement}
action={trigger}
builtinPlacements={placements}
popupPlacement={placement}
ref={triggerRef}
popupAlign={align}
getPopupContainer={getTooltipContainer}
onPopupVisibleChange={onVisibleChange}
afterPopupVisibleChange={afterVisibleChange}
popupMotion={motion}
defaultPopupVisible={defaultVisible}
autoDestroy={destroyTooltipOnHide}
mouseLeaveDelay={mouseLeaveDelay}
popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
mouseEnterDelay={mouseEnterDelay}
arrow={showArrow}
{...extraProps}
>
{getChildren()}
</Trigger>
);
};
export default forwardRef(Tooltip);