Skip to content

Commit fe2e017

Browse files
committed
use renderProps
1 parent e505c88 commit fe2e017

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/Tooltip.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface'
44
import useId from '@rc-component/util/lib/hooks/useId';
55
import { clsx } from 'clsx';
66
import * as React from 'react';
7-
import { useImperativeHandle, useRef, useState } from 'react';
7+
import { useImperativeHandle, useRef } from 'react';
88
import { placements } from './placements';
99
import Popup from './Popup';
1010

@@ -16,13 +16,13 @@ export interface TooltipProps
1616
| 'onPopupAlign'
1717
| 'builtinPlacements'
1818
| 'fresh'
19-
| 'children'
2019
| 'mouseLeaveDelay'
2120
| 'mouseEnterDelay'
2221
| 'prefixCls'
2322
| 'forceRender'
2423
| 'popupVisible'
2524
> {
25+
children: React.ReactElement;
2626
// Style
2727
classNames?: Partial<Record<SemanticName, string>>;
2828
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
@@ -90,19 +90,11 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
9090

9191
const extraProps: Partial<TooltipProps & TriggerProps> = { ...restProps };
9292

93-
const isControlled = 'visible' in props;
94-
95-
if (isControlled) {
93+
if ('visible' in props) {
9694
extraProps.popupVisible = props.visible;
9795
}
9896

99-
const [innerVisible, setInnerVisible] = useState(() => defaultVisible || false);
100-
const mergedVisible = isControlled ? props.visible : innerVisible;
101-
10297
const handleVisibleChange = (nextVisible: boolean) => {
103-
if (!isControlled) {
104-
setInnerVisible(nextVisible);
105-
}
10698
onVisibleChange?.(nextVisible);
10799
};
108100

@@ -126,14 +118,12 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
126118
}, [showArrow, classNames?.arrow, styles?.arrow, arrowContent]);
127119

128120
// ======================== Children ========================
129-
const getChildren = () => {
121+
const getChildren = (open: boolean) => {
130122
const child = React.Children.only(children);
131-
const originalProps = child?.props || {};
132-
const childProps = {
133-
...originalProps,
134-
'aria-describedby': overlay && mergedVisible ? mergedId : null,
123+
const ariaProps: React.AriaAttributes = {
124+
'aria-describedby': overlay && open ? mergedId : undefined,
135125
};
136-
return React.cloneElement<any>(children, childProps) as any;
126+
return React.cloneElement(child, ariaProps);
137127
};
138128

139129
// ========================= Render =========================
@@ -172,7 +162,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
172162
uniqueContainerStyle={styles?.uniqueContainer}
173163
{...extraProps}
174164
>
175-
{getChildren()}
165+
{({ open }) => getChildren(open)}
176166
</Trigger>
177167
);
178168
});

0 commit comments

Comments
 (0)