【发布时间】:2020-07-25 14:37:43
【问题描述】:
我可以专注于输入并修改文本。但是当我按下回车时,文本根本没有改变。我把代码留在后面。我有一份待办事项清单和一份已完成清单。当我编辑待办事项列表中的文本并按 Enter 键时,修改后的值会出现在我的控制台中,但是当我将待办事项发送到完成列表时返回原始值/文本
import React from "react";
import "../list.css";
class List extends React.Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
this.state = {
isEditing: false,
textInput: this.props.value,
};
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
this.textInput.current.focus();
}
// handleOnSubmit(event) {
// event.preventDefault();
// let aa = this.state.textInput;
// if (aa !== "") {
// this.setState({ items: [...this.props.items, aa] });
// console.log(aa);
// }
// }
// componentDidUpdate() {
// this.handleOnSubmit();
// }
onChange = (event) => {
// const textInput = this.textInput;
this.setState({ textInput: event.target.value });
};
componentDidMount() {
const editingState = true;
if (!this.state.isEditing) {
this.setState({ isEditing: editingState });
}
}
render() {
return (
<ul>
{this.props.title}
{this.props.items.map((listItem, index) => {
return (
<li id="list-item" key={index}>
<form onSubmit={(event) => this.onEdit(event)}>
<input
type="text"
ref={this.textInput}
onChange={(event) => {
this.onChange(event);
}}
defaultValue={listItem}
/>
</form>
<button onClick={() => this.props.onAction(listItem)}>
{this.props.label}
</button>
<button onClick={() => this.props.onDeleteItem(listItem)}>
Delete
</button>
<button onClick={() => this.props.onSortItem(listItem)}>
Sort
</button>
<button onClick={() => this.focusTextInput()}>Edit</button>
</li>
);
})}
</ul>
);
}
}
export default List;
import React from "react";
import Form from "./components/Form";
import List from "./components/List";
import "./App.css";
class App extends React.Component {
state = {
todoList: [],
doneList: [],
};
addTodo = (value) => {
if (value !== "") {
this.setState({ todoList: [...this.state.todoList, value] });
}
};
handleTodo = (value) => {
const todoList = this.state.todoList.filter(
(listValue) => listValue !== value
);
const doneList = [...this.state.doneList, value];
this.setState({ todoList, doneList });
};
handleDone = (value) => {
const doneList = this.state.doneList.filter(
(listValue) => listValue !== value
);
const todoList = [...this.state.todoList, value];
this.setState({ todoList, doneList });
};
deleteItem = (value) => {
const doneList = this.state.doneList.filter(
(listValue) => listValue !== value
);
const todoList = this.state.todoList.filter(
(listValue) => listValue !== value
);
this.setState({ doneList, todoList });
};
sortItem = (value) => {
const todoList = [value].concat(
this.state.todoList.filter((item) => item !== value)
);
this.setState({ todoList });
};
editText = (event) => {
event.preventDefault();
let aa = this.props.textInput;
if (aa !== "") {
this.setState({ todoList: [...this.state.todoList, aa] });
console.log(aa);
}
};
render() {
return (
<div className="App">
<Form onSubmit={this.addTodo} />
<List
onDeleteItem={this.deleteItem}
onAction={this.handleTodo}
items={this.state.todoList}
onSortItem={this.sortItem}
onEdit={this.editText}
title="Todo"
label="Done"
/>
<List
onDeleteItem={this.deleteItem}
onAction={this.handleDone}
items={this.state.doneList}
title="Done"
label="Todo"
/>
</div>
);
}
}
export default App;
【问题讨论】:
-
请发一个codeandbox示例项目让我们更好地帮助您