Skip to content

Commit 96c6fed

Browse files
committed
fix: false positive content prevention and better typing for it
1 parent 40d6de1 commit 96c6fed

5 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ const Tooltip = ({
933933
}, [delayShow, handleShowTooltipDelayed])
934934

935935
const actualContent = imperativeOptions?.content ?? content
936+
const hasContent = actualContent !== null && actualContent !== undefined
936937
const canShow = show && Object.keys(computedPosition.tooltipStyles).length > 0
937938

938939
useImperativeHandle(forwardRef, () => ({
@@ -964,7 +965,7 @@ const Tooltip = ({
964965
},
965966
activeAnchor,
966967
place: computedPosition.place,
967-
isOpen: Boolean(rendered && !hidden && actualContent && canShow),
968+
isOpen: Boolean(rendered && !hidden && hasContent && canShow),
968969
}))
969970

970971
useEffect(() => {
@@ -976,7 +977,7 @@ const Tooltip = ({
976977
}
977978
}, [])
978979

979-
return rendered && !hidden && actualContent ? (
980+
return rendered && !hidden && hasContent ? (
980981
<WrapperElement
981982
id={id}
982983
role={role}

src/components/TooltipController/TooltipController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
275275
{rendered}
276276
</div>
277277
) : null
278-
} else if (tooltipContent) {
278+
} else if (tooltipContent !== null && tooltipContent !== undefined) {
279279
renderedContent = tooltipContent
280280
}
281281

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type {
1515
export interface ITooltipController {
1616
className?: string
1717
classNameArrow?: string
18-
content?: string
19-
render?: (render: { content: string | null; activeAnchor: HTMLElement | null }) => ReactNode
18+
content?: ReactNode
19+
render?: (render: { content: ReactNode | null; activeAnchor: HTMLElement | null }) => ReactNode
2020
place?: PlacesType
2121
offset?: number
2222
id?: string

src/test/tooltip-imperative.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,33 @@ describe('tooltip imperative API', () => {
146146
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
147147
})
148148
})
149+
150+
test('imperative ref reports open for numeric zero content', async () => {
151+
let tooltipRefInstance = null
152+
153+
const TestComponent = () => {
154+
const tooltipRef = useRef(null)
155+
tooltipRefInstance = tooltipRef
156+
157+
return (
158+
<>
159+
<button onClick={() => tooltipRef.current?.open()}>Open Zero</button>
160+
<span data-tooltip-id="imperative-zero">Target</span>
161+
<Tooltip id="imperative-zero" content={0} ref={tooltipRef} />
162+
</>
163+
)
164+
}
165+
166+
render(<TestComponent />)
167+
168+
await userEvent.click(screen.getByText('Open Zero'))
169+
170+
const tooltip = await screen.findByRole('tooltip')
171+
172+
expect(tooltip).toBeInTheDocument()
173+
expect(tooltip).toHaveTextContent('0')
174+
await waitFor(() => {
175+
expect(tooltipRefInstance.current?.isOpen).toBe(true)
176+
})
177+
})
149178
})

src/test/tooltip-props.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,16 @@ describe('tooltip props', () => {
199199
expect(tooltip).toBeInTheDocument()
200200
expect(container).toMatchSnapshot()
201201
})
202+
203+
test('tooltip renders numeric zero content', async () => {
204+
render(<TooltipProps id="example-zero-content" content={0} />)
205+
const anchorElement = screen.getByText('Lorem Ipsum')
206+
207+
await userEvent.hover(anchorElement)
208+
209+
const tooltip = await screen.findByRole('tooltip')
210+
211+
expect(tooltip).toBeInTheDocument()
212+
expect(tooltip).toHaveTextContent('0')
213+
})
202214
})

0 commit comments

Comments
 (0)