import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import FastImage from 'react-native-fast-image'; import Animated, { useAnimatedStyle, useSharedValue, withSpring, withTiming } from 'react-native-reanimated'; import { colors, onePx, px2dp } from '../../utils'; interface Props { } interface State { } interface TypeResearch { name: string type: string } const SelectResearchTypeToast = () => { const containerRef = React.useRef(null); const delayTimer = React.useRef(null); const delayTimer1 = React.useRef(null); const overlayOpacity = useSharedValue(0.1); const layoutScaleValue = useSharedValue(0); const [researchTypeList, setResearchTypeList] = React.useState>([]); const showToastListener = React.useRef(null); const hideToastListener = React.useRef(null); const tempKey = React.useRef(null); React.useEffect(() => { overlayOpacity.value = withTiming(0.2); layoutScaleValue.value = withSpring(0.6); showToastListener.current = DeviceEventEmitter.addListener('show-selete-research-type-toast', (a) => { setResearchTypeList(a.researchTypeList ? a.researchTypeList : []); tempKey.current = a.key; delayTimer.current = setTimeout(() => { handleShowToast() }, 0); }); hideToastListener.current = DeviceEventEmitter.addListener('hide-selete-research-type-toast', (a) => { handleHideToast() }); return () => { delayTimer.current && clearTimeout(delayTimer.current); delayTimer1.current && clearTimeout(delayTimer1.current); showToastListener.current && showToastListener.current?.remove(); hideToastListener.current && hideToastListener.current?.remove(); } }, []); const handleShowToast = () => { requestAnimationFrame(() => { containerRef.current.setNativeProps({ style: { width: '100%', height: '100%' } }) delayTimer1.current = setTimeout(() => { overlayOpacity.value = 0.52; layoutScaleValue.value = 1; }, 0); }) }; const handleHideToast = () => { delayTimer.current = setTimeout(() => { containerRef.current.setNativeProps({ style: { width: 0, height: 0 } }) }, 240); overlayOpacity.value = 0.1; layoutScaleValue.value = 0; } const overlayAnimatedStyle = useAnimatedStyle(() => { return { opacity: withTiming(overlayOpacity.value, { duration: 240 }) } }); const toastBodyAnimatedStyle = useAnimatedStyle(() => { return { transform: [ { scale: withSpring(layoutScaleValue.value, { stiffness: 96 }), } ], opacity: withTiming(layoutScaleValue.value, { duration: 120 }), } }) const onSelectResearchType = (item: TypeResearch) => { DeviceEventEmitter.emit('on-select-research-type', { researchType: item.type, key: tempKey.current }); handleHideToast(); } return ( 选择调查方式 { researchTypeList && researchTypeList.map((ele, index) => { return ( onSelectResearchType(ele)} > {ele.name} ) }) } 取消 ) }; const styles = StyleSheet.create({ overlay: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, width: '100%', height: '100%', backgroundColor: '#000', zIndex: -1 }, container: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, zIndex: 999, width: 0, height: 0, overflow: 'hidden' }, content: { position: 'relative', width: '100%', height: '100%', flex: 1, justifyContent: 'center', alignItems: 'center', // backgroundColor: 'red' }, toastBody: { width: px2dp(300), minHeight: px2dp(200), backgroundColor: '#fff', borderRadius: px2dp(8), elevation: 12, justifyContent: 'space-between', alignItems: 'center', paddingVertical: px2dp(12) }, footer: { width: px2dp(272), height: px2dp(36), borderRadius: px2dp(18), justifyContent: 'center', alignItems: 'center', backgroundColor: '#00D38F58', borderWidth: 1, borderColor: '#00D38F64', marginTop: px2dp(24) // borderTopColor: '#eee', // borderTopWidth: onePx }, selectItem: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', // borderBottomWidth: 1, // borderBottomColor: '#303133', // paddingTop: px2dp(16), marginTop: px2dp(12), backgroundColor: '#f2f2f2', width: px2dp(272), height: px2dp(36), borderRadius: px2dp(5) }, selectItemName: { fontSize: 16, color: '#303133', marginLeft: px2dp(3) } }) export default SelectResearchTypeToast;