Skip to content

Commit b6343cb

Browse files
committed
refactor: remove useless structure
1 parent 1f56aa5 commit b6343cb

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

src/Popup.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,16 @@ export interface ContentProps {
1212
}
1313

1414
const Popup: React.FC<ContentProps> = (props) => {
15-
const {
16-
children,
17-
prefixCls,
18-
id,
19-
overlayInnerStyle: innerStyle,
20-
bodyClassName,
21-
className,
22-
style,
23-
} = props;
15+
const { children, prefixCls, id, className, style, bodyClassName, overlayInnerStyle } = props;
2416

2517
return (
26-
<div className={classNames(`${prefixCls}-content`, className)} style={style}>
27-
<div
28-
className={classNames(`${prefixCls}-inner`, bodyClassName)}
29-
id={id}
30-
role="tooltip"
31-
style={innerStyle}
32-
>
33-
{typeof children === 'function' ? children() : children}
34-
</div>
18+
<div
19+
id={id}
20+
className={classNames(`${prefixCls}-body`, className, bodyClassName)}
21+
style={{ ...overlayInnerStyle, ...style }}
22+
role="tooltip"
23+
>
24+
{typeof children === 'function' ? children() : children}
3525
</div>
3626
);
3727
};

src/Tooltip.tsx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useImperativeHandle, useRef } from 'react';
88
import { placements } from './placements';
99
import Popup from './Popup';
1010

11+
export type SemanticName = 'root' | 'arrow' | 'body';
12+
1113
export interface TooltipProps
1214
extends Pick<
1315
TriggerProps,
@@ -21,47 +23,46 @@ export interface TooltipProps
2123
| 'forceRender'
2224
| 'popupVisible'
2325
> {
26+
// Style
27+
classNames?: Partial<Record<SemanticName, string>>;
28+
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
29+
30+
/** Config popup motion */
31+
motion?: TriggerProps['popupMotion'];
32+
33+
/** @deprecated Please use `styles={{ root: {} }}` */
34+
overlayStyle?: React.CSSProperties;
35+
/** @deprecated Please use `classNames={{ root: '' }}` */
36+
overlayClassName?: string;
37+
/** @deprecated Please use `styles={{ body: {} }}` */
38+
overlayInnerStyle?: React.CSSProperties;
39+
40+
// Rest
2441
trigger?: ActionType | ActionType[];
2542
defaultVisible?: boolean;
2643
visible?: boolean;
2744
placement?: string;
28-
/** Config popup motion */
29-
motion?: TriggerProps['popupMotion'];
45+
3046
onVisibleChange?: (visible: boolean) => void;
3147
afterVisibleChange?: (visible: boolean) => void;
3248
overlay: (() => React.ReactNode) | React.ReactNode;
33-
/** @deprecated Please use `styles={{ root: {} }}` */
34-
overlayStyle?: React.CSSProperties;
35-
/** @deprecated Please use `classNames={{ root: '' }}` */
36-
overlayClassName?: string;
49+
3750
getTooltipContainer?: (node: HTMLElement) => HTMLElement;
3851
destroyOnHidden?: boolean;
3952
align?: AlignType;
4053
showArrow?: boolean | ArrowType;
4154
arrowContent?: React.ReactNode;
4255
id?: string;
43-
/** @deprecated Please use `styles={{ body: {} }}` */
44-
overlayInnerStyle?: React.CSSProperties;
56+
4557
zIndex?: number;
46-
styles?: TooltipStyles;
47-
classNames?: TooltipClassNames;
58+
4859
/**
4960
* Configures Tooltip to reuse the background for transition usage.
5061
* This is an experimental API and may not be stable.
5162
*/
5263
unique?: TriggerProps['unique'];
5364
}
5465

55-
export interface TooltipStyles {
56-
root?: React.CSSProperties;
57-
body?: React.CSSProperties;
58-
}
59-
60-
export interface TooltipClassNames {
61-
root?: string;
62-
body?: string;
63-
}
64-
6566
export interface TooltipRef extends TriggerRef {}
6667

6768
const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
@@ -108,7 +109,8 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
108109
prefixCls={prefixCls}
109110
id={mergedId}
110111
bodyClassName={tooltipClassNames?.body}
111-
overlayInnerStyle={{ ...overlayInnerStyle, ...tooltipStyles?.body }}
112+
overlayInnerStyle={overlayInnerStyle}
113+
style={tooltipStyles?.body}
112114
>
113115
{overlay}
114116
</Popup>
@@ -124,6 +126,23 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
124126
return React.cloneElement<any>(children, childProps) as any;
125127
};
126128

129+
// Process arrow configuration
130+
const getArrowConfig = () => {
131+
if (!showArrow) {
132+
return false;
133+
}
134+
135+
// Convert true to object for unified processing
136+
const arrowConfig = showArrow === true ? {} : showArrow;
137+
138+
// Apply semantic styles with unified logic
139+
return {
140+
...arrowConfig,
141+
className: classNames(arrowConfig.className, tooltipClassNames?.arrow),
142+
content: arrowConfig.content || arrowContent,
143+
};
144+
};
145+
127146
return (
128147
<Trigger
129148
popupClassName={classNames(overlayClassName, tooltipClassNames?.root)}
@@ -143,7 +162,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => {
143162
mouseLeaveDelay={mouseLeaveDelay}
144163
popupStyle={{ ...overlayStyle, ...tooltipStyles?.root }}
145164
mouseEnterDelay={mouseEnterDelay}
146-
arrow={showArrow}
165+
arrow={getArrowConfig()}
147166
{...extraProps}
148167
>
149168
{getChildren()}

tests/index.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render } from '@testing-library/react';
22
import React from 'react';
3-
import Tooltip, { TooltipRef } from '../src';
3+
import Tooltip, { type TooltipRef } from '../src';
44

55
const verifyContent = (wrapper: HTMLElement, content: string) => {
66
expect(wrapper.querySelector('.x-content').textContent).toBe(content);
@@ -73,7 +73,7 @@ describe('rc-tooltip', () => {
7373
);
7474
fireEvent.click(container.querySelector('.target'));
7575
expect(
76-
(container.querySelector('.rc-tooltip-inner') as HTMLElement).style.background,
76+
(container.querySelector('.rc-tooltip-body') as HTMLElement).style.background,
7777
).toEqual('red');
7878
});
7979

@@ -269,11 +269,11 @@ describe('rc-tooltip', () => {
269269
);
270270

271271
const tooltipElement = container.querySelector('.rc-tooltip') as HTMLElement;
272-
const tooltipBodyElement = container.querySelector('.rc-tooltip-inner') as HTMLElement;
272+
const tooltipBodyElement = container.querySelector('.rc-tooltip-body') as HTMLElement;
273273

274274
// 验证 classNames
275-
expect(tooltipElement.classList).toContain('custom-root');
276-
expect(tooltipBodyElement.classList).toContain('custom-body');
275+
expect(tooltipElement).toHaveClass('custom-root');
276+
expect(tooltipBodyElement).toHaveClass('custom-body');
277277

278278
// 验证 styles
279279
expect(tooltipElement.style.backgroundColor).toBe('blue');

0 commit comments

Comments
 (0)