gpt4 book ai didi

javascript - React.js 使用相同的输入文本框编辑待办事项

转载 作者:行者123 更新时间:2023-11-29 23:26:53 25 4
gpt4 key购买 nike

我实现了简单的单页 Todo 应用程序,具有添加、编辑和删除功能。大部分部分已经完成,但我卡在了一点。当我点击编辑按钮时,我想用点击行的值填充顶部的输入文本框,然后在编辑后我想在 ListView 中更新。你能指导我吗?

我创建了一个 fiddle 来解释它。

这是我的仓库: https://github.com/JigneshRaval/react-todo-app

JSFiddle 网址: https://jsfiddle.net/jigneshraval/3b3dabr6/20/

还在下面粘贴了可以通过单击“运行代码片段”按钮运行的工作代码。

const Title = ({ todoCount }) => {
return (
<div>
<div>
<h1>Todos: ({todoCount})</h1>
</div>
</div>
);
}

var randomString = function (length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}


class TodoForm extends React.Component {
// Input Tracker
constructor(props) {
super(props);
console.log(props.editTodo.title);
this.state = { value: props.editTodo.title };
this.input;
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
this.setState({
value: event.target.value
})
}

handleSubmit(event) {
event.preventDefault();
console.log(event.target);
let newTodoTitle = event.target.querySelector('input');
this.props.addTodo(this.input.value);
newTodoTitle.value = '';
}

renderAddTodoForm() {
// Return JSX
return (
<form onSubmit={this.handleSubmit}>
{/*<form onSubmit={(e) => {
e.preventDefault();
this.props.addTodo(this.input.value);
this.input.value = '';
}}>*/}
<input className="form-control col-md-12 add-form"
ref={(input) => this.input = input}
value={this.state.value}
onChange={this.handleChange.bind(this)} />
<br />
</form>
);
}

render() {
return this.renderAddTodoForm();
}
}


// 3. Single Todo
// ================================
const Todo = ({ todo, remove, edit }) => {
// Each Todo
return (
<li className="list-group-item">
<a href="#" data-todoid={todo._id} name="todoTitle" data-toggle="tooltip" data-placement="top" title="Click on item to delete.">{todo.title} = {todo.status}</a>

<button className="btn btn-danger float-right" onClick={() => { remove(todo._id) }}>Delete</button>
<button className="btn btn-primary float-right" onClick={() => { edit(todo._id) }}>Edit</button>
</li>);
}

// 2. Todo List
// ================================
const TodoList = ({ todos, remove, edit }) => {
// Map through the todos
const todoNode = todos.map((todo) => {
return (<Todo todo={todo} key={todo._id} remove={remove} edit={edit} />)
});

return (<ul className="list-group" style={{ marginTop: '30px' }}>{todoNode}</ul>);
}

// 1. Main TODO App
// ================================
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{ "title": "Buy Milk", "status": "pending", "_id": "3QACIouhZZlhmm6T" },
{ "title": "Buy new computer book", "status": "pending", "_id": "9Xz2MSHIeh87WMgF" },
{ "title": "Fetch Money", "status": "pending", "_id": "DZvP2o5Dd4t9J3Ax" },
{ "title": "Play new game", "status": "pending", "_id": "Dp3J6BacTeG8ijV8" },
{ "title": "new 2", "status": "pending", "_id": "ECDUmbjOt4vtOKSv" },
{ "title": "sdfsdafsaf", "status": "pending", "_id": "GwHBilbjsbXipQuI" },
{ "title": "New 10", "status": "pending", "_id": "HGWGsNEVFNXIGZ8S" },
{ "title": "ppp", "status": "pending", "_id": "IExYBTFUFkGoHqyN" }
],
isEditing: false,
editTodo: {}
}
}

// Add Todo
addTodo(value) {
this.state.data.push({ "title": value, "status": "pending", "today": { "$$date": Date.now() }, "_id": randomString(16) });
this.setState({ data: this.state.data });
}

// Edit Todo
editTodo(todoId) {
const remainder = this.state.data.filter((todo) => {
if (todo._id === todoId) return todo;
});
this.setState({ isEditing: true, editTodo: remainder[0] });
}

// Remove Todo
removeTodo(id) {
// Filter all todos except the one to be removed
const remainder = this.state.data.filter((todo) => {
if (todo._id !== id) return todo;
});
this.setState({ data: remainder });
}


handleInputChange(newValue) {
console.log(newValue);
}

render() {
return (
<main>
<div className="container">
<Title todoCount={this.state.data.length} />
<TodoForm isEditing={this.state.isEditing} handleInputChange={this.handleInputChange.bind()} editTodo={this.state.editTodo} addTodo={this.addTodo.bind(this)} />
<TodoList
todos={this.state.data}
remove={this.removeTodo.bind(this)}
edit={this.editTodo.bind(this)}
/>
</div>
</main>
)
}
}

ReactDOM.render(
<TodoApp />,
document.getElementById('app')
);
<script src="https://unpkg.com/react@16.2.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.2.0/umd/react-dom.development.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />

<div id="app"></div>

谁能指导我。

最佳答案

您需要向您的 ToDoForm 组件添加一个生命周期 Hook ,如下所示。但是在 setState 中添加条件,使其仅在 next 与 this.state.value 不同时调用

componentWillReceiveProps (next) {
this.setState({value: next.editTodo.title});
}

这将填充表单中的文本。您可以在此行之后进行进一步的更改。

请注意我们应该使用 componentWillReceiveProps 而不是 componentWillUpdate 来设置状态

关于javascript - React.js 使用相同的输入文本框编辑待办事项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48866315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com