11import type { ArrowType , TriggerProps , TriggerRef } from '@rc-component/trigger' ;
22import Trigger from '@rc-component/trigger' ;
33import type { ActionType , AlignType , AnimationType } from '@rc-component/trigger/lib/interface' ;
4+ import classNames from 'classnames' ;
45import * as React from 'react' ;
56import { forwardRef , useImperativeHandle , useRef } from 'react' ;
67import { placements } from './placements' ;
78import Popup from './Popup' ;
8- import classNames from 'classnames ' ;
9+ import useId from 'rc-util/lib/hooks/useId ' ;
910
1011export interface TooltipProps
1112 extends Pick <
@@ -60,7 +61,7 @@ export interface TooltipClassNames {
6061 body ?: string ;
6162}
6263
63- export interface TooltipRef extends TriggerRef { }
64+ export interface TooltipRef extends TriggerRef { }
6465
6566const Tooltip = ( props : TooltipProps , ref : React . Ref < TooltipRef > ) => {
6667 const {
@@ -91,7 +92,9 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
9192 ...restProps
9293 } = props ;
9394
95+ const mergedId = useId ( id ) ;
9496 const triggerRef = useRef < TriggerRef > ( null ) ;
97+
9598 useImperativeHandle ( ref , ( ) => triggerRef . current ) ;
9699
97100 const extraProps : Partial < TooltipProps & TriggerProps > = { ...restProps } ;
@@ -103,14 +106,26 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
103106 < Popup
104107 key = "content"
105108 prefixCls = { prefixCls }
106- id = { id }
109+ id = { mergedId }
107110 bodyClassName = { tooltipClassNames ?. body }
108111 overlayInnerStyle = { { ...overlayInnerStyle , ...tooltipStyles ?. body } }
109112 >
110113 { overlay }
111114 </ Popup >
112115 ) ;
113116
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+
114129 return (
115130 < Trigger
116131 popupClassName = { classNames ( overlayClassName , tooltipClassNames ?. root ) }
@@ -135,7 +150,7 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
135150 arrow = { showArrow }
136151 { ...extraProps }
137152 >
138- { children }
153+ { getChildren ( ) }
139154 </ Trigger >
140155 ) ;
141156} ;
0 commit comments