diff --git a/addons/web/static/src/views/fields/relational_utils.js b/addons/web/static/src/views/fields/relational_utils.js index 30b25d893b2..7cd890f2573 100644 --- a/addons/web/static/src/views/fields/relational_utils.js +++ b/addons/web/static/src/views/fields/relational_utils.js @@ -146,7 +146,15 @@ export class Many2XAutocomplete extends Component { fieldString, onClose: () => { const autoCompleteInput = autoCompleteContainer.el.querySelector("input"); - autoCompleteInput.value = ""; + + // There are two cases: + // 1. Value is the same as the input: it means the autocomplete has re-rendered with the right value + // This is in case we saved the record, triggering all the interface to update. + // 2. Value is different from the input: it means the input has a manually entered value and nothing + // happened, that is, we discarded the changes + if (this.props.value !== autoCompleteInput.value) { + autoCompleteInput.value = ""; + } autoCompleteInput.focus(); }, }); diff --git a/addons/web/static/tests/views/fields/many2one_field_tests.js b/addons/web/static/tests/views/fields/many2one_field_tests.js index 2bf3046c82d..fbb9a585ccb 100644 --- a/addons/web/static/tests/views/fields/many2one_field_tests.js +++ b/addons/web/static/tests/views/fields/many2one_field_tests.js @@ -669,6 +669,54 @@ QUnit.module("Fields", (hooks) => { assert.strictEqual($("tr.o_data_row").length, 0, "should display 0 records"); }); + QUnit.test( + "many2ones in list views: create in dialog keeps the input", + async function (assert) { + serverData.views = { + "partner,false,form": ` +
+ + `, + }; + + await makeView({ + type: "list", + resModel: "partner", + serverData, + arch: ` + + + `, + mockRPC(route, args) { + if (args.method === "create" || args.method === "write") { + assert.step(`${args.method}: ${JSON.stringify(args.args)}`); + } + }, + }); + + await click(target.querySelectorAll(".o_data_cell")[0]); + const input = target.querySelector(".o_field_widget[name=trululu] input"); + input.value = "yy"; + await triggerEvent(input, null, "input"); + await click(target, ".o_field_widget[name=trululu] input"); + await selectDropdownItem(target, "trululu", "Create and edit..."); + + await clickSave(target.querySelector(".modal")); + assert.verifySteps([`create: [{"name":"yy"}]`]); + assert.strictEqual( + target.querySelector(".o_field_widget[name=trululu] input").value, + "yy" + ); + + await click(target); + assert.verifySteps([`write: [[1],{"trululu":5}]`]); + assert.strictEqual( + target.querySelector(".o_data_cell[name=trululu]").textContent, + "yy" + ); + } + ); + QUnit.test( "onchanges on many2ones trigger when editing record in form view", async function (assert) {