Skip to content

Commit 2c2dd27

Browse files
committed
feat(form): updated TextArea to use the new useResizeObserver API
1 parent 052b3f2 commit 2c2dd27

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

packages/form/src/text-field/TextArea.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
TextareaHTMLAttributes,
88
useCallback,
99
useState,
10+
useRef,
1011
} from "react";
1112
import cn from "classnames";
1213
import { bem, useEnsuredRef, useResizeObserver } from "@react-md/utils";
@@ -168,9 +169,10 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
168169
setHeight(undefined);
169170
}
170171

171-
const [mask, setMask] = useState<HTMLTextAreaElement | null>(null);
172+
const maskRef = useRef<HTMLTextAreaElement | null>(null);
172173
const [scrollable, setScrollable] = useState(false);
173-
const updateHeight = (): void => {
174+
const updateHeight = useCallback(() => {
175+
const mask = maskRef.current;
174176
if (!mask) {
175177
return;
176178
}
@@ -195,19 +197,20 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
195197
if (height !== nextHeight) {
196198
setHeight(nextHeight);
197199
}
198-
};
200+
}, [height, maxRows, scrollable]);
199201

200202
// the window can be resized while there is text inside the textarea so need to
201203
// recalculate the height when the width changes as well.
202-
useResizeObserver({
204+
const [, maskRefHandler] = useResizeObserver(updateHeight, {
205+
ref: maskRef,
203206
disableHeight: true,
204-
target: mask,
205-
onResize: updateHeight,
206207
});
208+
207209
const [valued, onChange] = useValuedState<HTMLTextAreaElement>({
208210
value,
209211
defaultValue,
210212
onChange: (event) => {
213+
const mask = maskRef.current;
211214
if (propOnChange) {
212215
propOnChange(event);
213216
}
@@ -286,7 +289,7 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
286289
aria-hidden
287290
defaultValue={value || defaultValue}
288291
id={`${id}-mask`}
289-
ref={setMask}
292+
ref={maskRefHandler}
290293
readOnly
291294
rows={rows}
292295
tabIndex={-1}

0 commit comments

Comments
 (0)