Modal.weapp.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. export default function Modal(props: {
  8. children: React.ReactNode,
  9. testInfo?: React.ReactNode,
  10. title?: string,
  11. dismiss: Function,
  12. confirm?: Function,
  13. themeColor?: string,
  14. modalType?: ModalType,
  15. cancelCatchMove?: boolean
  16. }) {
  17. const [isDismiss, setIsDismiss] = useState(false)
  18. //阻止中间内容点击事件穿透
  19. function click(e) {
  20. if (process.env.TARO_ENV == 'weapp') {
  21. e.stopPropagation()
  22. }
  23. Taro.vibrateShort({
  24. type: 'medium'
  25. })
  26. }
  27. function onClick() {
  28. // Taro.vibrateShort({
  29. // type:'medium'
  30. // })
  31. }
  32. switch (props.modalType) {
  33. case ModalType.center:
  34. return <View className='modal modal_center_container' catchMove onClick={(e) => {
  35. if (process.env.TARO_ENV == 'weapp') {
  36. e.stopPropagation()
  37. }; props.dismiss()
  38. }}>
  39. <View className='center_modal_detail'>
  40. <Box><View onClick={click}>
  41. {
  42. props.children
  43. }
  44. </View>
  45. </Box>
  46. </View>
  47. </View>
  48. }
  49. function longPress(e) {
  50. if (process.env.TARO_ENV == 'weapp') {
  51. e.stopPropagation()
  52. }
  53. }
  54. function dismiss() {
  55. if (props.modalType == ModalType.center) {
  56. props.dismiss()
  57. return
  58. }
  59. setIsDismiss(true)
  60. setTimeout(() => {
  61. props.dismiss()
  62. }, 250)
  63. }
  64. global.dismissModal = dismiss;
  65. return <View className={isDismiss ? 'modal modal_dismiss' : 'modal'} catchMove onLongPress={longPress}>
  66. <View style={{ flex: 1, width: 375, flexShrink: 0 }} onClick={(e) => {
  67. if (process.env.TARO_ENV == 'weapp') {
  68. e.stopPropagation()
  69. }; dismiss()
  70. }}>
  71. {
  72. props.testInfo ? props.testInfo : null
  73. }
  74. </View>
  75. <View className={isDismiss ? 'modal_bottom_content modal_bottom_dismiss' : 'modal_bottom_content'} style={{ flexShrink: 0 }} onClick={onClick}>
  76. {/* <Text className='modal_title' style={{ color: color }}>{props.title ? props.title : '测试标题 '}</Text> */}
  77. {
  78. props.children
  79. }
  80. {/* <View className='modal_operate'>
  81. <View className='modal_btn' style={{ backgroundColor: color+alpha }} onClick={() => props.dismiss()}>
  82. <Text className='modal_cancel_text' style={{color:color}}>取消</Text>
  83. </View>
  84. <View className='btn_space' />
  85. <View className='modal_btn' style={{ backgroundColor: color }} onClick={() => props.confirm()}>
  86. <Text className='modal_confirm_text' style={{color:'#000'}}>确定</Text>
  87. </View>
  88. </View> */}
  89. </View>
  90. </View>
  91. }