Answers for "react js edit text on doubleclick"

13

React edit text on doubleclick

//https://flaviocopes.com/react-edit-doubleclick/

const [toggle, setToggle] = useState(true)
const [name, setName] = useState('test')

...

toggle ? (
  <p
    onDoubleClick={() => {
      setToggle(false)
    }}
  >{name}</p>
) : (
  <input
    type='text'
    value={project.status}
  />
)}

<input
  type="text"
  value={name}
  onChange={(event) => {
    setName(name)
  }}
  onKeyDown={(event) => {
    if (event.key === 'Enter' || event.key === 'Escape') {
      setToggle(true)
      event.preventDefault()
      event.stopPropagation()
    }
  }}
/>
Posted by: Guest on February-22-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language