import { View, Textarea, InputProps } from "@tarojs/components"; import TextPolish from '@/components/TextPolish' import styleIndex from "./index.module.less"; import { useState, useRef, useCallback, useMemo, forwardRef, useImperativeHandle } from "react"; import { countCharacters, getStrByMaxLength } from "@/utils/index"; interface Props { aiType: "personality" | "greeting" defaultAiTips: string; placeholder?: string; value: string; cursorSpacing?: number; maxlength?: number; autoHeight?: boolean; disabled?: boolean; confirmType?: keyof InputProps.ConfirmType; extra?: () => JSX.Element | JSX.Element[] | undefined; prefix?: () => JSX.Element | JSX.Element[] | undefined; style?: Record; onInput?: (value: string) => void; onBlur?: (value: string) => void; bgColor?: string; autoFocus?: boolean; extraClass?: string; onConfirm?: (value: string) => void; } const DEFAULT_TEXTAREA_MAXLENGTH = 10000; const Index = ({ value, aiType, defaultAiTips, bgColor, extraClass, style, disabled, confirmType, prefix, autoHeight, autoFocus = false, placeholder = "请输入...", onInput, onBlur, cursorSpacing, maxlength, extra, onConfirm, }: Props, ref: any) => { const [focus, setFocus] = useState(false); const [isPolishing, setIsPolishing] = useState(false); const inputRef = useRef(null); // 创建一个 ref // Create ref for TextPolish component const textPolishRef = useRef<{ handleClick: () => void }>(null); // Expose TextPolish ref to parent components useImperativeHandle(ref, () => ({ handleClick: async () => { if (textPolishRef.current) { return await textPolishRef.current.handleClick(); } } })); // Memoize character count to avoid recalculation const currentLength = useMemo(() => countCharacters(value), [value]); const handleFocus = () => { setFocus(true); }; const handleBlur = () => { console.log("textarea blur"); setFocus(false); if (onBlur && inputRef.current) { onBlur(inputRef.current.value); } }; const handleInput = useCallback((inputValue: string) => { if (!onInput) return; const len = countCharacters(inputValue); if (maxlength && len > maxlength) { const r = getStrByMaxLength(inputValue, maxlength); onInput(r); return; } onInput(inputValue); }, [onInput, maxlength]); const onPolished = (text: string | null) => { if (text && onInput) { onInput(text); } }; const handleTextareaInput = (e: any) => { handleInput(e.target.value); }; const handleConfirm = (e: any) => { onConfirm && onConfirm(e.detail.value); }; // Memoize container style const containerStyle = useMemo(() => { return bgColor ? { backgroundColor: bgColor } : {}; }, [bgColor]); const polishText = value?.length > 0 ? value : defaultAiTips; return ( {prefix && prefix()}