Skip to content

Instantly share code, notes, and snippets.

@achukka
Last active May 13, 2021 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achukka/ec9a36de5a77596ff9b31c2d9490830e to your computer and use it in GitHub Desktop.
Save achukka/ec9a36de5a77596ff9b31c2d9490830e to your computer and use it in GitHub Desktop.
Gist for AddItem in ToDoList project
import React from "react";
import { Item } from "./ToDoList";
const isValid = (item: Item): boolean => {
return item.task !== "" && item.priority !== -1;
};
class AddItem extends React.Component<{ addItem: any }, Item> {
constructor(props: any) {
super(props);
this.state = {
task: "",
priority: -1,
};
this.setTask = this.setTask.bind(this);
this.setPriority = this.setPriority.bind(this);
this.addItem = this.addItem.bind(this);
}
setTask(evt: any) {
this.setState({
task: evt.target.value,
});
}
setPriority(evt: any) {
this.setState({
priority: parseInt(evt.target.value),
});
}
addItem(evt: any) {
const item = this.state;
if (isValid(item)) {
this.props.addItem(item);
}
this.setState({
task: "",
priority: -1,
});
}
render() {
return (
<table>
<tbody>
<tr key={""}>
<td>Task:</td>
<td>
<input
id="task"
type="text"
placeholder="Enter task here"
onChange={this.setTask}
/>
</td>
<td>Priority:</td>
<td>
<input
id="prioity"
type="text"
placeholder="Enter priority here"
onChange={this.setPriority}
/>
</td>
<td>
<input id="submit" type="submit" onClick={this.addItem} />
</td>
</tr>
</tbody>
</table>
);
}
}
export default AddItem;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment