@@ -4,7 +4,7 @@ import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface'
44import useId from '@rc-component/util/lib/hooks/useId' ;
55import { clsx } from 'clsx' ;
66import * as React from 'react' ;
7- import { useImperativeHandle , useRef , useEffect , useCallback } from 'react' ;
7+ import { useImperativeHandle , useRef , useState } from 'react' ;
88import { placements } from './placements' ;
99import Popup from './Popup' ;
1010
@@ -90,55 +90,22 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
9090
9191 const extraProps : Partial < TooltipProps & TriggerProps > = { ...restProps } ;
9292
93- if ( 'visible' in props ) {
93+ const isControlled = 'visible' in props ;
94+
95+ if ( isControlled ) {
9496 extraProps . popupVisible = props . visible ;
9597 }
9698
97- const isControlled = 'visible' in props ;
98- const mergedVisible = props . visible ;
99- const [ popupMounted , setPopupMounted ] = React . useState ( ( ) => {
100- if ( forceRender ) {
101- return true ;
102- }
103- if ( isControlled ) {
104- return mergedVisible ;
105- }
106- return defaultVisible ;
107- } ) ;
108-
109- const updatePopupMounted = useCallback (
110- ( nextVisible : boolean ) => {
111- setPopupMounted ( ( prev ) => {
112- if ( nextVisible ) {
113- return true ;
114- }
115-
116- if ( destroyOnHidden ) {
117- return false ;
118- }
119-
120- return prev ;
121- } ) ;
122- } ,
123- [ destroyOnHidden ] ,
124- ) ;
99+ const [ innerVisible , setInnerVisible ] = useState ( ( ) => defaultVisible || false ) ;
100+ const mergedVisible = isControlled ? props . visible : innerVisible ;
125101
126102 const handleVisibleChange = ( nextVisible : boolean ) => {
127- updatePopupMounted ( nextVisible ) ;
103+ if ( ! isControlled ) {
104+ setInnerVisible ( nextVisible ) ;
105+ }
128106 onVisibleChange ?.( nextVisible ) ;
129107 } ;
130108
131- useEffect ( ( ) => {
132- if ( forceRender ) {
133- setPopupMounted ( true ) ;
134- return ;
135- }
136-
137- if ( isControlled ) {
138- updatePopupMounted ( mergedVisible ) ;
139- }
140- } , [ forceRender , isControlled , mergedVisible , updatePopupMounted ] ) ;
141-
142109 // ========================= Arrow ==========================
143110 // Process arrow configuration
144111 const mergedArrow = React . useMemo ( ( ) => {
@@ -164,7 +131,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
164131 const originalProps = child ?. props || { } ;
165132 const childProps = {
166133 ...originalProps ,
167- 'aria-describedby' : overlay && popupMounted ? mergedId : null ,
134+ 'aria-describedby' : overlay && mergedVisible ? mergedId : null ,
168135 } ;
169136 return React . cloneElement < any > ( children , childProps ) as any ;
170137 } ;
0 commit comments