Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for disabled tooltip callback #1211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ import { Tooltip } from 'react-tooltip';
| `setIsOpen` | `function` | no | | | The tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip |
| `afterShow` | `function` | no | | | A function to be called after the tooltip is shown |
| `afterHide` | `function` | no | | | A function to be called after the tooltip is hidden |
| `disableTooltip` | `function` | no | | | A function that takes a ref to each anchor element and returns a boolean indicating whether to hide the tooltip for that specific anchor. |
| `middlewares` | `Middleware[]` | no | | array of valid `floating-ui` middlewares | Allows for advanced customization. Check the [`floating-ui` docs](https://floating-ui.com/docs/middleware) for more information |
| `border` | CSS border | no | | a CSS border style | Change the style of the tooltip border (including the arrow) |
| `opacity` | CSS opacity | no | `0.9` | a CSS opacity value | Change the opacity of the tooltip |
Expand Down
6 changes: 5 additions & 1 deletion src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const Tooltip = ({
position,
afterShow,
afterHide,
disableTooltip,
gabrieljablonski marked this conversation as resolved.
Show resolved Hide resolved
// props handled by controller
content,
contentWrapperRef,
Expand Down Expand Up @@ -459,11 +460,14 @@ const Tooltip = ({
const elementRefs = new Set(anchorRefs)

anchorsBySelect.forEach((anchor) => {
if (disableTooltip?.(anchor)) {
return
}
elementRefs.add({ current: anchor })
})

const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)
if (anchorById) {
if (anchorById && !disableTooltip?.(anchorById)) {
elementRefs.add({ current: anchorById })
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface ITooltip {
setIsOpen?: (value: boolean) => void
afterShow?: () => void
afterHide?: () => void
disableTooltip?: (anchorRef: HTMLElement | null) => boolean
activeAnchor: HTMLElement | null
setActiveAnchor: (anchor: HTMLElement | null) => void
border?: CSSProperties['border']
Expand Down
2 changes: 2 additions & 0 deletions src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
setIsOpen,
afterShow,
afterHide,
disableTooltip,
role = 'tooltip',
}: ITooltipController,
ref,
Expand Down Expand Up @@ -370,6 +371,7 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
setIsOpen,
afterShow,
afterHide,
disableTooltip,
activeAnchor,
setActiveAnchor: (anchor: HTMLElement | null) => setActiveAnchor(anchor),
role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface ITooltipController {
setIsOpen?: (value: boolean) => void
afterShow?: () => void
afterHide?: () => void
disableTooltip?: (anchorRef: HTMLElement | null) => boolean
role?: React.AriaRole
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/__snapshots__/tooltip-props.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ exports[`tooltip props tooltip with delay show 1`] = `
</div>
`;

exports[`tooltip props tooltip with disableTooltip return false 1`] = `
<div>
<span
data-tooltip-id="example-disableTooltip-false"
>
Lorem Ipsum
</span>
<div
class="react-tooltip react-tooltip__place-top react-tooltip__show"
id="example-disableTooltip-false"
role="tooltip"
style="left: 5px; top: -10px;"
>
Hello World!
<div
class="react-tooltip-arrow"
style="left: -1px; bottom: -4px;"
/>
</div>
</div>
`;

exports[`tooltip props tooltip with float 1`] = `
<div>
<span
Expand Down
31 changes: 31 additions & 0 deletions src/test/tooltip-props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,35 @@ describe('tooltip props', () => {
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})

test('tooltip with disableTooltip return true', async () => {
render(
<TooltipProps
id="example-disableTooltip-true"
content="Hello World!"
disableTooltip={() => true}
/>,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
})

test('tooltip with disableTooltip return false', async () => {
const { container } = render(
<TooltipProps
id="example-disableTooltip-false"
content="Hello World!"
disableTooltip={() => false}
/>,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)

const tooltip = await screen.findByRole('tooltip')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
})
Loading