Skip to content

Commit 13d7b7a

Browse files
authored
feat: supports classnames & styles (#484)
* feat: supports classnames & styles * fix * improve test * update classname
1 parent f093bde commit 13d7b7a

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/Popup.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ export interface ContentProps {
88
overlayInnerStyle?: React.CSSProperties;
99
className?: string;
1010
style?: React.CSSProperties;
11+
innerClassName?: string;
1112
}
1213

1314
export default function Popup(props: ContentProps) {
14-
const { children, prefixCls, id, overlayInnerStyle, className, style } = props;
15+
const { children, prefixCls, id, overlayInnerStyle: innerStyle, innerClassName, className, style } =
16+
props;
1517

1618
return (
1719
<div className={classNames(`${prefixCls}-content`, className)} style={style}>
18-
<div className={`${prefixCls}-inner`} id={id} role="tooltip" style={overlayInnerStyle}>
20+
<div
21+
className={classNames(`${prefixCls}-inner`, innerClassName)}
22+
id={id}
23+
role="tooltip"
24+
style={innerStyle}
25+
>
1926
{typeof children === 'function' ? children() : children}
2027
</div>
2128
</div>

src/Tooltip.tsx

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as React from 'react';
55
import { forwardRef, useImperativeHandle, useRef } from 'react';
66
import { placements } from './placements';
77
import Popup from './Popup';
8+
import classNames from 'classnames';
89

910
export interface TooltipProps
1011
extends Pick<
@@ -42,6 +43,18 @@ export interface TooltipProps
4243
id?: string;
4344
overlayInnerStyle?: React.CSSProperties;
4445
zIndex?: number;
46+
styles?: TooltipStyles;
47+
classNames?: TooltipClassNames;
48+
}
49+
50+
export interface TooltipStyles {
51+
root?: React.CSSProperties;
52+
inner?: React.CSSProperties;
53+
}
54+
55+
export interface TooltipClassNames {
56+
root?: string;
57+
inner?: string;
4558
}
4659

4760
export interface TooltipRef extends TriggerRef {}
@@ -70,6 +83,8 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
7083
overlay,
7184
id,
7285
showArrow = true,
86+
classNames: tooltipClassNames,
87+
styles: tooltipStyles,
7388
...restProps
7489
} = props;
7590

@@ -82,14 +97,20 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
8297
}
8398

8499
const getPopupElement = () => (
85-
<Popup key="content" prefixCls={prefixCls} id={id} overlayInnerStyle={overlayInnerStyle}>
100+
<Popup
101+
key="content"
102+
prefixCls={prefixCls}
103+
id={id}
104+
innerClassName={tooltipClassNames?.inner}
105+
overlayInnerStyle={{ ...overlayInnerStyle, ...tooltipStyles?.inner }}
106+
>
86107
{overlay}
87108
</Popup>
88109
);
89110

90111
return (
91112
<Trigger
92-
popupClassName={overlayClassName}
113+
popupClassName={classNames(overlayClassName, tooltipClassNames?.root)}
93114
prefixCls={prefixCls}
94115
popup={getPopupElement}
95116
action={trigger}
@@ -106,7 +127,7 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
106127
defaultPopupVisible={defaultVisible}
107128
autoDestroy={destroyTooltipOnHide}
108129
mouseLeaveDelay={mouseLeaveDelay}
109-
popupStyle={overlayStyle}
130+
popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
110131
mouseEnterDelay={mouseEnterDelay}
111132
arrow={showArrow}
112133
{...extraProps}

tests/index.test.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,32 @@ describe('rc-tooltip', () => {
251251

252252
expect(nodeRef.current.nativeElement).toBe(container.querySelector('button'));
253253
});
254+
it('should apply custom styles to Tooltip', () => {
255+
const customClassNames = {
256+
inner: 'custom-inner',
257+
root: 'custom-root',
258+
};
259+
260+
const customStyles = {
261+
inner: { color: 'red' },
262+
root: { backgroundColor: 'blue' },
263+
};
264+
265+
const { container } = render(
266+
<Tooltip classNames={customClassNames} overlay={<div />} styles={customStyles} visible>
267+
<button />
268+
</Tooltip>,
269+
);
270+
271+
const tooltipElement = container.querySelector('.rc-tooltip') as HTMLElement;
272+
const tooltipInnerElement = container.querySelector('.rc-tooltip-inner') as HTMLElement;
273+
274+
// 验证 classNames
275+
expect(tooltipElement.classList).toContain('custom-root');
276+
expect(tooltipInnerElement.classList).toContain('custom-inner');
277+
278+
// 验证 styles
279+
expect(tooltipElement.style.backgroundColor).toBe('blue');
280+
expect(tooltipInnerElement.style.color).toBe('red');
281+
});
254282
});

0 commit comments

Comments
 (0)