11import React from 'react' ;
2- import { Modal , Dimensions , VirtualizedList } from 'react-native' ;
2+ import { Modal , Dimensions , VirtualizedList , View } from 'react-native' ;
33import Image from './Image' ;
44import { ImageViewerImageProps , ImageViewerComponentProps , ImageViewerComponentState , SwipeDirection } from './types' ;
55
66const SCREEN_WIDTH = Dimensions . get ( 'window' ) . width ;
77
88class ImageViewer extends React . Component < ImageViewerComponentProps , ImageViewerComponentState > {
99 state : ImageViewerComponentState = {
10- visible : false ,
11- images : [ ] ,
1210 scrollEnabled : true ,
13- startIndex : 0 ,
1411 } ;
15- private _scrollRef : { current : VirtualizedList < any > | null } = React . createRef ( ) ;
12+ static defaultProps = {
13+ animationType : 'none' ,
14+ } ;
1615
17- public show ( images : ImageViewerImageProps [ ] , startIndex : number = 0 ) {
18- this . setState ( { images, visible : true , startIndex} ) ;
19- }
16+ private _scrollRef : { current : any } = React . createRef ( ) ;
2017
21- private _closeInternal = ( ) => this . setState ( { visible : false } ) ;
18+ private _closeInternal = ( ) => this . props . onClose ( ) ;
2219 private _renderImage = ( info : { item : ImageViewerImageProps ; index : number } ) => (
2320 < Image
2421 source = { info . item . source }
2522 title = { info . item . title }
2623 onClose = { this . _closeInternal }
27- toggleEnableScroll = { ( enabled : boolean ) => this . setState ( { scrollEnabled : enabled } ) }
24+ toggleEnableScroll = { this . _handleToggleScrollState }
2825 imageIndex = { info . index }
29- imageTotal = { this . _getItemCount ( ) }
26+ imagesTotal = { this . _getItemCount ( ) }
3027 // extendable props
3128 debug = { this . props . debug }
3229 initialWidth = { this . props . imageProps ?. initialWidth }
@@ -35,36 +32,42 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
3532 />
3633 ) ;
3734
38- private _getItemCount = ( ) => this . state . images . length ;
35+ private _handleToggleScrollState = ( enabled : boolean ) => {
36+ this . setState ( { scrollEnabled : enabled } ) ;
37+ } ;
38+
39+ private _getItemCount = ( ) => this . props . images . length ;
3940 private _getItem = ( data : any , index : number ) => data [ index ] ;
4041 private _getItemLayout = ( _data : any , index : number ) => ( {
4142 length : SCREEN_WIDTH ,
4243 offset : SCREEN_WIDTH * index ,
4344 index,
4445 } ) ;
4546
46- private _keyExtractor = ( item : ImageViewerImageProps , index : number ) => `${ index } ` ;
47+ private _keyExtractor = ( _item : ImageViewerImageProps , index : number ) => `${ index } ` ;
4748
4849 render ( ) {
4950 return (
50- < Modal visible = { this . state . visible } transparent animationType = "none" onRequestClose = { this . _closeInternal } >
51+ < Modal visible = { this . props . visible } transparent animationType = { this . props . animationType } onRequestClose = { this . _closeInternal } >
5152 < VirtualizedList
5253 horizontal
5354 showsHorizontalScrollIndicator = { false }
5455 windowSize = { 2 }
55- data = { this . state . images }
56+ data = { this . props . images }
5657 renderItem = { this . _renderImage }
5758 keyExtractor = { this . _keyExtractor }
5859 getItemCount = { this . _getItemCount }
5960 getItem = { this . _getItem }
6061 getItemLayout = { this . _getItemLayout }
61- scrollEnabled = { this . state . scrollEnabled }
62+ scrollEnabled
6263 ref = { this . _scrollRef }
6364 removeClippedSubviews = { true }
6465 maxToRenderPerBatch = { 2 }
6566 initialNumToRender = { 2 }
6667 pagingEnabled
67- initialScrollIndex = { this . state . startIndex }
68+ initialScrollIndex = { this . props . initialIndex !== undefined ? this . props . initialIndex : 0 }
69+ listKey = { 'RNImageViewer' }
70+ disableScrollViewPanResponder = { ! this . state . scrollEnabled }
6871 />
6972 </ Modal >
7073 ) ;
0 commit comments