import { forwardRef, useEffect, useRef } from 'react' import cn from '@/utils/classnames' 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 = forwardRef( ( { value, onChange, placeholder, className, wrapperClassName, minHeight = 36, maxHeight = 96, autoFocus, controlFocus, onKeyDown, onKeyUp }: IProps, outerRef: any, ) => { // eslint-disable-next-line react-hooks/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 ')}