2018-12-22 17:20:04 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import TodoTextInput from './TodoTextInput';
|
|
|
|
|
|
|
|
export default class TodoItem extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
todo: PropTypes.object.isRequired,
|
|
|
|
editTodo: PropTypes.func.isRequired,
|
|
|
|
deleteTodo: PropTypes.func.isRequired,
|
2020-08-08 23:26:39 +03:00
|
|
|
markTodo: PropTypes.func.isRequired,
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = {
|
2020-08-08 23:26:39 +03:00
|
|
|
editing: false,
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDoubleClick = () => {
|
|
|
|
this.setState({ editing: true });
|
2019-01-10 21:51:14 +03:00
|
|
|
};
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
handleSave = (id, text) => {
|
|
|
|
if (text.length === 0) {
|
|
|
|
this.props.deleteTodo(id);
|
|
|
|
} else {
|
|
|
|
this.props.editTodo(id, text);
|
|
|
|
}
|
|
|
|
this.setState({ editing: false });
|
2019-01-10 21:51:14 +03:00
|
|
|
};
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { todo, markTodo, deleteTodo } = this.props;
|
|
|
|
|
|
|
|
let element;
|
|
|
|
if (this.state.editing) {
|
|
|
|
element = (
|
|
|
|
<TodoTextInput
|
|
|
|
text={todo.text}
|
|
|
|
editing={this.state.editing}
|
2020-08-08 23:26:39 +03:00
|
|
|
onSave={(text) => this.handleSave(todo.id, text)}
|
2018-12-22 17:20:04 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
element = (
|
2019-01-10 20:23:33 +03:00
|
|
|
<div className="view">
|
2018-12-22 17:20:04 +03:00
|
|
|
<input
|
2019-01-10 20:23:33 +03:00
|
|
|
className="toggle"
|
|
|
|
type="checkbox"
|
2018-12-22 17:20:04 +03:00
|
|
|
checked={todo.marked}
|
|
|
|
onChange={() => markTodo(todo.id)}
|
|
|
|
/>
|
2019-01-10 20:23:33 +03:00
|
|
|
<label htmlFor="text" onDoubleClick={this.handleDoubleClick}>
|
2018-12-22 17:20:04 +03:00
|
|
|
{todo.text}
|
|
|
|
</label>
|
2019-01-10 21:51:14 +03:00
|
|
|
<button className="destroy" onClick={() => deleteTodo(todo.id)} />
|
2018-12-22 17:20:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
className={classnames({
|
|
|
|
completed: todo.marked,
|
2020-08-08 23:26:39 +03:00
|
|
|
editing: this.state.editing,
|
2018-12-22 17:20:04 +03:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{element}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|