Skip to content

Commit

Permalink
perf(textarea): optimize TextArea init load autosize (#3286)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaixingOoO authored Dec 18, 2024
1 parent 05533e4 commit 9fe694d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/textarea/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getCharacterLength, getUnicodeLength, limitUnicodeMaxLength } from '../
import calcTextareaHeight from '../_common/js/utils/calcTextareaHeight';
import { textareaDefaultProps } from './defaultProps';
import useDefaultProps from '../hooks/useDefaultProps';
import useIsomorphicLayoutEffect from '../hooks/useLayoutEffect';

export interface TextareaProps
extends Omit<
Expand All @@ -22,7 +23,7 @@ export interface TextareaRefInterface extends React.RefObject<unknown> {
textareaElement: HTMLTextAreaElement;
}

const Textarea = forwardRef((originalProps: TextareaProps, ref: TextareaRefInterface) => {
const Textarea = forwardRef<TextareaRefInterface, TextareaProps>((originalProps, ref) => {
const props = useDefaultProps<TextareaProps>(originalProps, textareaDefaultProps);
const {
disabled,
Expand Down Expand Up @@ -92,11 +93,6 @@ const Textarea = forwardRef((originalProps: TextareaProps, ref: TextareaRefInter
}
}, [autosize]);

useEffect(() => {
adjustTextareaHeight();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [textareaRef?.current]);

function inputValueChangeHandle(e: React.FormEvent<HTMLTextAreaElement>) {
const { target } = e;
let val = (target as HTMLInputElement).value;
Expand All @@ -114,7 +110,7 @@ const Textarea = forwardRef((originalProps: TextareaProps, ref: TextareaRefInter
composingRef.current = true;
}

function handleCompositionEnd(e) {
function handleCompositionEnd(e: React.FormEvent<HTMLTextAreaElement>) {
if (composingRef.current) {
composingRef.current = false;
inputValueChangeHandle(e);
Expand All @@ -132,7 +128,12 @@ const Textarea = forwardRef((originalProps: TextareaProps, ref: TextareaRefInter
</span>
);

useEffect(() => {
useIsomorphicLayoutEffect(() => {
adjustTextareaHeight();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [textareaRef?.current]);

useIsomorphicLayoutEffect(() => {
// 当未设置 autosize 时,需要将 textarea 的 height 设置为 auto,以支持原生的 textarea rows 属性
if (autosize === false) {
setTextareaStyle({ height: 'auto', minHeight: 'auto' });
Expand Down

0 comments on commit 9fe694d

Please sign in to comment.