Skip to content

Commit

Permalink
feat: Improve support for input elements outside form.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Nov 6, 2023
1 parent 2a53aeb commit 3b0a417
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ function parseLength(length, reference_length = null) {

// Return a jQuery object with elements related to an input element.
function findRelatives(el) {
el = jqToNode(el);
const $el = $(el);
let $relatives = $(el);
let $label = $();
Expand All @@ -505,10 +506,7 @@ function findRelatives(el) {
$label = $(`label[for="${el.id}"]`);
}
if (!$label.length) {
let $form = $el.closest("form");
if (!$form.length) {
$form = $(document.body);
}
const $form = $(el.form || el.closest("form") || document.body);
$label = $form.find(`label[for="${el.name}"]`);
}
$relatives = $relatives.add($label);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dependshandler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import $ from "jquery";
import parser from "./depends_parse";
import utils from "../core/utils";

function DependsHandler($el, expression) {
var $context = $el.closest("form");
if (!$context.length) $context = $(document);
const el = utils.jqToNode($el);
this.$el = $el;
this.$context = $context;
this.$context = $(el.form || el.closest("form") || document);
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
}

Expand Down
2 changes: 1 addition & 1 deletion src/pat/auto-suggest/auto-suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default Base.extend({

// Clear values on reset.
events.add_event_listener(
this.el.closest("form"),
this.el.form || this.el.closest("form"), // TODO: `closest` necessary?
"reset",
"pat-auto-suggest--reset",
() => this.$el.select2("val", "")
Expand Down
2 changes: 1 addition & 1 deletion src/pat/close-panel/close-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default Base.extend({
if (
e.target.matches(":not([formnovalidate])") &&
e.target.matches("[type=submit], button:not([type=button])") &&
this.el.closest("form")?.checkValidity() === false
this.el.form?.checkValidity() === false
) {
// Prevent closing an invalid form when submitting.
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pat/image-crop/image-crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = {
// Set the form ID
if (opts.formId.length === 0) {
// no form ID supplied. Look for the closest parent form element
data.form = $this.closest("form");
data.form = $(this.form || this.closest("form"));
if (data.form.length === 0) {
log.error("No form specified or found");
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pat/sortable/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default Base.extend({
if (window.__patternslib_import_styles) {
import("./_sortable.scss");
}
this.$form = this.$el.closest("form");
this.$form = $(this.el.form || this.el.closest("form"));
this.options = parser.parse(this.$el, false);
this.recordPositions().initScrolling();
this.$el.on("pat-update", this.onPatternUpdate.bind(this));
Expand Down

0 comments on commit 3b0a417

Please sign in to comment.