Skip to content
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
2 changes: 1 addition & 1 deletion src/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
const getChildren: TriggerProps['children'] = ({ open }) => {
const child = React.Children.only(children);
const childAriaDescribedBy = (child.props as React.AriaAttributes)['aria-describedby'];
const ariaDescribedBy = [childAriaDescribedBy, overlay && open ? mergedId : undefined]
const ariaDescribedBy = [childAriaDescribedBy, overlay != null && open ? mergedId : undefined]
.filter(Boolean)
.join(' ');
const ariaProps: React.AriaAttributes = {
Expand Down
29 changes: 29 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,35 @@ describe('rc-tooltip', () => {
expect(trigger).toHaveAttribute('aria-describedby', 'existing-description');
});

it('should set aria-describedby when overlay is a falsy but valid node like 0', () => {
const { container } = render(
<Tooltip overlay={0} visible>
<button>Click me</button>
</Tooltip>,
);

const trigger = container.querySelector('button');
const describedBy = trigger.getAttribute('aria-describedby');
expect(describedBy).toBeTruthy();
expect(document.getElementById(describedBy)).toHaveTextContent('0');
});

it('should not set aria-describedby when overlay is null or undefined', () => {
const { container: nullContainer } = render(
<Tooltip overlay={null} visible>
<button>Click me</button>
</Tooltip>,
);
expect(nullContainer.querySelector('button')).not.toHaveAttribute('aria-describedby');

const { container: undefinedContainer } = render(
<Tooltip overlay={undefined} visible>
<button>Click me</button>
</Tooltip>,
);
expect(undefinedContainer.querySelector('button')).not.toHaveAttribute('aria-describedby');
});

it('should preserve original props of children', () => {
const onMouseEnter = jest.fn();

Expand Down
Loading