import React, { useState, useEffect, forwardRef, useRef, useImperativeHandle, } from "react"; import { View, Text, Image, Input, Textarea } from "@tarojs/components"; import Taro from "@tarojs/taro"; import dayjs from "dayjs"; import classnames from "classnames"; import CommentServices from "@/services/commentServices"; import messageService from "@/services/messageService"; import { delay } from "@/utils"; import type { BaseComment, Comment, ReplyComment, } from "@/services/commentServices"; import { useUserInfo } from "@/store/userStore"; import sendImg from "@/static/detail/icon-sendup.svg"; import addComment from "@/static/detail/icon-write.svg"; import emptyComment from "@/static/emptyStatus/comment-empty.png"; import CommonPopup from "../CommonPopup"; import styles from "./index.module.scss"; import { useKeyboardHeight } from "@/store/keyboardStore"; // const PAGESIZE = 4; const PAGESIZE = 1000; function toast(msg) { Taro.showToast({ title: msg, icon: "none" }); } interface CommentInputProps { onConfirm?: ( value: { content: string } & Partial, ) => void; } // 2️⃣ 定义通过 ref 暴露出去的方法类型 interface CommentInputRef { show: (params?: CommentInputReplyParamsType) => void; } interface CommentInputReplyParamsType { parent_id: number; reply_to_user_id: number; nickname: string; } const CommentInput = forwardRef( function (props, ref) { const { onConfirm } = props; const [visible, setVisible] = useState(false); const [value, setValue] = useState(""); const [params, setParams] = useState< CommentInputReplyParamsType | undefined >(); const { keyboardHeight, isKeyboardVisible, addListener, initializeKeyboardListener, } = useKeyboardHeight(); // 使用全局键盘状态监听 useEffect(() => { // 初始化全局键盘监听器 initializeKeyboardListener(); // 添加本地监听器 const removeListener = addListener(() => { // 布局是否响应交由 shouldReactToKeyboard 决定 }); return () => { removeListener(); }; }, [initializeKeyboardListener, addListener]); const inputDomRef = useRef(null); useImperativeHandle(ref, () => ({ show: (_params: CommentInputReplyParamsType | undefined) => { setVisible(true); setTimeout(() => { inputDomRef.current && inputDomRef.current?.focus(); }, 100); setParams(_params); }, })); function handleSend() { if (!value) { toast("评论内容不得为空"); return; } if (value.length > 200) { return; } onConfirm?.({ content: value, ...params }); onClose(); } function onClose() { setVisible(false); setValue(""); inputDomRef.current && inputDomRef.current?.blur(); } return ( 0 ? `${keyboardHeight}px` : "0", }} enableDragToClose={false} >