Merge pull request #33637 from sokumon/dialog-focus

fix: bring focus back on control when dialog is opened
This commit is contained in:
Soham Kulkarni 2025-08-18 14:41:18 +05:30 committed by GitHub
commit 5c659e8029
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,7 @@ frappe.ui.Dialog = class Dialog extends frappe.ui.FieldGroup {
super();
this.display = false;
this.is_dialog = true;
this.last_focus = null;
$.extend(this, { animate: true, size: null, auto_make: true }, opts);
if (this.auto_make) {
@ -247,6 +248,7 @@ frappe.ui.Dialog = class Dialog extends frappe.ui.FieldGroup {
show() {
// show it
this.handle_focus();
if (this.animate) {
this.$wrapper.addClass("fade");
} else {
@ -274,6 +276,21 @@ frappe.ui.Dialog = class Dialog extends frappe.ui.FieldGroup {
this.is_visible = false;
}
handle_focus() {
const me = this;
if (frappe.get_route) {
if (frappe.get_route()[0] == "Form") {
if (!me.last_focus) me.last_focus = document.activeElement;
}
$(document).on("escape", function () {
if (me.last_focus) {
me.last_focus.focus();
me.last_focus = null;
}
});
}
}
get_close_btn() {
return this.$wrapper.find(".btn-modal-close");
}