Modal.rn.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { View, Text, ScrollView } from '@tarojs/components'
  2. import './Modal.scss'
  3. import React, { useState } from 'react';
  4. import { ModalType } from '@/utils/types';
  5. import Box from './Box';
  6. import Taro from '@tarojs/taro';
  7. import {Modal as ModalRN} from 'react-native'
  8. export default function Modal(props: {
  9. children: React.ReactNode,
  10. testInfo?: React.ReactNode,
  11. title?: string,
  12. dismiss: Function,
  13. confirm?: Function,
  14. themeColor?: string,
  15. modalType?: ModalType,
  16. cancelCatchMove?: boolean
  17. }) {
  18. const [isDismiss, setIsDismiss] = useState(false)
  19. //阻止中间内容点击事件穿透
  20. function click(e) {
  21. if (process.env.TARO_ENV == 'weapp') {
  22. e.stopPropagation()
  23. }
  24. Taro.vibrateShort({
  25. type: 'medium'
  26. })
  27. }
  28. function onClick() {
  29. // Taro.vibrateShort({
  30. // type:'medium'
  31. // })
  32. }
  33. switch (props.modalType) {
  34. case ModalType.center:
  35. return <ModalRN transparent={true} >
  36. <View onClick={()=>{props.dismiss()}} style={{backgroundColor:'rgba(0,0,0,0.95)',width:'100%',height:'100%',alignItems:'center',justifyContent:'center'}}>
  37. <Box><View onClick={click}>
  38. {
  39. props.children
  40. }
  41. </View>
  42. </Box>
  43. </View>
  44. </ModalRN>
  45. }
  46. function longPress(e) {
  47. if (process.env.TARO_ENV == 'weapp') {
  48. e.stopPropagation()
  49. }
  50. }
  51. function dismiss() {
  52. if (props.modalType == ModalType.center) {
  53. props.dismiss()
  54. return
  55. }
  56. setIsDismiss(true)
  57. setTimeout(() => {
  58. props.dismiss()
  59. }, 250)
  60. }
  61. global.dismissModal = dismiss;
  62. return <ModalRN style={{backgroundColor:'red'}} >
  63. <View style={{ flex: 1, width: 375, flexShrink: 0 }} onClick={(e) => {
  64. dismiss()
  65. }}>
  66. {
  67. props.testInfo ? props.testInfo : null
  68. }
  69. </View>
  70. <View className={isDismiss ? 'modal_bottom_content modal_bottom_dismiss' : 'modal_bottom_content'} style={{ flexShrink: 0 }} onClick={onClick}>
  71. {/* <Text className='modal_title' style={{ color: color }}>{props.title ? props.title : '测试标题 '}</Text> */}
  72. {
  73. props.children
  74. }
  75. </View>
  76. </ModalRN>
  77. }