Skip to content

Commit 4b432b9

Browse files
authored
feat: added polymorphic props to Toggletip (#18756)
* feat: added polymorphic props to Toggletip * fix: remove test story
1 parent bfc320c commit 4b432b9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/react/src/components/Toggletip/__tests__/Toggletip-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ describe('Toggletip', () => {
8282
expect(container.firstChild).not.toHaveClass(`${prefix}--popover--open`);
8383
});
8484

85+
it('should render with custom element using as prop', () => {
86+
const CustomElement = forwardRef((props, ref) => (
87+
<div data-testid="custom-toggletip" ref={ref} {...props} />
88+
));
89+
90+
render(<Toggletip as={CustomElement}>Content</Toggletip>);
91+
expect(screen.getByTestId('custom-toggletip')).toBeInTheDocument();
92+
});
93+
8594
describe('Component API', () => {
8695
it('should support custom elements with the `as` prop', () => {
8796
const CustomComponent = forwardRef((props, ref) => {

packages/react/src/components/Toggletip/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import cx from 'classnames';
9-
import PropTypes from 'prop-types';
9+
import PropTypes, { WeakValidationMap } from 'prop-types';
1010
import React, {
1111
type ElementType,
1212
useContext,
@@ -23,6 +23,7 @@ import { useWindowEvent } from '../../internal/useEvent';
2323
import { useId } from '../../internal/useId';
2424
import { usePrefix } from '../../internal/usePrefix';
2525
import { PolymorphicProps } from '../../types/common';
26+
import { PolymorphicComponentPropWithRef } from '../../internal/PolymorphicProps';
2627

2728
type ToggletipLabelProps<E extends ElementType> = {
2829
as?: E;
@@ -84,16 +85,18 @@ function useToggletip() {
8485
return useContext(ToggletipContext);
8586
}
8687

87-
export interface ToggletipProps<E extends ElementType> {
88+
export interface ToggletipBaseProps {
8889
align?: PopoverAlignment;
8990
alignmentAxisOffset?: number;
90-
as?: E;
9191
autoAlign?: boolean;
9292
className?: string;
9393
children?: ReactNode;
9494
defaultOpen?: boolean;
9595
}
9696

97+
export type ToggletipProps<T extends ElementType> =
98+
PolymorphicComponentPropWithRef<T, ToggletipBaseProps>;
99+
97100
/**
98101
* Used as a container for the button and content of a toggletip. This component
99102
* is responsible for coordinating between interactions with the button and the

0 commit comments

Comments
 (0)