fix: autocomplete input value if label & value varies

This commit is contained in:
Saqib Ansari 2022-02-12 18:22:48 +05:30
parent 8f277a247e
commit 436543d212
2 changed files with 30 additions and 3 deletions

View file

@ -4,7 +4,7 @@ context('Control Autocomplete', () => {
cy.visit('/app/website');
});
function get_dialog_with_autocomplete() {
function get_dialog_with_autocomplete(options) {
cy.visit('/app/website');
return cy.dialog({
title: 'Autocomplete',
@ -13,7 +13,7 @@ context('Control Autocomplete', () => {
'label': 'Select an option',
'fieldname': 'autocomplete',
'fieldtype': 'Autocomplete',
'options': ['Option 1', 'Option 2', 'Option 3'],
'options': options || ['Option 1', 'Option 2', 'Option 3'],
}
]
});
@ -35,4 +35,23 @@ context('Control Autocomplete', () => {
});
});
it('should set the valid value with different label', () => {
const options_with_label = [
{ label: "Option 1", value: "option_1" },
{ label: "Option 2", value: "option_2" }
];
get_dialog_with_autocomplete(options_with_label).as('dialog');
cy.get('.frappe-control[data-fieldname=autocomplete] input').focus().as('input');
cy.get('.frappe-control[data-fieldname=autocomplete]').findByRole('listbox').should('be.visible');
cy.get('@input').type('2', { delay: 300 });
cy.get('.frappe-control[data-fieldname=autocomplete] input').type('{enter}', { delay: 300 });
cy.get('.frappe-control[data-fieldname=autocomplete] input').blur();
cy.get('@dialog').then(dialog => {
let value = dialog.get_value('autocomplete');
expect(value).to.eq('option_2');
dialog.clear();
});
});
});

View file

@ -26,6 +26,14 @@ frappe.ui.form.ControlAutocomplete = class ControlAutoComplete extends frappe.ui
}
}
get_input_value() {
if (this.$input) {
const label = this.$input.val();
const item = this._data?.find(i => i.label == label);
return item ? item.value : label;
}
}
get_awesomplete_settings() {
var me = this;
return {
@ -34,7 +42,7 @@ frappe.ui.form.ControlAutocomplete = class ControlAutoComplete extends frappe.ui
autoFirst: true,
list: this.get_data(),
data: function(item) {
if (!(item instanceof Object)) {
if (typeof item !== 'object') {
var d = { value: item };
item = d;
}