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
This commit is contained in:
Oliver 2023-04-20 22:40:08 +10:00 committed by GitHub
parent 7bc4de6a92
commit 91189fbb77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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();
});
}