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

fix(popover): prevent toggletip ref error #17225

Merged
merged 7 commits into from
Sep 5, 2024
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
22 changes: 18 additions & 4 deletions packages/react/src/components/Popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,26 @@ export const Popover: PopoverComponent = React.forwardRef(

const mappedChildren = React.Children.map(children, (child) => {
const item = child as any;
const displayName = item?.type?.displayName;

/**
* Only trigger elements (button) or trigger components (ToggletipButton) should be
* cloned because these will be decorated with a trigger-specific className and ref.
*
* There are also some specific components that should not be cloned when autoAlign
* is on, even if they are a trigger element.
*/
const isTriggerElement = item?.type === 'button';
const isTriggerComponent =
autoAlign && displayName && ['ToggletipButton'].includes(displayName);
const isAllowedTriggerComponent =
autoAlign &&
displayName &&
!['ToggletipContent', 'PopoverContent'].includes(displayName);

if (
(item?.type === 'button' ||
(autoAlign && item?.type?.displayName !== 'PopoverContent') ||
(autoAlign && item?.type?.displayName === 'ToggletipButton')) &&
React.isValidElement(item)
React.isValidElement(item) &&
(isTriggerElement || isTriggerComponent || isAllowedTriggerComponent)
) {
const className = (item?.props as any)?.className;
const ref = (item?.props as any).ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
const prefix = 'cds';
import React, { forwardRef } from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { Toggletip, ToggletipButton } from '..';
import {
Toggletip,
ToggletipButton,
ToggletipContent,
ToggletipActions,
} from '..';
import { Information } from '@carbon/react/icons';
import userEvent from '@testing-library/user-event';

describe('Toggletip', () => {
Expand Down Expand Up @@ -175,5 +181,26 @@ describe('Toggletip', () => {
);
expect(container.firstChild).not.toHaveClass(`${prefix}--popover--open`);
});

describe('autoAlign', () => {
it('should render without errors when composed with ToggletipButton, ToggletipContent, ToggletipActions', async () => {
render(
<div>
<Toggletip align="bottom" autoAlign defaultOpen>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>Test content</p>
<ToggletipActions>
<a href="#">Link</a>
<button>Button</button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
</div>
);
});
});
});
});
Loading