@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
57 lines (56 loc) • 2.16 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { cn } from '../../lib/utils';
import { Box } from '../Flex';
export const baseClassName = 'ab-Textarea';
const Textarea = React.forwardRef((props, ref) => {
const internalRef = React.useRef(null);
let { disabled, autoResizeOnFocus, className, ...textareaProps } = props;
const initialHeight = React.useRef(null);
let type = 'text';
if (textareaProps && textareaProps.type) {
type = textareaProps.type;
}
if (type === 'string') {
type = 'text';
}
const expandToFitContent = () => {
if (autoResizeOnFocus && initialHeight.current !== null && internalRef.current) {
internalRef.current.style.height = `0px`;
const height = internalRef.current.scrollHeight;
internalRef.current.style.height = `${height}px`;
}
};
const shrinkToInitialHeight = () => {
if (autoResizeOnFocus) {
internalRef.current.style.height = `${initialHeight.current}px`;
}
};
return (_jsx(Box, { ...textareaProps, onFocus: (e) => {
expandToFitContent();
props.onFocus?.(e);
if (internalRef.current && initialHeight.current === null) {
const height = internalRef.current.getBoundingClientRect().height;
initialHeight.current = height;
}
}, onBlur: (e) => {
shrinkToInitialHeight();
props.onBlur?.(e);
}, ref: (elRef) => {
if (internalRef) {
internalRef.current = elRef;
}
if (ref) {
if (typeof ref === 'function') {
ref(elRef);
}
else {
ref.current = elRef;
}
}
}, onChange: (e) => {
expandToFitContent();
props.onChange && props.onChange(e);
}, as: "textarea", type: type, disabled: disabled, className: cn(baseClassName, disabled ? `${baseClassName}--disabled` : '', className), spellCheck: false }));
});
export default Textarea;