From be4c18d3dd6e5aeae0129e04e775cae05516a476 Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Thu, 28 Nov 2024 17:40:44 +0800 Subject: [PATCH 1/4] feat: supports classnames & styles --- src/Popup.tsx | 11 +++++++++-- src/Tooltip.tsx | 27 ++++++++++++++++++++++++--- tests/index.test.tsx | 31 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/Popup.tsx b/src/Popup.tsx index cbaa9cf..361dc72 100644 --- a/src/Popup.tsx +++ b/src/Popup.tsx @@ -8,14 +8,21 @@ export interface ContentProps { overlayInnerStyle?: React.CSSProperties; className?: string; style?: React.CSSProperties; + overlayInnerClassName?: string; } export default function Popup(props: ContentProps) { - const { children, prefixCls, id, overlayInnerStyle, className, style } = props; + const { children, prefixCls, id, overlayInnerStyle, overlayInnerClassName, className, style } = + props; return (
- diff --git a/src/Tooltip.tsx b/src/Tooltip.tsx index 7f218d6..dbe1c19 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?: TootipStyles; + classNames?: TooltipClassNames; +} + +export interface TootipStyles { + 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..47efcfb 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -251,4 +251,35 @@ 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> +