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

React Quill - How to prevent default behaviour of enter key? #936

Closed
frdomovic opened this issue Dec 13, 2023 · 3 comments
Closed

React Quill - How to prevent default behaviour of enter key? #936

frdomovic opened this issue Dec 13, 2023 · 3 comments

Comments

@frdomovic
Copy link

React Quill - How to prevent default behaviour of enter key?

Using ReactQuill v2.0.0-beta.2 or v2.0.0

  onKeyDown(e) {
    if (e.key === 'Enter') {
      e.preventDefault();
      return false; // does nothing
    }
  ...
   <ReactQuill onKeyDown={onKeyDown.bind(this)} ... />

Doesn't prevent default behaviour and while using bindings like this:

<ReactQuill
      modules={{
        keyboard: {
          bindings: {
                  enter: {
                      key: 13,
                      handler: () => {/* do nothing */}
                  }
              }
      }
      }}
     ...
    />

Causes TypeError: Cannot read properties of undefined (reading 'delta')

@frdomovic
Copy link
Author

see 919

@tchen-l
Copy link

tchen-l commented Apr 29, 2024

try this

<div
  onKeyDownCapture={(e) => {
    if (e.code === 'Enter') {
      e.preventDefault()
    }
  }}
>
  <ReactQuill />
</div>

@Hujinna
Copy link

Hujinna commented May 13, 2024

try this

<div
  onKeyDownCapture={(e) => {
    if (e.code === 'Enter') {
      e.preventDefault()
    }
  }}
>
  <ReactQuill />
</div>

It wokers well.

const modules = useMemo(
   () => ({
     toolbar: false,
     keyboard: {
       bindings: {
         linebreak: {
           key: 13,
           shiftKey: true,
           handler: (range: { index: number }) => {
             const editor = ReactQuillRef.current?.getEditor();
             editor?.setSelection(range.index, 'silent');
             editor?.insertText(range.index, '\n', 'user');
             editor?.setSelection(range.index + 1, 'silent');
             editor?.format('linebreak', true, 'user');
           },
        },
   }),
  [ReactQuillRef]
 );
 const handleKeyDown = (e) => {
   if (e.keyCode === 13 && (e.shiftKey || e.metaKey || e.ctrlKey)) {
     console.log('换行');
   } else if (e.code === 'Enter' && !isTyping) {
     e.preventDefault();
     e.stopPropagation();
     handleSend();
   }
 };
<div
     onKeyDown={handleKeyDown}
     onCompositionStart={() => setIsTyping(true)}
     onCompositionEnd={() => setIsTyping(false)}
   >
     <ReactQuill
       ref={ReactQuillRef}
       theme="snow"
       value={text}
       formats={['bold', 'italic', 'underline', 'video', 'image', 'link']}
       modules={modules}
       onChange={setText}
     />
</div>

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

3 participants