Skip to content

Commit

Permalink
feat(localized-multiline-text-input): set value of cacheMeasurements …
Browse files Browse the repository at this point in the history
…based on whether isCondensed value has been toggled
  • Loading branch information
Sarah4VT committed Apr 24, 2024
1 parent e0ebb9f commit 13593a2
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useRef, useCallback, ChangeEventHandler } from 'react';
import {
useRef,
useCallback,
ChangeEventHandler,
useState,
useEffect,
} from 'react';
import TextareaAutosize, {
TextareaHeightChangeMeta,
} from 'react-textarea-autosize';
Expand Down Expand Up @@ -53,6 +59,20 @@ const MultilineInput = (props: TMultiLineInputProps) => {
const { onHeightChange } = props;
const ref = useRef<HTMLTextAreaElement | null>(null);

const [prevIsCondensed, setPrevIsCondensed] = useState(props.isCondensed);
const [cacheMeasurements, setCacheMeasurements] = useState(true);
useEffect(() => {
// If props.isCondensed has changed, the height of the layout will only be automatically recalculated if
// cacheMeasurements is set to false. So let's set cacheMeasurements to true by default to get the performance
// benefits, unless isCondensed value has changed, in which case we will set it to false for this render.
if (props.isCondensed !== prevIsCondensed) {
setCacheMeasurements(false);
} else {
setCacheMeasurements(true);
}
setPrevIsCondensed(props.isCondensed);
}, [props.isCondensed, prevIsCondensed]);

const handleHeightChange = useCallback<
(height: number, meta: TextareaHeightChangeMeta) => void
>(
Expand Down Expand Up @@ -104,6 +124,7 @@ const MultilineInput = (props: TMultiLineInputProps) => {
role="textbox"
minRows={MIN_ROW_COUNT}
maxRows={props.isOpen ? undefined : MIN_ROW_COUNT}
cacheMeasurements={cacheMeasurements}
{...filterDataAttributes(props)}
/>
);
Expand Down

0 comments on commit 13593a2

Please sign in to comment.