From e7994d264419dfd00c8126de43da553db90c8e34 Mon Sep 17 00:00:00 2001 From: alexsparkes Date: Mon, 16 Dec 2024 21:46:06 +0000 Subject: [PATCH] feat(textarea): implement TextareaAutosize component and update imports across the application --- src/components/Elements/AddModal/AddModal.jsx | 5 +- .../TextareaAutosize/TextareaAutosize.jsx | 73 +++++++++++++++++++ .../Form/Settings/TextareaAutosize/index.jsx | 1 + src/components/Form/Settings/index.jsx | 1 + .../greeting/options/GreetingOptions.jsx | 4 +- src/features/navbar/components/Notes.jsx | 2 +- 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 src/components/Form/Settings/TextareaAutosize/TextareaAutosize.jsx create mode 100644 src/components/Form/Settings/TextareaAutosize/index.jsx diff --git a/src/components/Elements/AddModal/AddModal.jsx b/src/components/Elements/AddModal/AddModal.jsx index 4be74446..cf56a119 100644 --- a/src/components/Elements/AddModal/AddModal.jsx +++ b/src/components/Elements/AddModal/AddModal.jsx @@ -1,10 +1,9 @@ import variables from 'config/variables'; import { useState, memo } from 'react'; -import { TextareaAutosize } from '@mui/material'; import { MdAddLink, MdClose } from 'react-icons/md'; -import { Tooltip } from 'components/Elements'; -import { Button } from 'components/Elements'; +import { Tooltip, Button } from 'components/Elements'; +import { TextareaAutosize } from 'components/Form'; function AddModal({ urlError, iconError, addLink, closeModal, edit, editData, editLink }) { const [name, setName] = useState(edit ? editData.name : ''); diff --git a/src/components/Form/Settings/TextareaAutosize/TextareaAutosize.jsx b/src/components/Form/Settings/TextareaAutosize/TextareaAutosize.jsx new file mode 100644 index 00000000..846a618e --- /dev/null +++ b/src/components/Form/Settings/TextareaAutosize/TextareaAutosize.jsx @@ -0,0 +1,73 @@ +import { useEffect, useRef } from 'react'; + +const TextareaAutosize = ({ + value, + onChange, + placeholder, + minRows = 1, + maxRows = 10, + className = '', + style = {}, + disabled = false, + error = false, + id, + 'aria-label': ariaLabel, + 'aria-describedby': ariaDescribedby, +}) => { + const textareaRef = useRef(null); + + useEffect(() => { + const textarea = textareaRef.current; + if (!textarea) return; + + const resizeTextarea = () => { + textarea.style.height = 'auto'; + const lineHeight = parseInt(getComputedStyle(textarea).lineHeight); + const minHeight = lineHeight * minRows; + const maxHeight = lineHeight * maxRows; + const newHeight = Math.max(minHeight, Math.min(textarea.scrollHeight, maxHeight)); + textarea.style.height = `${newHeight}px`; + }; + + resizeTextarea(); + }, [value, minRows, maxRows]); + + return ( +