1
1
import type { ArrowType , TriggerProps , TriggerRef } from '@rc-component/trigger' ;
2
2
import Trigger from '@rc-component/trigger' ;
3
3
import type { ActionType , AlignType , AnimationType } from '@rc-component/trigger/lib/interface' ;
4
+ import classNames from 'classnames' ;
4
5
import * as React from 'react' ;
5
6
import { forwardRef , useImperativeHandle , useRef } from 'react' ;
6
7
import { placements } from './placements' ;
7
8
import Popup from './Popup' ;
8
- import classNames from 'classnames ' ;
9
+ import useId from 'rc-util/lib/hooks/useId ' ;
9
10
10
11
export interface TooltipProps
11
12
extends Pick <
@@ -60,7 +61,7 @@ export interface TooltipClassNames {
60
61
body ?: string ;
61
62
}
62
63
63
- export interface TooltipRef extends TriggerRef { }
64
+ export interface TooltipRef extends TriggerRef { }
64
65
65
66
const Tooltip = ( props : TooltipProps , ref : React . Ref < TooltipRef > ) => {
66
67
const {
@@ -91,7 +92,9 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
91
92
...restProps
92
93
} = props ;
93
94
95
+ const mergedId = useId ( id ) ;
94
96
const triggerRef = useRef < TriggerRef > ( null ) ;
97
+
95
98
useImperativeHandle ( ref , ( ) => triggerRef . current ) ;
96
99
97
100
const extraProps : Partial < TooltipProps & TriggerProps > = { ...restProps } ;
@@ -103,14 +106,26 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
103
106
< Popup
104
107
key = "content"
105
108
prefixCls = { prefixCls }
106
- id = { id }
109
+ id = { mergedId }
107
110
bodyClassName = { tooltipClassNames ?. body }
108
111
overlayInnerStyle = { { ...overlayInnerStyle , ...tooltipStyles ?. body } }
109
112
>
110
113
{ overlay }
111
114
</ Popup >
112
115
) ;
113
116
117
+ const getChildren = ( ) => {
118
+ const child = React . Children . only ( children ) ;
119
+ const originalProps = child ?. props || { } ;
120
+
121
+ const childProps = {
122
+ ...originalProps ,
123
+ 'aria-describedby' : overlay ? mergedId : null ,
124
+ } ;
125
+
126
+ return React . cloneElement ( children , childProps ) ;
127
+ } ;
128
+
114
129
return (
115
130
< Trigger
116
131
popupClassName = { classNames ( overlayClassName , tooltipClassNames ?. root ) }
@@ -135,7 +150,7 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
135
150
arrow = { showArrow }
136
151
{ ...extraProps }
137
152
>
138
- { children }
153
+ { getChildren ( ) }
139
154
</ Trigger >
140
155
) ;
141
156
} ;
0 commit comments