Skip to content

feat: supports classnames & styles #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});