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 2 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;
overlayInnerClassName?: string;
Copy link
Member

Choose a reason for hiding this comment

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

这个要废弃的,不加新的了吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个是为了让 classnames.inner 的class进入到 `${prefixCls}-inner` 这边的class里面,原来已经暴露了一个classname用于content, popup 在其他地方另外有使用。 这个api可以在antd不对外暴露,不写入类型,推荐用户使用classnames.inner
image
image

Copy link
Member

Choose a reason for hiding this comment

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

overlayInnerClassName 和 overlayInnerStyle 改为 innerStyle 和 innerClassName 吧,overlay 在这里是多余的语义。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

overlayInnerStyle 是历史遗留问题,要改的话也只能变成 const { overlayInnerStyle: innerStyle } = props;
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

innerClassName 改好了,innerStyle 只能用别名的方式来了,毕竟popup这个组件导出在外部组件可用,不太能动了,不然可能引发break

}

export default function Popup(props: ContentProps) {
const { children, prefixCls, id, overlayInnerStyle, className, style } = props;
const { children, prefixCls, id, overlayInnerStyle, overlayInnerClassName, 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`, overlayInnerClassName)}
id={id}
role="tooltip"
style={overlayInnerStyle}
>
{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}
overlayInnerClassName={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
31 changes: 31 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Tooltip classNames={customClassNames} overlay={<div />} styles={customStyles} visible>
<button />
</Tooltip>,
);

const tooltipElement = container.querySelector('.rc-tooltip');
const tooltipInnerElement = container.querySelector('.rc-tooltip-inner');

// 验证 classNames
expect(tooltipElement.classList).toContain('custom-root');
expect(tooltipInnerElement.classList).toContain('custom-inner');

// 验证 styles
const tooltipElementStyle = getComputedStyle(tooltipElement);
const tooltipInnerElementStyle = getComputedStyle(tooltipInnerElement);

expect(tooltipElementStyle.backgroundColor).toBe('blue');
expect(tooltipInnerElementStyle.color).toBe('red');
});
});