ToolJet/frontend/src/AppBuilder/Widgets/TextArea.jsx
Nishidh Jain 02b4f761b7
refactor: BaseInput Component Structure (#14386)
* refactor: BaseInput Component Structure

* fix: Input overflowing over padding space in top alignment in default size
2025-12-19 16:27:58 +05:30

51 lines
1.7 KiB
JavaScript

import React, { useLayoutEffect, useCallback } from 'react';
import { BaseInput } from './BaseComponents/BaseInput';
import { useInput } from './BaseComponents/hooks/useInput';
import { useDynamicHeight } from '@/_hooks/useDynamicHeight';
import { useHeightObserver } from '@/_hooks/useHeightObserver';
export const TextArea = (props) => {
const inputLogic = useInput(props);
const { properties, height, width, id, adjustComponentPositions, currentLayout, currentMode, subContainerIndex } =
props;
const { inputRef, value } = inputLogic;
const isDynamicHeightEnabled = properties.dynamicHeight && currentMode === 'view';
const heightChangeValue = useHeightObserver(inputRef, isDynamicHeightEnabled);
const resizeTextArea = useCallback(() => {
if (!inputRef.current) return;
if (!isDynamicHeightEnabled) {
inputRef.current.style.height = '100%';
return;
}
inputRef.current.style.height = 'auto';
inputRef.current.style.height =
height >= inputRef.current.scrollHeight ? `${height}px` : `${inputRef.current.scrollHeight + 20}px`;
}, [inputRef?.current, height, isDynamicHeightEnabled]);
useLayoutEffect(() => {
resizeTextArea();
}, [width, height, isDynamicHeightEnabled, properties.placeholder, value, heightChangeValue]);
useDynamicHeight({
isDynamicHeightEnabled,
id,
height,
value: heightChangeValue,
adjustComponentPositions,
currentLayout,
width,
visibility: inputLogic.visibility,
subContainerIndex,
});
return (
<BaseInput
{...props}
{...inputLogic}
isDynamicHeightEnabled={isDynamicHeightEnabled}
inputType="textarea"
classes={{ leftIcon: 'tw-mt-0.5', loaderContainer: 'tw-mt-0.5' }}
/>
);
};