[FIX] web: X2many in list mode, press multi enter

Before this commit, in an X2many in editable list mode, creating two
valid records in succession by pressing Enter caused the first record
in the list to go into edit mode. The expected behavior is to add a new
empty line.

How to reproduce:
- Create a first valid record
- Press the enter key to validate it and a new line is created
- Complete this new line in a valid way
- Press the enter key to validate it

Result before the commit:
The first record switched to edit mode and there is no new empty line.

Result after this commit:
A new blank line is created in edit mode.
If you press enter again the empty line is validated and a new line is
created.

Part-of: odoo/odoo#96101
This commit is contained in:
FrancoisGe
2022-07-28 01:29:58 +02:00
parent afa12b9f04
commit 87b8feee03
2 changed files with 49 additions and 5 deletions
@@ -1208,11 +1208,14 @@ export class ListRenderer extends Component {
if (futureRecord) {
futureRecord.switchMode("edit");
} else if (this.lastIsDirty || !record.canBeAbandoned || this.lastCreatingAction) {
} else if (
this.lastIsDirty ||
!record.canBeAbandoned ||
(activeActions && (activeActions.canLink || activeActions.canCreate))
) {
this.props.onAdd({ group });
} else if (record.checkValidity()) {
const index = list.records.indexOf(record);
futureRecord = list.records[index + 1] || list.records.at(0);
} else {
futureRecord = list.records.at(0);
futureRecord.switchMode("edit");
}
break;
@@ -3746,6 +3746,46 @@ QUnit.module("Fields", (hooks) => {
assert.verifySteps(["get_views", "read", "onchange"]);
});
QUnit.test("pressing enter several times in a one2many", async function (assert) {
await makeView({
type: "form",
resModel: "partner",
serverData,
arch: `
<form>
<field name="turtles">
<tree editable="bottom">
<field name="turtle_foo"/>
</tree>
</field>
</form>`,
resId: 2,
});
await clickEdit(target);
await addRow(target);
assert.containsOnce(target, ".o_data_row");
assert.hasClass(target.querySelectorAll(".o_data_row")[0], "o_selected_row");
await editInput(target, "[name='turtle_foo'] input", "a");
triggerHotkey("Enter");
await nextTick();
assert.containsN(target, ".o_data_row", 2);
assert.hasClass(target.querySelectorAll(".o_data_row")[1], "o_selected_row");
await editInput(target, "[name='turtle_foo'] input", "a");
triggerHotkey("Enter");
await nextTick();
assert.containsN(target, ".o_data_row", 3);
assert.hasClass(target.querySelectorAll(".o_data_row")[2], "o_selected_row");
// this is a weird case, but there's no required fields, so the record is already valid, we can press Enter directly.
triggerHotkey("Enter");
await nextTick();
assert.containsN(target, ".o_data_row", 4);
assert.hasClass(target.querySelectorAll(".o_data_row")[3], "o_selected_row");
});
QUnit.test("editing a o2m, with required field and onchange", async function (assert) {
serverData.models.turtle.fields.turtle_foo.required = true;
delete serverData.models.turtle.fields.turtle_foo.default;
@@ -8944,7 +8984,8 @@ QUnit.module("Fields", (hooks) => {
assert.strictEqual(document.activeElement, input);
await triggerEvent(input, null, "keydown", { key: "Enter" });
triggerHotkey("Enter");
await nextTick();
assert.deepEqual(
[...target.querySelectorAll(".o_data_cell")].map((el) => el.textContent),
["", "pizza", ""]