import React, { Fragment, Component } from "react"; type InputState = { value: string; edit: boolean; }; type Props = { value: string; onChange: (value: string) => void; }; export class EditableText extends Component { constructor(props: Props) { super(props); this.state = { value: props.value, edit: false }; } UNSAFE_componentWillReceiveProps(props: Props) { this.setState({ value: props.value }); } private handleEdit(e: React.ChangeEvent) { this.setState({ value: e.target.value }); } private handleBlur() { const { value } = this.state; if (!value) { this.setState({ value: this.props.value, edit: false }); return; } this.props.onChange(value); this.setState({ edit: false }); } public render() { const { value, edit } = this.state; return ( {edit ? ( this.handleEdit(e)} onBlur={() => this.handleBlur()} onKeyDown={e => { if (e.key === "Enter") { this.handleBlur(); } }} autoFocus /> ) : ( this.setState({ edit: true })} className="project-name" > {value} )} ); } }