From a179efc0ea62ed7598d7bec03e312001b3a68667 Mon Sep 17 00:00:00 2001 From: Terry Sutton Date: Tue, 13 Aug 2024 13:12:16 -0230 Subject: [PATCH 1/3] Use ExpandableTextArea component --- .../components/ExpandableTextArea.tsx | 51 +++++++++++++++++++ apps/postgres-new/components/chat.tsx | 31 +++++++++-- apps/postgres-new/components/ui/textarea.tsx | 27 ++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 apps/postgres-new/components/ExpandableTextArea.tsx create mode 100644 apps/postgres-new/components/ui/textarea.tsx diff --git a/apps/postgres-new/components/ExpandableTextArea.tsx b/apps/postgres-new/components/ExpandableTextArea.tsx new file mode 100644 index 0000000..f2c6406 --- /dev/null +++ b/apps/postgres-new/components/ExpandableTextArea.tsx @@ -0,0 +1,51 @@ +'use client' + +import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react' +import { cn } from '~/lib/utils' +import { TextArea } from './ui/textarea' + +export interface ExpandingTextAreaProps extends React.TextareaHTMLAttributes { + /* The value of the textarea. Required to calculate the height of the textarea. */ + value: string +} + +/** + * This is a custom TextArea component that expands based on the content. + */ +const ExpandingTextArea = forwardRef( + ({ className, value, ...props }, ref) => { + const textAreaRef = useRef(null) + + // Expose the ref to the parent component + useImperativeHandle(ref, () => textAreaRef.current!) + /** + * This effect is used to resize the textarea based on the content + */ + useEffect(() => { + if (textAreaRef) { + if (textAreaRef.current && !value) { + textAreaRef.current.style.height = '40px' + } else if (textAreaRef && textAreaRef.current) { + textAreaRef.current.style.height = 'auto' + const newHeight = textAreaRef.current.scrollHeight + 'px' + textAreaRef.current.style.height = newHeight + } + } + }, [value, textAreaRef]) + + return ( +