import React, { useState, useEffect } from 'react'; import { View, Text, Textarea, Input, Picker } from '@tarojs/components'; import Taro from '@tarojs/taro'; import './EditModal.scss'; import { useKeyboardHeight } from '@/store/keyboardStore' interface EditModalProps { visible: boolean; title: string; type: string; placeholder: string; initialValue: string; maxLength: number; onSave: (value: string) => void; onCancel: () => void; validationMessage?: string; } const EditModal: React.FC = ({ visible, title, type, placeholder, initialValue, maxLength, onSave, onCancel, validationMessage }) => { const [value, setValue] = useState(initialValue); const [isValid, setIsValid] = useState(true); // 使用全局键盘状态 const { keyboardHeight, isKeyboardVisible, addListener, initializeKeyboardListener } = useKeyboardHeight() // 使用全局键盘状态监听 useEffect(() => { // 初始化全局键盘监听器 initializeKeyboardListener() // 添加本地监听器 const removeListener = addListener((height, visible) => { console.log('AiImportPopup 收到键盘变化:', height, visible) }) return () => { removeListener() } }, [initializeKeyboardListener, addListener]) useEffect(() => { if (visible) { setValue(initialValue); } }, [visible, initialValue]); const handle_input_change = (e: any) => { const new_value = e.detail.value; setValue(new_value); // 验证输入 const valid = new_value.length >= 2 && new_value.length <= maxLength; setIsValid(valid); }; const handle_save = () => { if (!isValid) { Taro.showToast({ title: validationMessage || `请填写 2-${maxLength} 个字符`, icon: 'none', duration: 2000 }); return; } onSave(value); }; const handle_cancel = () => { setValue(initialValue); onCancel(); }; if (!visible) { return null; } return ( {/* 标题栏 */} {title} {/* 内容区域 */} {/* 文本输入区域 */} {type === 'nickname' ? ( ) : ( <>