Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
stimulus ctrl+enter to submit form
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Nov 9, 2024
1 parent 06490c0 commit 98126bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/javascript/controllers/form_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="form"
export default class extends Controller {
submit(event) {
event.preventDefault()
this.element.requestSubmit()
}
}
4 changes: 2 additions & 2 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: post) do |form| %>
<%= form_with(model: post, data: {controller: "form"}) do |form| %>
<% if post.errors.any? %>
<div style="color: red">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
Expand All @@ -13,7 +13,7 @@

<div>
<%= form.label :body, style: "display: block" %>
<%= form.textarea :body %>
<%= form.textarea :body, data: {action: "keydown.ctrl+enter->form#submit keydown.meta+enter->form#submit"} %>
</div>

<div>
Expand Down

0 comments on commit 98126bb

Please sign in to comment.