Skip to content

Commit c6f0206

Browse files
committed
Completer.js: Trigger autosubmit on focus out for manual input if the not instrumented
Ensure the form submits automatically when: - The completer is not part of term input or instrumented - The completer has the autosubmit dataset attribute - No suggestion is explicitly selected on manual input
1 parent 01b4082 commit c6f0206

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

asset/js/widget/Completer.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ define(["../notjQuery"], function ($) {
66
constructor(input, instrumented = false) {
77
this.input = input;
88
this.instrumented = instrumented;
9+
this.hasNotBeenCompleted = false; // Flag to identify if the input has been completed at least once.
910
this.selectionStartInput = null;
1011
this.selectionActive = false;
1112
this.mouseSelectionActive = false;
@@ -268,6 +269,7 @@ define(["../notjQuery"], function ($) {
268269

269270
complete(input, value, data) {
270271
$(input).focus({ scripted: true });
272+
this.hasNotBeenCompleted = false;
271273

272274
if (this.instrumented) {
273275
if (! Object.keys(data).length) {
@@ -476,7 +478,7 @@ define(["../notjQuery"], function ($) {
476478
}
477479

478480
onFocusOut(event) {
479-
if (this.completedInput === null) {
481+
if (this.completedInput === null && (this.instrumented || ! this.hasNotBeenCompleted)) {
480482
// If there are multiple instances of Completer bound to the same suggestion container
481483
// all of them try to handle the event. Though, only one of them is responsible and
482484
// that's the one which has a completed input set.
@@ -492,12 +494,20 @@ define(["../notjQuery"], function ($) {
492494
&& ! this.termSuggestions.contains(document.activeElement)
493495
) {
494496
// Hide the suggestions if the user doesn't navigate them
495-
if (input !== completedInput) {
497+
if (completedInput !== null && input !== completedInput) {
496498
// Restore input if a suggestion lost focus
497499
this.suggest(completedInput, this.completedValue);
498500
}
499501

500502
this.hideSuggestions();
503+
504+
// Autosubmit when the user leaves without selecting a suggestion on manual input.
505+
// Only for non-instrumented mode — instrumented inputs (e.g. TermInput) handle
506+
// autosubmit themselves via BaseInput.autoSubmit() with proper term data.
507+
if (! this.instrumented && this.shouldAutoSubmit()) {
508+
this.hasNotBeenCompleted = false;
509+
$(input.form).trigger('submit', { submittedBy: input });
510+
}
501511
}
502512
}, 250);
503513
}
@@ -712,6 +722,7 @@ define(["../notjQuery"], function ($) {
712722

713723
onInput(event) {
714724
let input = event.target;
725+
this.hasNotBeenCompleted = true;
715726

716727
if (input.minLength > 0 && input.value.length < input.minLength) {
717728
return;

0 commit comments

Comments
 (0)