How to prevent quill mention triggering "Enter" on keyDown event handler? #295
-
Hello I want to prevent quill mention triggering the "Enter" keyCode in the keyDown listener after selecting item in the list. What's happening here is when user press enter in the mention list it's automatically sending the message to the server which is not correct. Is there a way or another approach to resolve my issue?
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
@jaypeetancero you can use Quill-Mention's callback to find out if the dropdown is open or closed in order to know if you can make the submit.
Hoping that this helps you |
Beta Was this translation helpful? Give feedback.
-
@GitHubish , Thank you for your comment. I tried your code but still didn't work but what I did is instead of using onOpen and onClose props, I use the onSelect props available then I imitate your idea to set if user selected an data in the list here's the code for others to check
Thank you for the idea |
Beta Was this translation helpful? Give feedback.
-
bump?? let me just say i have had trouble getting the keydown to recognize the "isMentionListOpen" state correctly. what happens for me is that the state setters set the value to closed before the keydown function runs, so it still goes through. what worked for me was putting a timeout on the state setter in the onClose so that it wouldn't set the state to closed before the enter keydown handler ran. |
Beta Was this translation helpful? Give feedback.
-
This is how I handle it: const Mention = Quill.import('modules/mention');
class CustomMention extends Mention {
hideMentionList() {
this.options.onBeforeClose();
this.mentionContainer.style.display = 'none';
this.mentionContainer.remove();
this.quill.root.removeAttribute('aria-activedescendant');
setTimeout(() => this.setIsOpen(false), 0);
}
}
Quill.register('modules/mention', CustomMention, true); const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey && ref.current) {
const editor = ref.current.getEditor();
const isOpenMention = Boolean(editor.getModule('mention')?.isOpen);
if (isOpenMention) return;
// do something...
}
}; |
Beta Was this translation helpful? Give feedback.
This is how I handle it: