File tree Expand file tree Collapse file tree 2 files changed +31
-5
lines changed
Expand file tree Collapse file tree 2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -328,20 +328,30 @@ const Preview: React.FC<PreviewProps> = props => {
328328 } ;
329329
330330 const escClosingRef = useRef ( false ) ;
331+ const openRef = useRef ( open ) ;
332+ openRef . current = open ;
331333
332334 // >>>>> Effect: Keyboard
333335 const onKeyDown = useEvent ( ( event : KeyboardEvent ) => {
334- if ( open ) {
336+ if ( openRef . current ) {
335337 const { keyCode, key } = event ;
336338
337339 if ( keyCode === KeyCode . ESC || key === 'Escape' ) {
340+ if ( escClosingRef . current ) {
341+ return ;
342+ }
343+
338344 escClosingRef . current = true ;
345+ openRef . current = false ;
339346 event . preventDefault ( ) ;
347+ if ( keyCode === KeyCode . ESC ) {
348+ event . stopPropagation ( ) ;
349+ }
340350 onClose ?.( ) ;
341351
342- Promise . resolve ( ) . then ( ( ) => {
352+ setTimeout ( ( ) => {
343353 escClosingRef . current = false ;
344- } ) ;
354+ } , 0 ) ;
345355 return ;
346356 }
347357
Original file line number Diff line number Diff line change @@ -57,14 +57,30 @@ describe('Preview portal esc fallback', () => {
5757 expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
5858 } ) ;
5959
60- it ( 'avoids duplicate close when keydown esc already handled' , ( ) => {
60+ it ( 'avoids duplicate close when keydown esc already handled (key only) ' , ( ) => {
6161 const onClose = jest . fn ( ) ;
6262
6363 render (
6464 < Preview prefixCls = "rc-image-preview" open src = "x" mousePosition = { null } onClose = { onClose } /> ,
6565 ) ;
6666
67- fireEvent . keyDown ( window , { key : 'Escape' , keyCode : 27 } ) ;
67+ fireEvent . keyDown ( window , { key : 'Escape' } ) ;
68+
69+ act ( ( ) => {
70+ ( global as any ) . __portalProps . onEsc ( { top : true } ) ;
71+ } ) ;
72+
73+ expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
74+ } ) ;
75+
76+ it ( 'avoids duplicate close when keydown esc already handled (keyCode only)' , ( ) => {
77+ const onClose = jest . fn ( ) ;
78+
79+ render (
80+ < Preview prefixCls = "rc-image-preview" open src = "x" mousePosition = { null } onClose = { onClose } /> ,
81+ ) ;
82+
83+ fireEvent . keyDown ( window , { keyCode : 27 } ) ;
6884
6985 act ( ( ) => {
7086 ( global as any ) . __portalProps . onEsc ( { top : true } ) ;
You can’t perform that action at this time.
0 commit comments