Commit 8173712a by Madhankumar

bug fix on task component

parent 6581c9f4
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
"id": 1 "id": 1
}, },
{ {
"title": "helloxxsxsxsxsxsxsxsxsxsxsxsxsxssssssssssssssssssssxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadddadsdsdsadasasasadasaadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaavrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrryyyyy", "title": "helloxxsxsxsxsxsxsxsxsxsxsxsxsxssssssssssssssssssssxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadddadsdsdsadasasasadasaadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaavrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrryyyyyrt",
"isCompleted": false, "isCompleted": false,
"id": 2 "id": 2
}, },
{ {
"title": "hello", "title": "rfrfrfredefefefefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"isCompleted": false, "isCompleted": false,
"id": 3 "id": 3
} }
......
import { FaTimesCircle } from "react-icons/fa"; import { FaTimesCircle } from "react-icons/fa";
import Checkbox from "../../base/checkbox"; import Checkbox from "../../base/checkbox";
import "./task.css"; import "./task.css";
import { import { useEffect, useRef, useState, useCallback } from "react";
useEffect,
useRef,
useLayoutEffect,
useState,
useCallback,
} from "react";
function Task({ id = 1, title, isCompleted, onChange, onDelete }) { function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
const [debouncetitle, setDebounceTitle] = useState(title); const [debouncetitle, setDebounceTitle] = useState(title);
...@@ -23,6 +17,8 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) { ...@@ -23,6 +17,8 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
} }
return false; return false;
}; };
// (useCallback)---> to avoid calling the checkbox component without any action applied on checkbox
const handleChange = useCallback(() => { const handleChange = useCallback(() => {
setIsCheck(!ischeck); setIsCheck(!ischeck);
onChange({ id, isCompleted: !ischeck }); onChange({ id, isCompleted: !ischeck });
...@@ -31,6 +27,7 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) { ...@@ -31,6 +27,7 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
const handleContent = (e) => { const handleContent = (e) => {
setDebounceTitle(e.target.value); setDebounceTitle(e.target.value);
const textarea = document.querySelector("textarea"); const textarea = document.querySelector("textarea");
//calculate the height of content based on scrollheight
textarea.addEventListener("keyup", (e) => { textarea.addEventListener("keyup", (e) => {
let scheight = e.target.scrollHeight; let scheight = e.target.scrollHeight;
textarea.style.height = `${scheight}px`; textarea.style.height = `${scheight}px`;
...@@ -39,6 +36,11 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) { ...@@ -39,6 +36,11 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
//debounce //debounce
useEffect(() => { useEffect(() => {
//checking the content height and set on page load
const scrollHeight = textarea_ref.current.scrollHeight;
textarea_ref.current.style.height = `${scrollHeight}px`;
//after 2 second it will execute
const handler = setTimeout(() => { const handler = setTimeout(() => {
if (textarea_ref.current.value != title) { if (textarea_ref.current.value != title) {
onChange({ id, title: debouncetitle }); onChange({ id, title: debouncetitle });
...@@ -50,10 +52,6 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) { ...@@ -50,10 +52,6 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
}; };
}, [debouncetitle]); }, [debouncetitle]);
useLayoutEffect(() => {
const scrollHeight = textarea_ref.current.scrollHeight;
textarea_ref.current.style.height = `${scrollHeight}px`;
}, [debouncetitle]);
return ( return (
<div className="task-section"> <div className="task-section">
<div> <div>
...@@ -69,7 +67,7 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) { ...@@ -69,7 +67,7 @@ function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
contentEditable={true} contentEditable={true}
value={debouncetitle} value={debouncetitle}
onChange={handleContent} onChange={handleContent}
disabled={ischeck} // disabled={ischeck}
></textarea> ></textarea>
</div> </div>
<FaTimesCircle className="fa-close" onClick={handleDelete} /> <FaTimesCircle className="fa-close" onClick={handleDelete} />
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
.task-section > div { .task-section > div {
display: flex; display: flex;
align-items: center; align-items: center;
width: 50%; width: 90%;
} }
.fa-close { .fa-close {
color: #ccc; color: #ccc;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment