From 6ed96bae857ec820a1db0d682346a10b11d57ff5 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 11 Jan 2024 15:00:15 +1100 Subject: [PATCH] Submit image when selected This could have been done with a tiny amount of inline JS, but I went this way in case I needed any special logic for UJS. It turns out we don't.. but it still looks a bit cleaner this way. --- app/views/admin/products_v3/_edit_image.html.haml | 5 ++--- app/webpacker/controllers/form_controller.js | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 app/webpacker/controllers/form_controller.js diff --git a/app/views/admin/products_v3/_edit_image.html.haml b/app/views/admin/products_v3/_edit_image.html.haml index 2f64a278ec..6edaf8d5cb 100644 --- a/app/views/admin/products_v3/_edit_image.html.haml +++ b/app/views/admin/products_v3/_edit_image.html.haml @@ -5,12 +5,11 @@ -# Submit to controller, because StimulusReflex doesn't support file uploads = form_for [:admin, product, image], url: admin_product_image_path(product, image), - html: { multipart: true } do |f| + html: { multipart: true }, data: { controller: "form" } do |f| %input{ type: :hidden, name: :return_url, value: return_url} = f.hidden_field :viewable_id, value: product.id %p -# label.button provides a handy shortcut to open the file selector on click. Unfortunately this trick isn't keyboard accessible though.. = f.label :attachment, t(".upload"), class: "button primary icon-upload-alt" - = f.file_field :attachment, style: "display: none" - = f.submit # todo: submit automatically + = f.file_field :attachment, style: "display: none", "data-action": "change->form#submit" diff --git a/app/webpacker/controllers/form_controller.js b/app/webpacker/controllers/form_controller.js new file mode 100644 index 0000000000..aaac9db4ec --- /dev/null +++ b/app/webpacker/controllers/form_controller.js @@ -0,0 +1,7 @@ +import { Controller } from "stimulus"; + +export default class FormController extends Controller { + submit() { + this.element.submit(); + } +}