From 13d7b7a4a01b55a0e6d3efe3855be6e52bf8436c Mon Sep 17 00:00:00 2001
From: thinkasany <480968828@qq.com>
Date: Fri, 29 Nov 2024 10:57:25 +0800
Subject: [PATCH] feat: supports classnames & styles (#484)
* feat: supports classnames & styles
* fix
* improve test
* update classname
---
src/Popup.tsx | 11 +++++++++--
src/Tooltip.tsx | 27 ++++++++++++++++++++++++---
tests/index.test.tsx | 28 ++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/src/Popup.tsx b/src/Popup.tsx
index cbaa9cf..7e19976 100644
--- a/src/Popup.tsx
+++ b/src/Popup.tsx
@@ -8,14 +8,21 @@ export interface ContentProps {
overlayInnerStyle?: React.CSSProperties;
className?: string;
style?: React.CSSProperties;
+ innerClassName?: string;
}
export default function Popup(props: ContentProps) {
- const { children, prefixCls, id, overlayInnerStyle, className, style } = props;
+ const { children, prefixCls, id, overlayInnerStyle: innerStyle, innerClassName, className, style } =
+ props;
return (
-
+
{typeof children === 'function' ? children() : children}
diff --git a/src/Tooltip.tsx b/src/Tooltip.tsx
index 7f218d6..395019d 100644
--- a/src/Tooltip.tsx
+++ b/src/Tooltip.tsx
@@ -5,6 +5,7 @@ import * as React from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';
import { placements } from './placements';
import Popup from './Popup';
+import classNames from 'classnames';
export interface TooltipProps
extends Pick<
@@ -42,6 +43,18 @@ export interface TooltipProps
id?: string;
overlayInnerStyle?: React.CSSProperties;
zIndex?: number;
+ styles?: TooltipStyles;
+ classNames?: TooltipClassNames;
+}
+
+export interface TooltipStyles {
+ root?: React.CSSProperties;
+ inner?: React.CSSProperties;
+}
+
+export interface TooltipClassNames {
+ root?: string;
+ inner?: string;
}
export interface TooltipRef extends TriggerRef {}
@@ -70,6 +83,8 @@ const Tooltip = (props: TooltipProps, ref: React.Ref
) => {
overlay,
id,
showArrow = true,
+ classNames: tooltipClassNames,
+ styles: tooltipStyles,
...restProps
} = props;
@@ -82,14 +97,20 @@ const Tooltip = (props: TooltipProps, ref: React.Ref) => {
}
const getPopupElement = () => (
-
+
{overlay}
);
return (
) => {
defaultPopupVisible={defaultVisible}
autoDestroy={destroyTooltipOnHide}
mouseLeaveDelay={mouseLeaveDelay}
- popupStyle={overlayStyle}
+ popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
mouseEnterDelay={mouseEnterDelay}
arrow={showArrow}
{...extraProps}
diff --git a/tests/index.test.tsx b/tests/index.test.tsx
index 9541285..917136c 100644
--- a/tests/index.test.tsx
+++ b/tests/index.test.tsx
@@ -251,4 +251,32 @@ describe('rc-tooltip', () => {
expect(nodeRef.current.nativeElement).toBe(container.querySelector('button'));
});
+ it('should apply custom styles to Tooltip', () => {
+ const customClassNames = {
+ inner: 'custom-inner',
+ root: 'custom-root',
+ };
+
+ const customStyles = {
+ inner: { color: 'red' },
+ root: { backgroundColor: 'blue' },
+ };
+
+ const { container } = render(
+ } styles={customStyles} visible>
+
+ ,
+ );
+
+ const tooltipElement = container.querySelector('.rc-tooltip') as HTMLElement;
+ const tooltipInnerElement = container.querySelector('.rc-tooltip-inner') as HTMLElement;
+
+ // 验证 classNames
+ expect(tooltipElement.classList).toContain('custom-root');
+ expect(tooltipInnerElement.classList).toContain('custom-inner');
+
+ // 验证 styles
+ expect(tooltipElement.style.backgroundColor).toBe('blue');
+ expect(tooltipInnerElement.style.color).toBe('red');
+ });
});