55 */
66
77import React , { useState , useRef } from 'react'
8- import T from 'prop-types'
9- import { values } from 'ramda'
108
9+ import type { TTooltipPlacement , TTooltipAnimation } from '@/spec'
1110import { css , buildLog , isDescendant } from '@/utils'
1211import { useOutsideClick } from '@/hooks'
1312
@@ -26,19 +25,44 @@ import {
2625/* eslint-disable-next-line */
2726const log = buildLog ( 'c:Tooltip:index' )
2827
29- const Tooltip = ( {
28+ type TProps = {
29+ children : React . ReactNode
30+ content : string | React . ReactNode
31+ animation ?: TTooltipAnimation
32+ placement ?: TTooltipPlacement
33+ // more options see: https://atomiks.github.io/tippyjs/all-options/
34+ delay ?: number
35+ duration ?: number
36+ trigger ?: 'mouseenter focus' | 'click'
37+ hideOnClick ?: boolean
38+ noPadding ?: boolean
39+ showArrow ?: boolean
40+ footerBehavior ?: 'default' | 'confirm' | 'delete-confirm' | 'add'
41+ // currently only for AvatarsRow, it will collapse the height
42+ // for same reason, figure out later
43+ contentHeight ?: string
44+
45+ onShow ?: ( ) => void
46+ onHide ?: ( ) => void
47+ onConfirm ?: ( ) => void
48+ }
49+
50+ const Tooltip : React . FC < TProps > = ( {
3051 children,
31- noPadding,
52+ animation = 'fade' ,
53+ noPadding = false ,
3254 onHide,
3355 onShow,
34- placement,
56+ placement = 'top' ,
57+ delay = 0 ,
58+ duration = 0 ,
3559 content,
36- hideOnClick,
37- showArrow,
38- footerBehavior,
60+ hideOnClick = true ,
61+ showArrow = true ,
62+ footerBehavior = 'default' ,
63+ trigger = 'mouseenter focus' ,
3964 onConfirm,
4065 contentHeight,
41- ...restProps
4266} ) => {
4367 const [ instance , setInstance ] = useState ( null )
4468 const [ active , setActive ] = useState ( false )
@@ -110,7 +134,10 @@ const Tooltip = ({
110134 } }
111135 zIndex = { css . zIndex . popover }
112136 interactive
113- { ...restProps }
137+ delay = { delay }
138+ duration = { duration }
139+ animation = { animation }
140+ trigger = { trigger }
114141 >
115142 { ContentComp }
116143 </ StyledTippy >
@@ -120,7 +147,7 @@ const Tooltip = ({
120147 content = { PopoverContent }
121148 placement = { placement }
122149 hideOnClick = { hideOnClick }
123- animation = "scale"
150+ animation = { animation }
124151 onHide = { ( instance ) => {
125152 setInstance ( instance )
126153 setActive ( false )
@@ -133,76 +160,13 @@ const Tooltip = ({
133160 } }
134161 zIndex = { css . zIndex . popover }
135162 interactive
136- { ...restProps }
163+ delay = { delay }
164+ duration = { duration }
165+ trigger = { trigger }
137166 >
138167 { ContentComp }
139168 </ NoPaddingStyledTippy >
140169 )
141170}
142171
143- Tooltip . propTypes = {
144- children : T . node . isRequired ,
145- content : T . oneOfType ( [ T . string , T . node ] ) . isRequired ,
146- // options
147- animation : T . oneOf ( [
148- 'shift-away' ,
149- 'shift-toward' ,
150- 'fade' ,
151- 'scale' ,
152- 'perspective' ,
153- ] ) ,
154- arrow : T . bool ,
155- delay : T . number ,
156- duration : T . number ,
157- placement : T . oneOf ( [
158- 'top' ,
159- 'top-start' ,
160- 'top-end' ,
161- 'bottom' ,
162- 'bottom-start' ,
163- 'bottom-end' ,
164- 'left' ,
165- 'left-start' ,
166- 'left-end' ,
167- 'right' ,
168- 'right-start' ,
169- 'right-end' ,
170- ] ) ,
171- // hooks
172- trigger : T . oneOf ( [ 'mouseenter focus' , 'click' ] ) ,
173- hideOnClick : T . oneOf ( [ true , false ] ) ,
174- maxWidth : T . oneOf ( [ 350 , 'none' ] ) ,
175- // more options see: https://atomiks.github.io/tippyjs/all-options/
176- onShow : T . oneOfType ( [ T . func , T . instanceOf ( null ) ] ) ,
177- onHide : T . oneOfType ( [ T . func , T . instanceOf ( null ) ] ) ,
178- noPadding : T . bool ,
179- showArrow : T . bool ,
180- footerBehavior : T . oneOf ( values ( FOOTER_BEHAVIOR ) ) ,
181- onConfirm : T . oneOfType ( [ T . func , T . instanceOf ( null ) ] ) ,
182-
183- // currently only for AvatarsRow, it will collapse the height
184- // for same reason, figure out later
185- contentHeight : T . string ,
186- }
187-
188- Tooltip . defaultProps = {
189- animation : 'fade' ,
190- arrow : true ,
191- delay : 0 ,
192- duration : 0 ,
193- hideOnClick : true ,
194- placement : 'top' ,
195- // hooks
196- trigger : 'mouseenter focus' ,
197- onShow : null ,
198- onHide : null ,
199- noPadding : false ,
200- maxWidth : 'none' ,
201- showArrow : true ,
202- footerBehavior : 'default' ,
203- onConfirm : null ,
204-
205- contentHeight : 'auto' ,
206- }
207-
208172export default React . memo ( Tooltip )
0 commit comments