fix(ux): set default country flag for phone component (#37030)

* fix(ux): set default country for phone input

* refactor: call `set_default_country` instead of repeat the same logic
This commit is contained in:
Kerolles Fathy 2026-02-16 13:32:41 +02:00 committed by GitHub
parent 7c756aa811
commit f8ea27793a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ frappe.ui.form.ControlPhone = class ControlPhone extends frappe.ui.form.ControlD
super.make_input();
this.setup_country_code_picker();
this.input_events();
this.set_default_country();
}
async setup_country_codes() {
@ -21,6 +22,15 @@ frappe.ui.form.ControlPhone = class ControlPhone extends frappe.ui.form.ControlD
}
}
set_default_country() {
if (!this.get_value()) {
const default_country = frappe.sys_defaults?.country || "India";
if (this.country_codes && this.country_codes[default_country]) {
this.country_code_picker.on_change(default_country, false);
}
}
}
input_events() {
this.$input.keydown((e) => {
const key_code = e.keyCode;
@ -32,7 +42,7 @@ frappe.ui.form.ControlPhone = class ControlPhone extends frappe.ui.form.ControlD
});
// Replaces code when selected and removes previously selected.
this.country_code_picker.on_change = (country) => {
this.country_code_picker.on_change = (country, focus = true) => {
if (!country) {
return this.reset_input();
}
@ -59,7 +69,9 @@ frappe.ui.form.ControlPhone = class ControlPhone extends frappe.ui.form.ControlD
this.update_padding();
// hide popover and focus input
this.$wrapper.popover("hide");
this.$input.focus();
if (focus) {
this.$input.focus();
}
};
this.$wrapper.find(".selected-phone").on("click", (e) => {
@ -133,6 +145,7 @@ frappe.ui.form.ControlPhone = class ControlPhone extends frappe.ui.form.ControlD
if (!this.get_value()) {
this.reset_input();
this.set_default_country();
}
}