Skip to content

Commit

Permalink
Fix(web-react): TextArea should autoresize when mounted
Browse files Browse the repository at this point in the history
refs #DS-1158
  • Loading branch information
literat committed Feb 26, 2024
1 parent 45aec24 commit 3aba6ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions packages/web-react/src/components/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,13 +8,24 @@ import { useAdjustHeight } from './useAdjustHeight';
const _TextArea = (props: SpiritTextAreaProps, ref: ForwardedRef<HTMLTextAreaElement>): 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 (
<TextFieldBase
isMultiline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormEvent, FormEventHandler, MutableRefObject, ForwardedRef } from 'react';
import { FormEvent, FormEventHandler, ForwardedRef, MutableRefObject } from 'react';

export interface UseAdjustHeightProps {
elementReference?: MutableRefObject<ForwardedRef<HTMLTextAreaElement>>;
Expand All @@ -8,6 +8,7 @@ export interface UseAdjustHeightProps {
}

export interface UseAdjustHeightReturn {
adjustHeight: (element: HTMLTextAreaElement) => void;
onInput: FormEventHandler<HTMLTextAreaElement>;
}

Expand Down Expand Up @@ -47,6 +48,7 @@ export const useAdjustHeight = ({
};

return {
adjustHeight,
onInput: inputHandler,
};
};

0 comments on commit 3aba6ec

Please sign in to comment.