11import React , { Component } from 'react' ;
2- import { GetArrowPropsArg , GetTooltipPropsArg , TooltipProps } from './types' ;
3- import { callAll , TooltipContext } from './utils' ;
2+ import {
3+ GetArrowPropsArg ,
4+ GetTooltipPropsArg ,
5+ TooltipProps ,
6+ TriggerTypes
7+ } from './types' ;
8+ import { callAll , TooltipContext , setRef } from './utils' ;
49
510const MUTATION_OBSERVER_CONFIG : MutationObserverInit = {
611 childList : true ,
@@ -14,13 +19,16 @@ class Tooltip extends Component<TooltipProps> {
1419 private tooltipRef ! : HTMLElement | null ;
1520
1621 public componentDidMount ( ) {
17- const { trigger} = this . props ;
1822 const observer = ( this . observer = new MutationObserver ( ( ) => {
1923 this . props . scheduleUpdate ( ) ;
2024 } ) ) ;
2125 observer . observe ( this . tooltipRef ! , MUTATION_OBSERVER_CONFIG ) ;
2226
23- if ( trigger !== 'none' && trigger !== 'focus' ) {
27+ if (
28+ this . isTriggeredBy ( 'hover' ) ||
29+ this . isTriggeredBy ( 'click' ) ||
30+ this . isTriggeredBy ( 'right-click' )
31+ ) {
2432 const {
2533 removeParentOutsideClickHandler,
2634 removeParentOutsideRightClickHandler
@@ -43,12 +51,15 @@ class Tooltip extends Component<TooltipProps> {
4351 }
4452
4553 public componentWillUnmount ( ) {
46- const { trigger} = this . props ;
4754 if ( this . observer ) {
4855 this . observer . disconnect ( ) ;
4956 }
5057
51- if ( trigger !== 'none' && trigger !== 'focus' ) {
58+ if (
59+ this . isTriggeredBy ( 'hover' ) ||
60+ this . isTriggeredBy ( 'click' ) ||
61+ this . isTriggeredBy ( 'right-click' )
62+ ) {
5263 const {
5364 isParentNoneTriggered,
5465 addParentOutsideClickHandler,
@@ -83,6 +94,13 @@ class Tooltip extends Component<TooltipProps> {
8394 ) ;
8495 }
8596
97+ private isTriggeredBy ( event : TriggerTypes ) {
98+ const { trigger} = this . props ;
99+ return (
100+ trigger === event || ( Array . isArray ( trigger ) && trigger . includes ( event ) )
101+ ) ;
102+ }
103+
86104 private handleOutsideClick ?: EventListener = event => {
87105 if ( this . tooltipRef && ! this . tooltipRef . contains ( event . target as Node ) ) {
88106 const { parentOutsideClickHandler} = this . context ;
@@ -131,28 +149,24 @@ class Tooltip extends Component<TooltipProps> {
131149 this . handleOutsideRightClick !
132150 ) ;
133151
134- private getTooltipRef = ( ref : HTMLElement | null ) => {
135- this . tooltipRef = ref ;
136- this . props . innerRef ( ref ) ;
152+ private getTooltipRef = ( node : HTMLElement | null ) => {
153+ this . tooltipRef = node ;
154+ setRef ( this . props . innerRef , node ) ;
137155 } ;
138156
139157 private getArrowProps = ( props : GetArrowPropsArg = { } ) => ( {
140158 ...props ,
141159 style : { ...props . style , ...this . props . arrowProps . style }
142160 } ) ;
143161
144- private getTooltipProps = ( props : GetTooltipPropsArg = { } ) => {
145- const isHoverTriggered = this . props . trigger === 'hover' ;
146-
147- return {
148- ...props ,
149- ...( isHoverTriggered && {
150- onMouseEnter : callAll ( this . props . clearScheduled , props . onMouseEnter ) ,
151- onMouseLeave : callAll ( this . props . hideTooltip , props . onMouseLeave )
152- } ) ,
153- style : { ...props . style , ...this . props . style }
154- } ;
155- } ;
162+ private getTooltipProps = ( props : GetTooltipPropsArg = { } ) => ( {
163+ ...props ,
164+ ...( this . isTriggeredBy ( 'hover' ) && {
165+ onMouseEnter : callAll ( this . props . clearScheduled , props . onMouseEnter ) ,
166+ onMouseLeave : callAll ( this . props . hideTooltip , props . onMouseLeave )
167+ } ) ,
168+ style : { ...props . style , ...this . props . style }
169+ } ) ;
156170
157171 private contextValue = {
158172 isParentNoneTriggered : this . props . trigger === 'none' ,
0 commit comments