import React, { useState, useEffect } from 'react';
import isFunction from 'lodash-es/isFunction';
import { Clear } from '@pingtou/rn-vant-icons';
import { View, Text, Modal, Pressable, TouchableOpacity } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import Image from '../Image';
import Swiper from '../Swiper';
import { useThemeFactory } from '../Theme';
import constants from '../utils/constants';
import { cloneReactNode } from '../utils/cloneReactNode';
import { createStyle } from './style';
import type { ImagePreviewProps } from './type';
const defaultProps = {
overlay: true,
showIndex: true,
images: [],
swipeDuration: 300,
startPosition: 0,
closeIconPosition: 'top-right' as const,
showIndicators: false,
closeOnlyClickCloseIcon: false,
maxZoom: 3,
closeIcon: ,
};
const ImagePreview = (_props: ImagePreviewProps): JSX.Element => {
const props = { ...defaultProps, ..._props };
const { beforeClose, closeIcon } = props;
const insets = useSafeAreaInsets();
const [visible, setVisible] = useState(() => props.visible);
const [active, setActive] = useState(() => props.startPosition);
const { styles, theme } = useThemeFactory(createStyle);
useEffect(() => {
setVisible(props.visible);
}, [props.visible]);
const handleClose = async (closeByIcon = false) => {
if (!closeByIcon && props.closeOnlyClickCloseIcon) return;
const couldClose = isFunction(beforeClose) ? await Promise.resolve(beforeClose(active)) : true;
if (couldClose) {
setVisible(false);
props.onClose?.();
}
};
const renderImages = () => (
{props.images.map(image => (
handleClose()}
>
))}
);
const renderIndex = () => {
if (props.showIndex) {
return (
{props.indexRender
? props.indexRender({ index: active, len: props.images.length })
: `${active + 1} / ${props.images.length}`}
);
}
return null;
};
const renderCloseIcon = () => {
if (!props.closeable) return null;
return (
handleClose(true)}
>
{cloneReactNode(closeIcon, {
size: theme.image_preview_close_icon_size,
color: theme.image_preview_close_icon_color,
})}
);
};
return (
handleClose()}
onDismiss={props.onClosed}
testID={props.testID}
>
handleClose()}>
{renderImages()}
{renderIndex()}
{renderCloseIcon()}
);
};
export default ImagePreview;