import React from 'react'; import ReactDOM from 'react-dom'; import classNames from 'classnames'; const ESCAPE_KEY = 27; const ENTER_KEY = 13; export default class TodoItem extends React.Component { constructor(props) { super(props); this.state = { editText: props.todo.title }; this.handleEdit = this.handleEdit.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleKeyDown = this.handleKeyDown.bind(this); this.handleChange = this.handleChange.bind(this); } shouldComponentUpdate(nextProps, nextState) { return ( nextProps.todo !== this.props.todo || nextProps.editing !== this.props.editing || nextState.editText !== this.state.editText ); } componentDidUpdate(prevProps) { if (!prevProps.editing && this.props.editing) { const node = ReactDOM.findDOMNode(this.refs.editField); node.focus(); node.setSelectionRange(node.value.length, node.value.length); } } handleSubmit() { const val = this.state.editText.trim(); if (val) { this.props.onSave(val); this.setState({editText: val}); } else { this.props.onDestroy(); } } handleEdit() { this.props.onEdit(); this.setState({editText: this.props.todo.title}); } handleKeyDown(event) { if (event.which === ESCAPE_KEY) { this.setState({editText: this.props.todo.title}); this.props.onCancel(event); } else if (event.which === ENTER_KEY) { this.handleSubmit(event); } } handleChange(event) { if (this.props.editing) { this.setState({editText: event.target.value}); } } render() { return (