[FIX] web: use default value when creating a new property

Bug
===
Currently, the default value of a property is only used when we create
new record. But it can also be used when we create a new property.

Note that the default value is written on the property ONLY if the
property just have been created, to not update existing value on
other records (the goal is to have a behavior similar to normal fields).

Task-3172106

X-original-commit: e3099ae584cef74434fdb51f7fee2ae6250f18ca
Part-of: odoo/odoo#113448
This commit is contained in:
std-odoo
2023-02-28 10:02:48 +01:00
parent 7c7c369237
commit bbb20584c2
2 changed files with 75 additions and 0 deletions
@@ -491,10 +491,29 @@ export class PropertiesField extends Component {
onClose: () => {
this.state.movedPropertyName = null;
target.classList.remove("disabled");
if (isNewlyCreated) {
this._setDefaultPropertyValue(currentName(propertyName));
}
},
}
);
}
/**
* Write the default value on the given property.
*
* @param {string} propertyName
*/
_setDefaultPropertyValue(propertyName) {
const propertiesValues = this.propertiesList;
const newProperty = propertiesValues.find((property) => property.name === propertyName);
newProperty.value = newProperty.default;
// it won't update the props, it's a trick because the onClose event of the popover
// is called not synchronously, and so if we click on "create a property", it will close
// the popover, calling this function, but the value will be overwritten because of onPropertyCreate
this.props.value = propertiesValues;
this.props.record.update({ [this.props.name]: propertiesValues });
}
}
export const propertiesField = {
@@ -1168,4 +1168,60 @@ QUnit.module("Fields", (hooks) => {
const items = target.querySelectorAll(".o_kanban_record:nth-child(1) .o_kanban_property_field");
assert.equal(items.length, 2);
});
/**
* Test the behavior of the default value. It should be propagated on the property
* value only when we create a new property. If the property already exists, and we
* change the default value, it should never update the property value.
*/
QUnit.test("properties: default value", async function (assert) {
async function mockRPC(route, { method, model, kwargs }) {
if (method === "check_access_rights") {
return true;
}
}
await makeView({
type: "form",
resModel: "partner",
resId: 1,
serverData,
arch: `
<form>
<sheet>
<group>
<field name="company_id"/>
<field name="properties"/>
</group>
</sheet>
</form>`,
mockRPC,
});
const field = target.querySelector(".o_field_properties");
assert.ok(field, "The field must be in the view");
// create a new property
// edit the default value and close the popover definition
// because we just created the property, the default value should be propagated
await click(target, ".o_field_property_add button");
await nextTick();
await editInput(target, ".o_field_property_definition_value input", "First Default Value");
await closePopover(target);
const newProperty = field.querySelector(".o_field_properties .o_property_field:nth-child(3)");
assert.strictEqual(newProperty.querySelector(".o_property_field_value input").value, "First Default Value");
// empty the new / existing property value, and re-open the property we created and change the default value
// it shouldn't be propagated because it's the second time we open the definition
const existingProperty = field.querySelector(".o_field_properties .o_property_field:nth-child(1)");
for (const property of [newProperty, existingProperty]) {
await editInput(property, ".o_property_field_value input", "");
await click(property, ".o_field_property_open_popover");
await nextTick();
await editInput(target, ".o_field_property_definition_value input", "Second Default Value");
await closePopover(target);
assert.strictEqual(property.querySelector(".o_property_field_value input").value, "");
}
});
});