Skip to content

Commit

Permalink
fix(lib input-change-events): Support form elements outsde a form con…
Browse files Browse the repository at this point in the history
…tainer.

Also support form elements which are outside of a form container.
This allows pat-autosubmit to also work with form inputs which are bound
to a form but not contained wiithin.
  • Loading branch information
thet committed Jan 13, 2025
1 parent 92dc7f5 commit 3a37fa2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lib/input-change-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ const _ = {
// We've been given an element that is not a form input. We
// therefore assume that it's a container of form inputs and
// register handlers for its children.
$el.findInclusive(":input").each(_.registerHandlersForElement);
for (const _el of $el[0].closest("form").elements) {
// Search for all form elements, also those outside the form
// container.
_.registerHandlersForElement.bind(_el)();
}
}
},

registerHandlersForElement() {
let el_within_form = true;
const $form = $(this.form);
if (this.closest("form") !== this.form) {
el_within_form = false;
}

const $el = $(this);
const isNumber = $el.is("input[type=number]");
const isText = $el.is("input:text, input[type=search], textarea");
Expand All @@ -53,24 +63,24 @@ const _ = {
if ("onkeyup" in window) {
$el.on("keyup." + namespace, function () {
log.debug("translating keyup");
$el.trigger("input-change");
(el_within_form ? $el : $form).trigger("input-change");
});
}
}
if (isText || isNumber) {
$el.on("input." + namespace, function () {
log.debug("translating input");
$el.trigger("input-change");
(el_within_form ? $el : $form).trigger("input-change");
});
} else {
$el.on("change." + namespace, function () {
log.debug("translating change");
$el.trigger("input-change");
(el_within_form ? $el : $form).trigger("input-change");
});
}

$el.on("blur", function () {
$el.trigger("input-defocus");
(el_within_form ? $el : $form).trigger("input-defocus");
});
},

Expand Down

0 comments on commit 3a37fa2

Please sign in to comment.