import React, { useRef, useState, useEffect } from 'react' import type { CSSProperties, ReactNode } from 'react' import { View, Text } from '@tarojs/components' import { Button } from '@nutui/nutui-react-taro' import { useKeyboardHeight } from '@/store/keyboardStore' import styles from './index.module.scss' export interface CustomPopupProps { visible: boolean onClose: () => void title?: ReactNode showHeader?: boolean hideFooter?: boolean cancelText?: string confirmText?: string onCancel?: () => void onConfirm?: () => void children?: ReactNode className?: string style?: CSSProperties // 与 CommonPopup 保持入参一致 position?: 'center' | 'bottom' | 'top' | 'left' | 'right' round?: boolean zIndex?: number enableDragToClose?: boolean } const CustomPopup: React.FC = ({ visible, onClose, title, showHeader = false, hideFooter = false, cancelText = '返回', confirmText = '完成', onCancel, onConfirm, children, className, style, position = 'bottom', round = true, zIndex, enableDragToClose = true, }) => { const [dragOffset, setDragOffset] = useState(0) const [isDragging, setIsDragging] = useState(false) const touchStartY = useRef(0) // 使用全局键盘状态 const { keyboardHeight, isKeyboardVisible, addListener, initializeKeyboardListener } = useKeyboardHeight() // 使用全局键盘状态监听 useEffect(() => { // 初始化全局键盘监听器 initializeKeyboardListener() // 添加本地监听器 const removeListener = addListener((height, visible) => { console.log('CustomPopup 收到键盘变化:', height, visible) }) return () => { removeListener() } }, [initializeKeyboardListener, addListener]) if (!visible) { return null } const handleCancel = () => { if (onCancel) { onCancel() } else { onClose() } } const handleTouchStart = (e: any) => { if (!enableDragToClose) return touchStartY.current = e.touches[0].clientY setIsDragging(true) } const handleTouchMove = (e: any) => { if (!enableDragToClose || !isDragging) return const currentY = e.touches[0].clientY const deltaY = currentY - touchStartY.current if (deltaY > 0) { setDragOffset(Math.min(deltaY, 100)) } } const handleTouchEnd = () => { if (!enableDragToClose || !isDragging) return setIsDragging(false) if (dragOffset > 50) { onClose() } setDragOffset(0) } const overlayAlignItems = position === 'center' ? 'center' : position === 'top' ? 'flex-start' : 'flex-end' const handleOverlayClick = () => { onClose() } // 阻止弹窗内的触摸事件冒泡 const handleTouchMoveInPopup = (e: any) => { if (!isKeyboardVisible) { e.stopPropagation() } } return ( { e.stopPropagation() }} > {enableDragToClose && ( )} {showHeader && ( {typeof title === 'string' ? ( {title} ) : ( title )} )} {children} {!hideFooter && !isKeyboardVisible && ( )} ) } export default CustomPopup