Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CustomImageComponent #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ImageViewing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Props = {
delayLongPress?: number;
HeaderComponent?: ComponentType<{ imageIndex: number }>;
FooterComponent?: ComponentType<{ imageIndex: number }>;
CustomImageComponent?: ComponentType;
};

const DEFAULT_ANIMATION_TYPE = "fade";
Expand All @@ -66,6 +67,7 @@ function ImageViewing({
delayLongPress = DEFAULT_DELAY_LONG_PRESS,
HeaderComponent,
FooterComponent,
CustomImageComponent,
}: Props) {
const imageList = useRef<VirtualizedList<ImageSource>>(null);
const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);
Expand Down Expand Up @@ -140,6 +142,7 @@ function ImageViewing({
delayLongPress={delayLongPress}
swipeToCloseEnabled={swipeToCloseEnabled}
doubleTapToZoomEnabled={doubleTapToZoomEnabled}
CustomImageComponent={CustomImageComponent}
/>
)}
onMomentumScrollEnd={onScroll}
Expand Down
13 changes: 5 additions & 8 deletions src/components/ImageItem/ImageItem.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import React, { useCallback, useRef, useState } from "react";
import React, { useCallback, useRef, useState, ComponentType } from "react";

import {
Animated,
Expand Down Expand Up @@ -39,6 +39,7 @@ type Props = {
delayLongPress: number;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
CustomImageComponent?: ComponentType;
};

const ImageItem = ({
Expand All @@ -49,6 +50,7 @@ const ImageItem = ({
delayLongPress,
swipeToCloseEnabled = true,
doubleTapToZoomEnabled = true,
CustomImageComponent = null
}: Props) => {
const imageContainer = useRef<ScrollView & NativeMethodsMixin>(null);
const imageDimensions = useImageDimensions(imageSrc);
Expand Down Expand Up @@ -131,13 +133,8 @@ const ImageItem = ({
onScrollEndDrag,
})}
>
<Animated.Image
{...panHandlers}
source={imageSrc}
style={imageStylesWithOpacity}
onLoad={onLoaded}
/>
{(!isLoaded || !imageDimensions) && <ImageLoading />}
{CustomImageComponent ? (React.createElement(CustomImageComponent, {panHandlers:{...panHandlers}, source:{imageSrc}, style: {imageStylesWithOpacity}, onLoad:{onLoaded}, loadingImage: <ImageLoading />})) : (<Animated.Image {...panHandlers} source={imageSrc} style={imageStylesWithOpacity} onLoad={onLoaded}/>)}
Copy link

@Rhilip Rhilip Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have try your patch and I wonder if some typo exist when use React.createElement,
It might be React.createElement(CustomImageComponent, {panHandlers:{...panHandlers}, source:imageSrc, style: imageStylesWithOpacity, onLoad:onLoaded, loadingImage: <ImageLoading />}) , no extra { or } around source, style and onLoad props.

{(!isLoaded || !imageDimensions || !CustomImageComponent) && <ImageLoading />}
</ScrollView>
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/ImageItem/ImageItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import React from "react";
import React, {ComponentType} from "react";
import { GestureResponderEvent } from "react-native";
import { ImageSource } from "../../@types";

Expand All @@ -18,6 +18,7 @@ declare type Props = {
delayLongPress: number;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
CustomImageComponent?: ComponentType;
};

declare const _default: React.MemoExoticComponent<({
Expand All @@ -27,6 +28,7 @@ declare const _default: React.MemoExoticComponent<({
onLongPress,
delayLongPress,
swipeToCloseEnabled,
CustomImageComponent,
}: Props) => JSX.Element>;

export default _default;
18 changes: 6 additions & 12 deletions src/components/ImageItem/ImageItem.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import React, { useCallback, useRef, useState } from "react";
import React, { useCallback, useRef, useState, ComponentType } from "react";

import {
Animated,
Expand Down Expand Up @@ -41,6 +41,7 @@ type Props = {
delayLongPress: number;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
CustomImageComponent?: ComponentType;
};

const ImageItem = ({
Expand All @@ -51,6 +52,7 @@ const ImageItem = ({
delayLongPress,
swipeToCloseEnabled = true,
doubleTapToZoomEnabled = true,
CustomImageComponent = null
}: Props) => {
const scrollViewRef = useRef<ScrollView>(null);
const [loaded, setLoaded] = useState(false);
Expand Down Expand Up @@ -130,17 +132,9 @@ const ImageItem = ({
onScroll,
})}
>
{(!loaded || !imageDimensions) && <ImageLoading />}
<TouchableWithoutFeedback
onPress={doubleTapToZoomEnabled ? handleDoubleTap : undefined}
onLongPress={onLongPressHandler}
delayLongPress={delayLongPress}
>
<Animated.Image
source={imageSrc}
style={imageStylesWithOpacity}
onLoad={() => setLoaded(true)}
/>
{(!loaded || !imageDimensions || !CustomImageComponent) && <ImageLoading />}
<TouchableWithoutFeedback onPress={doubleTapToZoomEnabled ? handleDoubleTap : undefined} onLongPress={onLongPressHandler} delayLongPress={delayLongPress}>
{CustomImageComponent ? (React.createElement(CustomImageComponent, {source:{imageSrc}, style: {imageStylesWithOpacity}, onLoad:() => setLoaded(true), loadingImage: <ImageLoading />})) : (<Animated.Image source={imageSrc} style={imageStylesWithOpacity} onLoad={() => setLoaded(true)}/>)}
</TouchableWithoutFeedback>
</ScrollView>
</View>
Expand Down