Skip to content

Commit 4b09132

Browse files
committed
chore: clean code
1 parent dbe400e commit 4b09132

File tree

2 files changed

+2
-125
lines changed

2 files changed

+2
-125
lines changed

src/Popup.tsx

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,22 @@ export interface ContentProps {
1111
bodyClassName?: string;
1212
}
1313

14-
const getTextContent = (node: (() => React.ReactNode) | React.ReactNode): string => {
15-
if (!node) {
16-
return '';
17-
}
18-
19-
const resolvedNode = typeof node === 'function' ? node() : node;
20-
21-
if (typeof resolvedNode === 'string' || typeof resolvedNode === 'number') {
22-
return String(resolvedNode);
23-
}
24-
25-
if (Array.isArray(resolvedNode)) {
26-
return resolvedNode.map(getTextContent).join(' ');
27-
}
28-
29-
if (React.isValidElement(resolvedNode)) {
30-
return getTextContent(resolvedNode.props.children);
31-
}
32-
33-
return '';
34-
};
35-
3614
export default function Popup(props: ContentProps) {
3715
const { children, prefixCls, id, overlayInnerStyle: innerStyle, bodyClassName, className, style } =
3816
props;
3917

40-
const tooltipText = getTextContent(children);
41-
4218
return (
4319
<>
4420
<div className={classNames(`${prefixCls}-content`, className)} style={style}>
4521
<div
22+
id={id}
23+
role="tooltip"
4624
className={classNames(`${prefixCls}-inner`, bodyClassName)}
4725
style={innerStyle}
4826
>
4927
{typeof children === 'function' ? children() : children}
5028
</div>
5129
</div>
52-
{tooltipText && (
53-
<div
54-
id={id}
55-
role="tooltip"
56-
style={{ width: 0, height: 0, position: 'absolute', overflow: 'hidden', opacity: 0 }}
57-
>
58-
{tooltipText}
59-
</div>
60-
)}
6130
</>
6231
);
6332
}

tests/popup.test.tsx

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,8 @@
1-
import React from 'react';
2-
import { render } from '@testing-library/react';
31
import Popup from '../src/Popup';
42

53
describe('Popup', () => {
64
// Used in antd for C2D2C
75
it('should export', () => {
86
expect(Popup).toBeTruthy();
97
});
10-
11-
it('should correctly extract text from string, number, function, and element', () => {
12-
const { getByRole } = render(
13-
<Popup prefixCls="test" id="popup-id">
14-
{() => (
15-
<>
16-
{'Hello'}
17-
{123}
18-
<span>World</span>
19-
</>
20-
)}
21-
</Popup>,
22-
);
23-
24-
const tooltip = getByRole('tooltip');
25-
const hiddenTextContainer = tooltip.querySelector('div > div');
26-
27-
expect(hiddenTextContainer.textContent).toBe('Hello 123 World');
28-
});
29-
30-
it('should apply updated hidden text styles correctly', () => {
31-
const { getByRole } = render(
32-
<Popup prefixCls="test" id="popup-id">
33-
test hidden text
34-
</Popup>,
35-
);
36-
37-
const tooltip = getByRole('tooltip');
38-
const hiddenTextContainer = tooltip.querySelector('div > div');
39-
40-
expect(hiddenTextContainer).toHaveStyle({
41-
width: '0',
42-
height: '0',
43-
position: 'absolute',
44-
overflow: 'hidden',
45-
opacity: '0',
46-
});
47-
});
48-
49-
it('should return empty string if children is null or undefined', () => {
50-
const { getByRole } = render(
51-
<Popup prefixCls="test" id="popup-empty">
52-
{null}
53-
</Popup>,
54-
);
55-
const tooltip = getByRole('tooltip');
56-
57-
expect(tooltip.querySelector('div > div')).toBeNull();
58-
});
59-
60-
it('should handle nested arrays correctly', () => {
61-
const { getByRole } = render(
62-
<Popup prefixCls="test" id="popup-nested">
63-
{[
64-
'First',
65-
['Second', 'Third'],
66-
<span key="fourth">Fourth</span>,
67-
]}
68-
</Popup>,
69-
);
70-
const tooltip = getByRole('tooltip');
71-
const hiddenTextContainer = tooltip.querySelector('div > div');
72-
73-
// "First Second Third Fourth"
74-
expect(hiddenTextContainer.textContent).toBe('First Second Third Fourth');
75-
});
76-
77-
it('should handle function returning an array', () => {
78-
const { getByRole } = render(
79-
<Popup prefixCls="test" id="popup-func-array">
80-
{() => ['Alpha', <span key="beta">Beta</span>]}
81-
</Popup>,
82-
);
83-
const tooltip = getByRole('tooltip');
84-
const hiddenTextContainer = tooltip.querySelector('div > div');
85-
86-
// "Alpha Beta"
87-
expect(hiddenTextContainer.textContent).toBe('Alpha Beta');
88-
});
89-
90-
it('should handle function returning undefined', () => {
91-
const { getByRole } = render(
92-
<Popup prefixCls="test" id="popup-func-undefined">
93-
{() => undefined}
94-
</Popup>,
95-
);
96-
const tooltip = getByRole('tooltip');
97-
98-
expect(tooltip.querySelector('div > div')).toBeNull();
99-
});
1008
});

0 commit comments

Comments
 (0)