mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-26 20:56:48 +00:00
A StimulusReflex form handler was being ignored, resulting in an error. Note that this method can support angular forms, the submit buttons just need to be updated to type='submit' (why they are not already boggles me).
22 lines
941 B
JavaScript
22 lines
941 B
JavaScript
import hotkeys from "hotkeys-js";
|
|
|
|
// Enable hotkeys on form elements
|
|
hotkeys.filter = function (event) {
|
|
var tagName = (event.target || event.srcElement).tagName;
|
|
hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT|BUTTON)$/.test(tagName) ? "input" : "other");
|
|
return true;
|
|
};
|
|
|
|
// Submit form
|
|
// Although 'enter' will submit the form in many cases, it doesn't cover elements such
|
|
// as select and textarea. This shortcut is a standard used across many major websites.
|
|
hotkeys("ctrl+enter, command+enter", function (event, handler) {
|
|
const form = event.target.form;
|
|
|
|
// Simulate a click on the first available submit button. This seems to be the most robust option,
|
|
// ensuring that event handlers are handled first (eg for StimulusReflex). If there's no submit
|
|
// button, nothing happens (eg for Angular forms).
|
|
const submit = form && form.querySelector('input[type="submit"], button[type="submit"]');
|
|
submit && submit.click();
|
|
});
|