Skip to content

Commit

Permalink
[Fixed] onChange event issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sayshark75 committed Oct 5, 2023
1 parent 5dc43fa commit a28eba1
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions src/components/Todo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect, useRef, useState } from "react";


function usePrevious(value) {
const ref = useRef();
useEffect(() => {
Expand All @@ -11,7 +10,7 @@ function usePrevious(value) {

export default function Todo(props) {
const [isEditing, setEditing] = useState(false);
const [newName, setNewName] = useState('');
const [newName, setNewName] = useState("");

const editFieldRef = useRef(null);
const editButtonRef = useRef(null);
Expand All @@ -22,6 +21,14 @@ export default function Todo(props) {
setNewName(e.target.value);
}

function handleBlur(e) {
if (e.target.value === "") {
setNewName(props.name);
return;
}
setNewName(e.target.value);
}

function handleSubmit(e) {
e.preventDefault();
if (!newName.trim()) {
Expand All @@ -42,18 +49,17 @@ export default function Todo(props) {
id={props.id}
className="todo-text"
type="text"
value={newName || props.name}
value={newName || ""}
onChange={handleChange}
onBlur={handleBlur}
ref={editFieldRef}
/>
</div>
<div className="btn-group">

<button
type="button"
className="btn todo-cancel"
onClick={() => setEditing(false)}
>
onClick={() => setEditing(false)}>
Cancel
<span className="visually-hidden">renaming {props.name}</span>
</button>
Expand All @@ -68,37 +74,34 @@ export default function Todo(props) {
const viewTemplate = (
<div className="stack-small">
<div className="c-cb">
<input
id={props.id}
type="checkbox"
defaultChecked={props.completed}
onChange={() => props.toggleTaskCompleted(props.id)}
/>
<label className="todo-label" htmlFor={props.id}>
{props.name}
</label>
</div>
<div className="btn-group">
<input
id={props.id}
type="checkbox"
defaultChecked={props.completed}
onChange={() => props.toggleTaskCompleted(props.id)}
/>
<label className="todo-label" htmlFor={props.id}>
{props.name}
</label>
</div>
<div className="btn-group">
<button
type="button"
className="btn"
onClick={() => setEditing(true)}
ref={editButtonRef}
>
Edit <span className="visually-hidden">{props.name}</span>
</button>
<button
type="button"
className="btn btn__danger"
onClick={() => props.deleteTask(props.id)}
>
Delete <span className="visually-hidden">{props.name}</span>
</button>
</div>
ref={editButtonRef}>
Edit <span className="visually-hidden">{props.name}</span>
</button>
<button
type="button"
className="btn btn__danger"
onClick={() => props.deleteTask(props.id)}>
Delete <span className="visually-hidden">{props.name}</span>
</button>
</div>
</div>
);


useEffect(() => {
if (!wasEditing && isEditing) {
editFieldRef.current.focus();
Expand All @@ -108,6 +111,5 @@ export default function Todo(props) {
}
}, [wasEditing, isEditing]);


return <li className="todo">{isEditing ? editingTemplate : viewTemplate}</li>;
}

0 comments on commit a28eba1

Please sign in to comment.