| "use client";
|
| import { useEffect } from 'react';
|
|
|
| const ResponsiveTextarea = ({ maxRows = 10, ...props }) => {
|
| useEffect(() => {
|
| const textarea = document.getElementById('responsive-textarea');
|
|
|
| const adjustHeight = () => {
|
| textarea.style.height = 'auto';
|
| const lineHeight = parseInt(getComputedStyle(textarea).lineHeight);
|
| const rows = (textarea.scrollHeight / lineHeight)-2;
|
| console.log(rows);
|
|
|
| if (rows <= maxRows) {
|
| textarea.style.height = `${(textarea.scrollHeight)/2}px`;
|
| textarea.style.overflowY = 'hidden';
|
| } else {
|
| textarea.style.height = `${lineHeight * maxRows}px`;
|
| textarea.style.overflowY = 'auto';
|
| }
|
| };
|
|
|
| textarea.addEventListener('input', adjustHeight);
|
|
|
| textarea.addEventListener('change', adjustHeight);
|
|
|
|
|
| adjustHeight();
|
|
|
|
|
| return () => {
|
| textarea.removeEventListener('input', adjustHeight);
|
| };
|
| }, [maxRows]);
|
| useEffect(() => {
|
| const textarea = document.getElementById('responsive-textarea');
|
| const adjustHeight = () => {
|
| textarea.style.height = 'auto';
|
| const lineHeight = parseInt(getComputedStyle(textarea).lineHeight);
|
| const rows = (textarea.scrollHeight / lineHeight)-2;
|
| console.log(rows);
|
|
|
| if (rows <= maxRows) {
|
| textarea.style.height = `${(textarea.scrollHeight)/2}px`;
|
| textarea.style.overflowY = 'hidden';
|
| } else {
|
| textarea.style.height = `${lineHeight * maxRows}px`;
|
| textarea.style.overflowY = 'auto';
|
| }
|
| };
|
| adjustHeight();
|
| })
|
|
|
| return (
|
| <textarea
|
| id="responsive-textarea"
|
| style={{ resize: 'none', overflowY: 'hidden', display:'flex', alignItems:'center', justifyContent:'center' }}
|
| {...props}
|
| />
|
| );
|
| };
|
|
|
| export default ResponsiveTextarea;
|
|
|