Skip to content

Commit 69623ab

Browse files
authored
feat: add rest parameter to ToggletipLabel (#18601)
1 parent bef47b9 commit 69623ab

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import React from 'react';
9+
import { render, screen } from '@testing-library/react';
10+
import { ToggletipLabel } from '..';
11+
812
describe('ToggletipLabel', () => {
913
describe('Component API', () => {
10-
it.todo('should support custom elements with the `as` prop');
11-
it.todo('should support a custom class name with the `className` prop');
14+
it('should support custom elements with the `as` prop', () => {
15+
render(
16+
<ToggletipLabel as="div" data-testid="toggletip-label">
17+
Label Text
18+
</ToggletipLabel>
19+
);
20+
21+
const label = screen.getByTestId('toggletip-label');
22+
23+
expect(label.tagName).toBe('DIV');
24+
});
25+
26+
it('should support a custom class name with the `className` prop', () => {
27+
const { container } = render(
28+
<ToggletipLabel className="custom-class">Label Text</ToggletipLabel>
29+
);
30+
31+
expect(container.firstChild).toHaveClass('custom-class');
32+
});
33+
34+
it('should forward extra props to the underlying element', () => {
35+
render(
36+
<ToggletipLabel as="p" data-custom="123">
37+
Label Text
38+
</ToggletipLabel>
39+
);
40+
41+
const label = screen.getByText('Label Text');
42+
43+
expect(label).toHaveAttribute('data-custom', '123');
44+
});
1245
});
1346
});

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -28,7 +28,7 @@ type ToggletipLabelProps<E extends ElementType> = {
2828
as?: E;
2929
children?: ReactNode;
3030
className?: string;
31-
};
31+
} & Omit<React.ComponentPropsWithoutRef<E>, 'as' | 'children' | 'className'>;
3232

3333
/**
3434
* Used to render the label for a Toggletip
@@ -37,12 +37,15 @@ export function ToggletipLabel<E extends ElementType>({
3737
as: BaseComponent = 'span' as E,
3838
children,
3939
className: customClassName,
40+
...rest
4041
}: ToggletipLabelProps<E>) {
4142
const prefix = usePrefix();
4243
const className = cx(`${prefix}--toggletip-label`, customClassName);
4344
const BaseComponentAsAny = BaseComponent as any;
4445
return (
45-
<BaseComponentAsAny className={className}>{children}</BaseComponentAsAny>
46+
<BaseComponentAsAny className={className} {...rest}>
47+
{children}
48+
</BaseComponentAsAny>
4649
);
4750
}
4851

0 commit comments

Comments
 (0)