import React, {ReactNode, useState, useEffect, useRef, useMemo} from 'react' import { Animated, Dimensions } from "react-native" import { View, Button, Text, ScrollView } from '@tarojs/components-rn' import {RTX} from './constants/constants' import {changeS2} from './constants/utils' import './widget.css' interface Modal { children?: ReactNode isShow?: boolean title?: string content?: string onCancel?: () => void onConfirm?: (e?: any) => void cancelTxt?: string confirmTxt?: string isHaveCancel?: boolean isHaveConfirm?: boolean size?: string bgColor?: string /** 标题的字体大小 */ titleSize?: string /** 标题的字体颜色 */ titleColor?: string /** 内容的字体大小 */ contentSize?: string /** 内容的字体颜色 */ contentColor?: string /** 按钮颜色 */ buttonColor?: string /** 按钮大小 */ buttonSize?: string buttonRadius?: string } const {width, height} = Dimensions.get('window') export default (props: Modal) => { const { isShow=false, title='', content='', onCancel=() => {}, onConfirm=() => {}, cancelTxt='取消', confirmTxt='确定', isHaveCancel=true, isHaveConfirm=true, children, size='600 auto', bgColor='rgba(255,255,255,1)', titleSize='32', titleColor='#060606', contentSize='28', contentColor='#060606', buttonColor='#1890ff', buttonSize='190 76', buttonRadius='15' } = props const [backSize, setBackSize] = useState({ width: 0, height: 0, }) const fadeAnim = useRef(new Animated.Value(0)).current const fadeIn = () => { Animated.timing(fadeAnim, { toValue: 1, duration: 460, useNativeDriver: true }).start() } const fadeOut = () => { Animated.timing(fadeAnim, { toValue: 0, duration: 460, useNativeDriver: true }).start() } useEffect(() => { if(isShow){ setBackSize({ width: width, height: height }) fadeIn() }else{ fadeOut() setTimeout(() => { setBackSize({ width: 0, height: 0 }) }, 500); } }, [isShow, width, height]) const msize = useMemo(() => { if(backSize.width === 0){ return { width: 0, height: 0 } } return { width: size ? changeS2(size)[0] : 'auto', height: size ? changeS2(size)[1] : 'auto', } }, [size, backSize.width]) return( { onCancel() }} > {}} > { backSize.width > 0 && {title} { children ? children : {content} } { (isHaveCancel || isHaveConfirm) && { isHaveCancel && { onCancel() }} > {cancelTxt} } { isHaveConfirm && } } } ) }