Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

added multi_select_controller and added multi-select support for tags without Ctrl/Shift in transaction forms (#55)

Bishal Shresthaefd96f606cd0
diff --git a/app/javascript/controllers/multi_select_controller.js b/app/javascript/controllers/multi_select_controller.js
new file mode 100644
index 0000000000..6bd050710e
--- /dev/null
+++ b/app/javascript/controllers/multi_select_controller.js
@@ -0,0 +1,21 @@
+import { Controller } from "@hotwired/stimulus";
+
+export default class extends Controller {
+ connect() {
+ this.element.addEventListener('mousedown', this.toggleOption);
+ }
+
+ disconnect() {
+ this.element.removeEventListener('mousedown', this.toggleOption);
+ }
+
+ toggleOption = (e) => {
+ const option = e.target;
+ if (option.tagName === 'OPTION') {
+ e.preventDefault();
+ option.selected = !option.selected;
+ const event = new Event('change', { bubbles: true });
+ this.element.dispatchEvent(event);
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erb
index 3760f7dd0a..48f27d1af0 100644
--- a/app/views/transactions/_form.html.erb
+++ b/app/views/transactions/_form.html.erb
@@ -38,7 +38,8 @@
multiple: true,
label: t(".tags_label"),
container_class: "h-40"
- } %>
+ },
+ { "data-controller": "multi-select" } %>
<% end %>
<%= f.text_area :notes,
label: t(".note_label"),
diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erb
index 4111cd437c..9554fabe65 100644
--- a/app/views/transactions/show.html.erb
+++ b/app/views/transactions/show.html.erb
@@ -84,7 +84,8 @@
label: t(".tags_label"),
container_class: "h-40"
},
- { "data-auto-submit-form-target": "auto" } %>
+ { "data-controller": "multi-select", "data-auto-submit-form-target": "auto" }
+ %>
<% end %>
<% end %>