From 98126bb58effc79439d235d02cc8f9f2028dda48 Mon Sep 17 00:00:00 2001 From: Yaro Shm Date: Sat, 9 Nov 2024 20:56:03 +0100 Subject: [PATCH] stimulus ctrl+enter to submit form --- app/javascript/controllers/form_controller.js | 9 +++++++++ app/views/posts/_form.html.erb | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 app/javascript/controllers/form_controller.js diff --git a/app/javascript/controllers/form_controller.js b/app/javascript/controllers/form_controller.js new file mode 100644 index 0000000..1ea7fb2 --- /dev/null +++ b/app/javascript/controllers/form_controller.js @@ -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() + } +} diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb index 71c6207..b639b22 100644 --- a/app/views/posts/_form.html.erb +++ b/app/views/posts/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: post) do |form| %> +<%= form_with(model: post, data: {controller: "form"}) do |form| %> <% if post.errors.any? %>

<%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:

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