import { cn } from '@langgenius/dify-ui/cn' import { useEffect, useRef } from 'react' import { sleep } from '@/utils' type IProps = { placeholder?: string value: string onChange: (e: React.ChangeEvent) => void className?: string wrapperClassName?: string minHeight?: number maxHeight?: number autoFocus?: boolean controlFocus?: number onKeyDown?: (e: React.KeyboardEvent) => void onKeyUp?: (e: React.KeyboardEvent) => void } const AutoHeightTextarea = ({ ref: outerRef, value, onChange, placeholder, className, wrapperClassName, minHeight = 36, maxHeight = 96, autoFocus, controlFocus, onKeyDown, onKeyUp, }: IProps & { ref?: React.RefObject }) => { // oxlint-disable-next-line react/rules-of-hooks const ref = outerRef || useRef(null) const doFocus = () => { if (ref.current) { ref.current.setSelectionRange(value.length, value.length) ref.current.focus() return true } return false } const focus = async () => { if (!doFocus()) { let hasFocus = false await sleep(100) hasFocus = doFocus() if (!hasFocus) focus() } } useEffect(() => { if (autoFocus) focus() }, []) useEffect(() => { if (controlFocus) focus() }, [controlFocus]) return (
10000 ? 140 : 130, }} > {!value ? placeholder : value.replace(/\n$/, '\n ')}