Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using "Enter" to add a new line does not work as expected #182

Open
llccrr opened this issue Nov 25, 2019 · 9 comments
Open

Using "Enter" to add a new line does not work as expected #182

llccrr opened this issue Nov 25, 2019 · 9 comments

Comments

@llccrr
Copy link

llccrr commented Nov 25, 2019

I tried the library using this codesandbox: https://codesandbox.io/s/l91xvkox9l

If I use Shift + Enter to add a new line it works.
If I use Enter to add a new line, then click away to on blur it, it removes lines that were created.
Basically:
"AAAAA
aa
AA"
becomes "AAAAAaaAA"

browser: Chrome - version 78.0.3904.97

@lovasoa
Copy link
Owner

lovasoa commented Nov 25, 2019

What browser are you using ? Do you observe this behavior only when the contenteditable element is is managed by react-contenteditable ?

@llccrr
Copy link
Author

llccrr commented Nov 25, 2019

What browser are you using ?

I edited my post with the browser version.

Do you observe this behavior only when the contenteditable element is is managed by react-contenteditable ?

I did not try to manage a contenteditable without it, I wanted to avoid doing that by using this library.

@llccrr
Copy link
Author

llccrr commented Nov 25, 2019

I just give it a quick try, using Enter key in an editable div seems to work, multi-lines are kept.

@lovasoa
Copy link
Owner

lovasoa commented Nov 25, 2019

Bon, essayons en français 😛

Quel navigateur utilisez-vous ? Firefox, chrome, safari... ? Et quelle version ?

Pouvez-vous créer un élément avec la propriété contenteditable en dehors de react-contenteditable, et indiquer si vous constatez le même problème ?

@llccrr
Copy link
Author

llccrr commented Nov 25, 2019

ça ne changera pas vraiment ma réponse, je pense que c'était compréhensible :(

Quel navigateur utilisez-vous ? Firefox, chrome, safari... ? Et quelle version ?

J'ai édité mon post avec la version du navigateur, à savoir Chrome v78.

Pouvez-vous créer un élément avec la propriété contenteditable en dehors de react-contenteditable, et indiquer si vous constatez le même problème ?

J'ai essayé rapidement de créer une div avec contenteditable, les sauts à la ligne effectués avec Entrée sont bien gardés.

@llccrr
Copy link
Author

llccrr commented Nov 25, 2019

I just figure out that it was because of a "display: flex".

@llccrr llccrr closed this as completed Nov 25, 2019
@llccrr llccrr reopened this Nov 25, 2019
@llccrr
Copy link
Author

llccrr commented Nov 25, 2019

My bad.. the bug is still present.
I can confirm you that this behavior is not present when using the contenteditable property outside of react-contenteditable

@dimaaan21
Copy link

dimaaan21 commented Mar 8, 2020

@llccrr , I had the same problem.
For solving this i've just used React.useRef(null) instead of React.createRef() and all became OK.

Reason (in my case): during every re-render <ContentEditable> received new ref that's why caret were replaced at the end only and new line weren't created.

@morinted
Copy link

My solution involves using white-space: pre-wrap; and then controlling the enter key in order to prevent the insertion of div/p:

<ContentEditable
    onKeyDown={(event) => {
        if (event.key === 'Enter') {
          document.execCommand('insertLineBreak')
          event.preventDefault()
        }
      }}
/>

Dorian-Grim added a commit to Dorian-Grim/utm-info-quizz that referenced this issue Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants