Skip to content

Commit

Permalink
feat: supports classnames & styles (#484)
Browse files Browse the repository at this point in the history
* feat: supports classnames & styles

* fix

* improve test

* update classname
  • Loading branch information
thinkasany authored Nov 29, 2024
1 parent f093bde commit 13d7b7a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className={classNames(`${prefixCls}-content`, className)} style={style}>
<div className={`${prefixCls}-inner`} id={id} role="tooltip" style={overlayInnerStyle}>
<div
className={classNames(`${prefixCls}-inner`, innerClassName)}
id={id}
role="tooltip"
style={innerStyle}
>
{typeof children === 'function' ? children() : children}
</div>
</div>
Expand Down
27 changes: 24 additions & 3 deletions src/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -70,6 +83,8 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
overlay,
id,
showArrow = true,
classNames: tooltipClassNames,
styles: tooltipStyles,
...restProps
} = props;

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

const getPopupElement = () => (
<Popup key="content" prefixCls={prefixCls} id={id} overlayInnerStyle={overlayInnerStyle}>
<Popup
key="content"
prefixCls={prefixCls}
id={id}
innerClassName={tooltipClassNames?.inner}
overlayInnerStyle={{ ...overlayInnerStyle, ...tooltipStyles?.inner }}
>
{overlay}
</Popup>
);

return (
<Trigger
popupClassName={overlayClassName}
popupClassName={classNames(overlayClassName, tooltipClassNames?.root)}
prefixCls={prefixCls}
popup={getPopupElement}
action={trigger}
Expand All @@ -106,7 +127,7 @@ const Tooltip = (props: TooltipProps, ref: React.Ref<TooltipRef>) => {
defaultPopupVisible={defaultVisible}
autoDestroy={destroyTooltipOnHide}
mouseLeaveDelay={mouseLeaveDelay}
popupStyle={overlayStyle}
popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
mouseEnterDelay={mouseEnterDelay}
arrow={showArrow}
{...extraProps}
Expand Down
28 changes: 28 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Tooltip classNames={customClassNames} overlay={<div />} styles={customStyles} visible>
<button />
</Tooltip>,
);

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');
});
});

1 comment on commit 13d7b7a

@vercel
Copy link

@vercel vercel bot commented on 13d7b7a Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tooltip – ./

tooltip-react-component.vercel.app
tooltip-git-master-react-component.vercel.app

Please sign in to comment.