@@ -7,6 +7,7 @@ import React, {
7
7
TextareaHTMLAttributes ,
8
8
useCallback ,
9
9
useState ,
10
+ useRef ,
10
11
} from "react" ;
11
12
import cn from "classnames" ;
12
13
import { bem , useEnsuredRef , useResizeObserver } from "@react-md/utils" ;
@@ -168,9 +169,10 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
168
169
setHeight ( undefined ) ;
169
170
}
170
171
171
- const [ mask , setMask ] = useState < HTMLTextAreaElement | null > ( null ) ;
172
+ const maskRef = useRef < HTMLTextAreaElement | null > ( null ) ;
172
173
const [ scrollable , setScrollable ] = useState ( false ) ;
173
- const updateHeight = ( ) : void => {
174
+ const updateHeight = useCallback ( ( ) => {
175
+ const mask = maskRef . current ;
174
176
if ( ! mask ) {
175
177
return ;
176
178
}
@@ -195,19 +197,20 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
195
197
if ( height !== nextHeight ) {
196
198
setHeight ( nextHeight ) ;
197
199
}
198
- } ;
200
+ } , [ height , maxRows , scrollable ] ) ;
199
201
200
202
// the window can be resized while there is text inside the textarea so need to
201
203
// recalculate the height when the width changes as well.
202
- useResizeObserver ( {
204
+ const [ , maskRefHandler ] = useResizeObserver ( updateHeight , {
205
+ ref : maskRef ,
203
206
disableHeight : true ,
204
- target : mask ,
205
- onResize : updateHeight ,
206
207
} ) ;
208
+
207
209
const [ valued , onChange ] = useValuedState < HTMLTextAreaElement > ( {
208
210
value,
209
211
defaultValue,
210
212
onChange : ( event ) => {
213
+ const mask = maskRef . current ;
211
214
if ( propOnChange ) {
212
215
propOnChange ( event ) ;
213
216
}
@@ -286,7 +289,7 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
286
289
aria-hidden
287
290
defaultValue = { value || defaultValue }
288
291
id = { `${ id } -mask` }
289
- ref = { setMask }
292
+ ref = { maskRefHandler }
290
293
readOnly
291
294
rows = { rows }
292
295
tabIndex = { - 1 }
0 commit comments