Skip to content

Commit 3def61d

Browse files
committed
refactor(CTooltip): improve className handling; improve props interface; make visible property reactive
1 parent 21bacfd commit 3def61d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

packages/coreui-react/src/components/tooltip/CTooltip.tsx

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React, { FC, ReactNode, useRef, useState } from 'react'
1+
import React, { FC, HTMLAttributes, ReactNode, useEffect, useRef, useState } from 'react'
22
import { createPortal } from 'react-dom'
3-
43
import classNames from 'classnames'
54
import PropTypes from 'prop-types'
65
import { usePopper } from 'react-popper'
76
import { Transition } from 'react-transition-group'
87

98
import { Triggers, triggerPropType } from '../Types'
109

11-
export interface CTooltipProps {
12-
// TODO: find solution to not use any
13-
children: any
10+
export interface CTooltipProps extends HTMLAttributes<HTMLDivElement> {
11+
/**
12+
* A string of all className you want applied to the component.
13+
*/
14+
className?: string
1415
/**
1516
* Content node for your component.
1617
*/
@@ -45,6 +46,7 @@ export interface CTooltipProps {
4546

4647
export const CTooltip: FC<CTooltipProps> = ({
4748
children,
49+
className,
4850
content,
4951
offset = [0, 0],
5052
onHide,
@@ -73,6 +75,10 @@ export const CTooltip: FC<CTooltipProps> = ({
7375
placement: placement,
7476
})
7577

78+
useEffect(() => {
79+
setVisible(visible)
80+
}, [visible])
81+
7682
const getTransitionClass = (state: string) => {
7783
return state === 'entering'
7884
? 'fade'
@@ -85,7 +91,7 @@ export const CTooltip: FC<CTooltipProps> = ({
8591

8692
return (
8793
<>
88-
{React.cloneElement(children, {
94+
{React.cloneElement(children as React.ReactElement<any>, {
8995
ref: setReferenceElement,
9096
...((trigger === 'click' || trigger.includes('click')) && {
9197
onClick: () => setVisible(!_visible),
@@ -121,6 +127,7 @@ export const CTooltip: FC<CTooltipProps> = ({
121127
`tooltip bs-tooltip-${
122128
placement === 'left' ? 'start' : placement === 'right' ? 'end' : placement
123129
}`,
130+
className,
124131
transitionClass,
125132
)}
126133
ref={setPopperElement}
@@ -142,7 +149,7 @@ export const CTooltip: FC<CTooltipProps> = ({
142149
}
143150

144151
CTooltip.propTypes = {
145-
children: PropTypes.any,
152+
children: PropTypes.node,
146153
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
147154
offset: PropTypes.any, // TODO: find good proptype
148155
onHide: PropTypes.func,

0 commit comments

Comments
 (0)