Merge pull request #16909 from rmehta/select-fix

fix(minor): refactor add_options
This commit is contained in:
Rushabh Mehta 2022-05-16 15:56:02 +05:30 committed by GitHub
commit 5958b41c91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 31 deletions

View file

@ -73,7 +73,8 @@ frappe.ui.form.ControlSelect = class ControlSelect extends frappe.ui.form.Contro
if(this.$input) {
var selected = this.$input.find(":selected").val();
this.$input.empty().add_options(options || []);
this.$input.empty();
frappe.ui.form.add_options(this.$input, options || []);
if(value===undefined && selected) {
this.$input.val(selected);
@ -101,39 +102,47 @@ frappe.ui.form.ControlSelect = class ControlSelect extends frappe.ui.form.Contro
}
};
frappe.ui.form.add_options = function(input, options_list) {
let $select = $(input);
if (!Array.isArray(options_list)) {
return $select;
}
// create options
for(var i=0, j=options_list.length; i<j; i++) {
var v = options_list[i];
var value = null;
var label = null;
if (!is_null(v)) {
var is_value_null = is_null(v.value);
var is_label_null = is_null(v.label);
var is_disabled = Boolean(v.disabled);
var is_selected = Boolean(v.selected);
if (is_value_null && is_label_null) {
value = v;
label = __(v);
} else {
value = is_value_null ? "" : v.value;
label = is_label_null ? __(value) : __(v.label);
}
}
$('<option>').html(cstr(label))
.attr('value', value)
.prop('disabled', is_disabled)
.prop('selected', is_selected)
.appendTo($select.get(0));
}
// select the first option
$select.get(0).selectedIndex = 0;
$select.trigger('select-change');
return $select;
};
// add <option> list to <select>
(function($) {
$.fn.add_options = function(options_list) {
// create options
for(var i=0, j=options_list.length; i<j; i++) {
var v = options_list[i];
var value = null;
var label = null;
if (!is_null(v)) {
var is_value_null = is_null(v.value);
var is_label_null = is_null(v.label);
var is_disabled = Boolean(v.disabled);
var is_selected = Boolean(v.selected);
if (is_value_null && is_label_null) {
value = v;
label = __(v);
} else {
value = is_value_null ? "" : v.value;
label = is_label_null ? __(value) : __(v.label);
}
}
$('<option>').html(cstr(label))
.attr('value', value)
.prop('disabled', is_disabled)
.prop('selected', is_selected)
.appendTo(this);
}
// select the first option
this.selectedIndex = 0;
$(this).trigger('select-change');
return $(this);
return frappe.ui.add_options(this.get(0), options_list);
};
$.fn.set_working = function() {
this.prop('disabled', true);

View file

@ -191,6 +191,7 @@ a.card {
h4.modal-title {
font-size: 1em;
margin: 0px !important;
}
h5.modal-title {