From 91189fbb776c9e74263ef381a946108124c19163 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 20 Apr 2023 22:40:08 +1000 Subject: [PATCH] Fix for "focus" field in forms (#4644) - Reimplements ability to auto-focus fields when launching modal forms - Can specify with the "focus" option - Otherwise, will focus on the first available field --- InvenTree/templates/js/translated/forms.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index b470cb2758..cda991d1f6 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -692,6 +692,21 @@ function constructFormBody(fields, options) { // Scroll to the top $(options.modal).find('.modal-form-content-wrapper').scrollTop(0); + + // Focus on a particular field + let focus_field = options.focus; + + if (focus_field == null && field_names.length > 0) { + // If no focus field is specified, focus on the first field + focus_field = field_names[0]; + } + + let el = $(options.modal + ` #id_${focus_field}`); + + // Add a callback to focus on the first field + $(options.modal).on('shown.bs.modal', function() { + el.focus(); + }); }