diff --git a/packages/web-react/src/components/TextArea/TextArea.tsx b/packages/web-react/src/components/TextArea/TextArea.tsx index 7539f5fc59..4b35a214a2 100644 --- a/packages/web-react/src/components/TextArea/TextArea.tsx +++ b/packages/web-react/src/components/TextArea/TextArea.tsx @@ -1,4 +1,4 @@ -import React, { forwardRef, useRef, RefObject, ForwardedRef } from 'react'; +import React, { ForwardedRef, RefObject, forwardRef, useEffect, useRef } from 'react'; import { SpiritTextAreaProps } from '../../types'; import { TextFieldBase } from '../TextFieldBase'; import { useAdjustHeight } from './useAdjustHeight'; @@ -8,13 +8,24 @@ import { useAdjustHeight } from './useAdjustHeight'; const _TextArea = (props: SpiritTextAreaProps, ref: ForwardedRef): JSX.Element => { const { onInput, isAutoResizing, autoResizingMaxHeight = 400, ...restProps } = props; const elementReference = useRef(ref); - const { onInput: onInputHandler } = useAdjustHeight({ + const { adjustHeight, onInput: onInputHandler } = useAdjustHeight({ elementReference, onInput, isAutoResizing, maxHeight: autoResizingMaxHeight, }); + useEffect(() => { + if (isAutoResizing) { + // Because of mixed props for input and textarea + const textArea = elementReference?.current as unknown as HTMLTextAreaElement; + + if (textArea) { + adjustHeight(textArea); + } + } + }, [isAutoResizing, adjustHeight, elementReference]); + return ( >; @@ -8,6 +8,7 @@ export interface UseAdjustHeightProps { } export interface UseAdjustHeightReturn { + adjustHeight: (element: HTMLTextAreaElement) => void; onInput: FormEventHandler; } @@ -47,6 +48,7 @@ export const useAdjustHeight = ({ }; return { + adjustHeight, onInput: inputHandler, }; };