Skip to content

Commit 3b0a417

Browse files
committed
feat: Improve support for input elements outside form.
1 parent 2a53aeb commit 3b0a417

File tree

6 files changed

+9
-11
lines changed

6 files changed

+9
-11
lines changed

src/core/utils.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ function parseLength(length, reference_length = null) {
494494

495495
// Return a jQuery object with elements related to an input element.
496496
function findRelatives(el) {
497+
el = jqToNode(el);
497498
const $el = $(el);
498499
let $relatives = $(el);
499500
let $label = $();
@@ -505,10 +506,7 @@ function findRelatives(el) {
505506
$label = $(`label[for="${el.id}"]`);
506507
}
507508
if (!$label.length) {
508-
let $form = $el.closest("form");
509-
if (!$form.length) {
510-
$form = $(document.body);
511-
}
509+
const $form = $(el.form || el.closest("form") || document.body);
512510
$label = $form.find(`label[for="${el.name}"]`);
513511
}
514512
$relatives = $relatives.add($label);

src/lib/dependshandler.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import $ from "jquery";
22
import parser from "./depends_parse";
3+
import utils from "../core/utils";
34

45
function DependsHandler($el, expression) {
5-
var $context = $el.closest("form");
6-
if (!$context.length) $context = $(document);
6+
const el = utils.jqToNode($el);
77
this.$el = $el;
8-
this.$context = $context;
8+
this.$context = $(el.form || el.closest("form") || document);
99
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
1010
}
1111

src/pat/auto-suggest/auto-suggest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default Base.extend({
132132

133133
// Clear values on reset.
134134
events.add_event_listener(
135-
this.el.closest("form"),
135+
this.el.form || this.el.closest("form"), // TODO: `closest` necessary?
136136
"reset",
137137
"pat-auto-suggest--reset",
138138
() => this.$el.select2("val", "")

src/pat/close-panel/close-panel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default Base.extend({
2727
if (
2828
e.target.matches(":not([formnovalidate])") &&
2929
e.target.matches("[type=submit], button:not([type=button])") &&
30-
this.el.closest("form")?.checkValidity() === false
30+
this.el.form?.checkValidity() === false
3131
) {
3232
// Prevent closing an invalid form when submitting.
3333
return;

src/pat/image-crop/image-crop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var _ = {
5252
// Set the form ID
5353
if (opts.formId.length === 0) {
5454
// no form ID supplied. Look for the closest parent form element
55-
data.form = $this.closest("form");
55+
data.form = $(this.form || this.closest("form"));
5656
if (data.form.length === 0) {
5757
log.error("No form specified or found");
5858
return;

src/pat/sortable/sortable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default Base.extend({
2020
if (window.__patternslib_import_styles) {
2121
import("./_sortable.scss");
2222
}
23-
this.$form = this.$el.closest("form");
23+
this.$form = $(this.el.form || this.el.closest("form"));
2424
this.options = parser.parse(this.$el, false);
2525
this.recordPositions().initScrolling();
2626
this.$el.on("pat-update", this.onPatternUpdate.bind(this));

0 commit comments

Comments
 (0)