这里使用react 封装的组件 调用自动聚焦到 文本最后 .
autofocus 自动聚焦
捕获input dom
使用 textareaRef.current?.setSelectionRange(-1, -1); (-1, -1)表示聚焦到文字最后. (0, 0) 表示 聚焦到开头位置.
import classNames from 'classnames';
import styles from './index.module.scss';
import { useRef, useEffect } from 'react';
// 1,定义props类型
interface textareaProps
extends React.DetailedHTMLProps<
React.TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
> {
value: string;
}
export default function Textarea(props: textareaProps) {
//2.1声明ref
const textareaRef = useRef<HTMLTextAreaElement>(null);
//2,实现最大的长度限制 默认值是100
const {
maxLength = 100,
className,
value,
autoFocus = true,
...restProps
} = props;
//2.3 生命周期挂