DEV Community

Daveyon Mayne 😻
Daveyon Mayne 😻

Posted on

4 2

Using Draft.js as a single-line input

Hey all 👋🏼 Today I want to share with you a very simple code on how to use Draft.js as a single line input.

import Draft, { Editor, ... } from 'draft-js'

const keyBindingFn = (e) => {
  if (!e.metaKey && e.code === 'Enter') {
    // Function to execute...
    return false
  }

  // Return Draft's default command for this key.
  return Draft.getDefaultKeyBinding(e)
}

<Editor 
  [..]
  keyBindingFn={keyBindingFn}
/>
Enter fullscreen mode Exit fullscreen mode

The code above will add line breaks should the user presses the command key (mac) + enter key. Nothing will happen if the enter button alone is pressed. You could then execute a function to save the entered value.

Note the user could copy/paste inside the editor. For my setup, I'm ok with that.

✌🏼

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay