Skip to content

Commit 96d616a

Browse files
authored
chore: cache content when closing (#367)
1 parent 0bb9183 commit 96d616a

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/Popup/PopupContent.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as React from 'react';
2+
3+
export interface PopupContentProps {
4+
children?: React.ReactNode;
5+
cache?: boolean;
6+
}
7+
8+
const PopupContent = React.memo(
9+
({ children }: PopupContentProps) => children as React.ReactElement,
10+
(_, next) => next.cache,
11+
);
12+
13+
if (process.env.NODE_ENV !== 'production') {
14+
PopupContent.displayName = 'PopupContent';
15+
}
16+
17+
export default PopupContent;

src/Popup/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { TriggerProps } from '../';
99
import type { AlignType } from '../interface';
1010
import Arrow from './Arrow';
1111
import Mask from './Mask';
12+
import PopupContent from './PopupContent';
1213

1314
export interface PopupProps {
1415
prefixCls: string;
@@ -229,7 +230,7 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
229230
arrowY={arrowY}
230231
/>
231232
)}
232-
{childNode}
233+
<PopupContent cache={!open}>{childNode}</PopupContent>
233234
</div>
234235
);
235236
}}

tests/motion.test.jsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,44 @@ describe('Trigger.Motion', () => {
100100
pointerEvents: 'none',
101101
});
102102
});
103+
104+
it('no update when close', () => {
105+
const genTrigger = ({ children, ...props }) => (
106+
<Trigger
107+
popup={children}
108+
popupMotion={{
109+
motionName: 'bamboo',
110+
}}
111+
popupVisible
112+
{...props}
113+
>
114+
<span />
115+
</Trigger>
116+
);
117+
118+
const { rerender } = render(
119+
genTrigger({
120+
children: <div className="bamboo" />,
121+
}),
122+
);
123+
124+
expect(document.querySelector('.bamboo')).toBeTruthy();
125+
126+
// rerender when open
127+
rerender(
128+
genTrigger({
129+
children: <div className="little" />,
130+
}),
131+
);
132+
expect(document.querySelector('.little')).toBeTruthy();
133+
134+
// rerender when close
135+
rerender(
136+
genTrigger({
137+
popupVisible: false,
138+
children: <div className="light" />,
139+
}),
140+
);
141+
expect(document.querySelector('.little')).toBeTruthy();
142+
});
103143
});

0 commit comments

Comments
 (0)