Created
January 13, 2021 10:51
-
-
Save timseriakov/c9820241111fa438db6cdcdd4e8ea71b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {ChangeEvent, useState} from 'react'; | |
import {TextField} from '@material-ui/core'; | |
type EditableSpanPropsType = { | |
value: string | |
onChange: (newValue: string) => void | |
} | |
export const EditableSpan = React.memo(function (props: EditableSpanPropsType) { | |
console.log("EditableSpan called"); | |
let [editMode, setEditMode] = useState(false); | |
let [title, setTitle] = useState(props.value); | |
const activateEditMode = () => { | |
setEditMode(true); | |
setTitle(props.value); | |
} | |
const activateViewMode = () => { | |
setEditMode(false); | |
props.onChange(title); | |
} | |
const changeTitle = (e: ChangeEvent<HTMLInputElement>) => { | |
setTitle(e.currentTarget.value) | |
} | |
return editMode | |
? <TextField value={title} onChange={changeTitle} autoFocus onBlur={activateViewMode} /> | |
: <span onDoubleClick={activateEditMode}>{props.value}</span> | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment