fix: autocomplete input value if label & value varies
This commit is contained in:
parent
8f277a247e
commit
436543d212
2 changed files with 30 additions and 3 deletions
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue