File size: 913 Bytes
79278ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useEffect, useRef } from "react";
import TextAreaProps from "./TextArea.interface";
import "./TextArea.scss";

const TextArea = ({ ...props }: TextAreaProps) => {
  const textAreaRef = useRef<HTMLTextAreaElement>(null);

  useEffect(() => {
    const resizeObserver = new ResizeObserver(() => {
      if (textAreaRef.current) {
        textAreaRef.current.style.height = "auto";
        textAreaRef.current.style.height = `${
          textAreaRef.current.scrollHeight + 2
        }px`;
      }
    });

    if (textAreaRef.current) {
      resizeObserver.observe(textAreaRef.current);
    }

    return () => {
      if (textAreaRef.current) {
        resizeObserver.unobserve(textAreaRef.current);
      }
    };
  }, [props.value]);

  return (
    <textarea
      {...props}
      ref={textAreaRef}
      className={`auto-expand-textarea ${props.className}`}
    />
  );
};

export default TextArea;