my blade file
<form class="new-chat-form border-gradient" wire:submit.prevent="sendMessage" enctype="multipart/form-data">
<textarea wire:model="inputMessage" id="message" rows="1" placeholder="Send a message..."
onkeydown="handleKeyDown(event)" id="search-form"></textarea>
<button type="submit" class="form-icon icon-send sendButton">
<i class="feather-send"></i>
</button>
</div>
</form>
<p class="b3 small-text">{{ config('app.name') }} can make mistakes. Consider checking important information.
</p>
</div>
</div>
@script
<script>
function handleKeyPress(event) {
const textarea = event.target;
// Check if Enter key is pressed without Shift
if (event.key === 'Enter' && event.shiftKey === false) {
event.preventDefault();
@this.call('sendMessage');
}
// Check if Enter key is pressed with Shift
else if (event.key === 'Enter' && event.shiftKey === true) {
event.preventDefault();
const cursorPosition = textarea.selectionStart;
textarea.value =
textarea.value.slice(0, cursorPosition) +
'n' +
textarea.value.slice(cursorPosition);
textarea.selectionStart = textarea.selectionEnd = cursorPosition + 1;
@this.set('inputMessage', textarea.value);
}
}
</script>
@endscript
i have tried this method but it says in console undefined function
and then i found livewire key modifiers , i tried them also
<form class="new-chat-form border-gradient" wire:submit.prevent="sendMessage" enctype="multipart/form-data">
<textarea wire:model="inputMessage" id="message" rows="1" placeholder="Send a message..."
wire:keydown.enter="sendMessage" wire:keydown.shift.enter="addNewLine" id="search-form"></textarea>
<button type="submit" class="form-icon icon-send sendButton">
<i class="feather-send"></i>
</button>
</div>
</form>
but while pressing enter form submits but when i click shift+enter then both function runs, sendMessage and addNewLine
how i can do form submit and add new line please help im stucked with this for a day
2
Answers
wire:keydown.shift.enter
event isn’t exclusive. It still triggerswire:keydown.enter
since the key event containsEnter
.you can make javascript batter to perform your logic
blade
JavaScript
Enter
withoutShift
. The form should submit, andsendMessage
should be called.Shift + Enter
. A new line should be added in the textarea without submitting the form.This is a very simple soluton using Alpine:
When the shift key is pressed togheter with the enter key a newline is inserted at the current position as expected, otherwise the enter key causes a call to the backend sendMessage() method via the $wire object