@@ -2,7 +2,7 @@ import React, { MouseEvent, useRef, useEffect } from 'react';
22import { CloseIcon , DeleteIcon } from 'tdesign-icons-react' ;
33import { CSSTransition } from 'react-transition-group' ;
44import { CSSTransitionClassNames } from 'react-transition-group/CSSTransition' ;
5- import { TdImageViewerProps , ImageInfo } from './type' ;
5+ import { TdImageViewerProps , ImageInfo , ImageViewerCloseTrigger } from './type' ;
66import { StyledProps } from '../common' ;
77import { usePrefixClass } from '../hooks/useClass' ;
88import useDefault from '../_util/useDefault' ;
@@ -64,11 +64,22 @@ const ImageViewer: React.FC<ImageViewerProps> = (props) => {
6464 } , [ show ] ) ;
6565
6666 const imageViewerClass = usePrefixClass ( 'image-viewer' ) ;
67- const handleClose = ( e : MouseEvent , trigger : 'overlay' | 'close-btn' ) => {
67+ const handleClose = ( e : MouseEvent , trigger : ImageViewerCloseTrigger ) => {
6868 e . stopPropagation ( ) ;
6969 setShow ( false ) ;
70- onClose ?.( { trigger, visible : false , index } ) ;
70+ onClose ?.( { trigger, visible : false , index : currentIndex } ) ;
7171 } ;
72+
73+ // 通过(事件委托)将点击事件绑定到组件根,判断点击目标
74+ const handleClick = ( e : MouseEvent ) => {
75+ const target = e . target as HTMLElement ;
76+ if ( target . tagName === 'IMG' ) {
77+ handleClose ( e , 'image' ) ;
78+ } else {
79+ handleClose ( e , 'overlay' ) ;
80+ }
81+ } ;
82+
7283 const handleDelete = ( ) => {
7384 onDelete ?.( currentIndex ?? 0 ) ;
7485 } ;
@@ -207,8 +218,8 @@ const ImageViewer: React.FC<ImageViewerProps> = (props) => {
207218 classNames = { animationClassNames }
208219 nodeRef = { rootRef }
209220 >
210- < div ref = { rootRef } className = { imageViewerClass } >
211- < div className = { `${ imageViewerClass } __mask` } onClick = { ( e ) => handleClose ( e , 'overlay' ) } > </ div >
221+ < div ref = { rootRef } className = { imageViewerClass } onClick = { handleClick } >
222+ < div className = { `${ imageViewerClass } __mask` } > </ div >
212223 < TSwiper
213224 ref = { swiperRootRef }
214225 autoplay = { false }
0 commit comments