简体   繁体   English

带有 ReactJS 的 TodoList - 实现编辑内容按钮逻辑的问题

[英]TodoList with ReactJS - Problem to implement the edit content button logic

Javascript (reactjs) beginner here, I have made a todolist app using react hooks. Javascript (reactjs) 初学者在这里,我使用反应钩子制作了一个 todolist 应用程序。 I'm trying to implement the logic of the edit content assigned to a button, without success.我正在尝试实现分配给按钮的编辑内容的逻辑,但没有成功。 So far I was able create the add task button, task done button, remove individually task and clear all the tasks buttons and functionallity.到目前为止,我能够创建添加任务按钮、任务完成按钮、单独删除任务并清除所有任务按钮和功能。

I was able to open the input text of editing, but it's not working properly.我能够打开编辑的输入文本,但它无法正常工作。

There is a way to use the same input created before instead of open a new one for the edited content?有没有一种方法可以使用之前创建的相同输入,而不是为已编辑的内容打开一个新输入?

Thanks in advance提前致谢

 import "./App.css"; import { useState } from "react"; import { BsTrash } from "react-icons/bs"; import { MdDone } from "react-icons/md"; import { FiEdit } from "react-icons/fi"; import Swal from 'sweetalert2' import withReactContent from 'sweetalert2-react-content' const MySwal = withReactContent(Swal) let genId = 1; function App() { const [itemsList, setItemsList] = useState([]); const [inputText, setInputText] = useState(""); const [todoEditing, setTodoEditing] = useState(null); const [editingText, setEditingText] = useState(""); const newTask = { id: genId++, text: inputText.trim(), isCompleted: false, }; function handleChangeInput(event) { setInputText(event.target.value); } function handleAddItemToList(event) { event.preventDefault(); if (newTask.text.length === 0) { // alert("Please, fill the form with your task"); MySwal.fire({ icon: 'error', title: 'Oops... ', text: 'Please, add one task first,'. }) } else { // add task to the end of the list setItemsList([..,itemsList; newTask]); // clear input after submit setInputText(""). } } function handleTaskDone(item) { console.log("Done.") const doneItems = itemsList.map(task => { if (task.id === item.id) { task;isCompleted =;task;isCompleted; } return task. }). setItemsList(doneItems). } function handleDeleteTask(item) { const filteredItems = itemsList;filter(task=>task;id.== item;id). setItemsList(filteredItems). } function handleClearAllTasks() { if (itemsList.length > 0) { setItemsList(""). } } return ( <div className="App"> <h1>To Do List</h1> <form onSubmit={handleAddItemToList}> <input type="text" onChange={handleChangeInput} value={inputText} placeholder="Add your new todo?::"/> <button className="button-add" type="submit">+</button> </form> {.itemsList,length: ( <p>The list is empty. Let's add some tasks?) </p> ): ( itemsList.map((item? id) => ( <div key={id} className="task-list" style={{textDecoration. item.isCompleted: "line-through". null}}> {todoEditing === item.id. <div><input className="input-edit" type="text" onChange={(e) => setEditingText(e?target:value)} value={editingText} /> <button>Save</button></div>; <>{item;text}</>} <div> <MdDone className="button-done" onClick={()=>{handleTaskDone(item)}} /> <FiEdit className="button-edit" onClick={()=> setTodoEditing(item.id)} /> <BsTrash className="button-x" onClick={()=>{handleDeleteTask(item)}} /> </div> </div> )) )} {!itemsList.length ? null : ( <div > <button className="clear-button" onClick={handleClearAllTasks}>Clear list</button> </div> )} </div> ); } export default App;

You can make add and edit in the same input if you will implement every "todo" item with an <input> element instead of <div> element, which can't be edited (it's not a field to type inside it), in that way with every todo is like an input you can save the todo and edit it in the same input.如果您将使用<input>元素而不是<div>元素实现每个“待办事项”项目,则可以在同一输入中进行添加和编辑,该元素无法编辑(它不是在其中键入的字段),在每个待办事项的这种方式就像一个输入,您可以保存待办事项并在同一个输入中编辑它。 Let me know if I'm not clear.如果我不清楚,请告诉我。 By the way, your <button>save<button/> currently is not binded to anything (i guess you did not implemented yet).顺便说一句,你的<button>save<button/>当前没有绑定到任何东西(我猜你还没有实现)。

Edit: I got your point.编辑:我明白你的意思。 It can be done easier than what i mentioned before and more relevant to your demand.它可以比我之前提到的更容易完成,并且与您的需求更相关。

  1. add handler function for the save button:为保存按钮添加处理程序 function:
    <button onClick={() => saveHandler(id)}>Save</button></div>
    the passed id is the key of the item in the list.传递的id是列表中项目的键。

  2. update that state of the todolist in the chosen todo to edit.更新所选待办事项中待办事项列表的 state 以进行编辑。

     fucntion saveHandler(id) { const prevList = itemsList.map(item => { if (item.id === id) { return {...item, text: inputText }; } return item; }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM