From ba381f80ede7ffc0fd7f95a306d5a715f534d080 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Mon, 30 Jan 2023 11:36:08 +0000 Subject: [PATCH] [FIX] web: fix the formatting of the float / integer property Bug === The number property should be formatted like a normal integer / float field, in the form view (show 2 decimal for the float, thousands separators, etc, even when editing). Task-3162402 closes odoo/odoo#113449 X-original-commit: 99c251820dcf4791625fabd3fcd61db84280d408 Signed-off-by: Thibault Delavallee (tde) --- .../views/fields/properties/property_value.js | 13 +++- .../fields/properties/property_value.xml | 8 +- .../views/fields/properties_field_tests.js | 75 ++++++++++++++++++- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/views/fields/properties/property_value.js b/addons/web/static/src/views/fields/properties/property_value.js index 0faef505f46..30ef811c146 100644 --- a/addons/web/static/src/views/fields/properties/property_value.js +++ b/addons/web/static/src/views/fields/properties/property_value.js @@ -13,6 +13,7 @@ import { m2oTupleFromData } from "@web/views/fields/many2one/many2one_field"; import { PropertyTags } from "./property_tags"; import { AutoComplete } from "@web/core/autocomplete/autocomplete"; import { formatFloat, formatInteger, formatMany2one } from "@web/views/fields/formatters"; +import { parseFloat, parseInteger } from "@web/views/fields/parsers"; import { deserializeDate, deserializeDateTime, @@ -207,9 +208,17 @@ export class PropertyValue extends Component { } else if (this.props.type === "date") { newValue = newValue && serializeDate(newValue); } else if (this.props.type === "integer") { - newValue = parseInt(newValue) || 0; + try { + newValue = parseInteger(newValue) || 0; + } catch { + newValue = 0; + } } else if (this.props.type === "float") { - newValue = parseFloat(newValue) || 0; + try { + newValue = parseFloat(newValue) || 0; + } catch { + newValue = 0; + } } else if (["many2one", "many2many"].includes(this.props.type)) { // {id: 5, name: 'Demo'} => [5, 'Demo'] newValue = diff --git a/addons/web/static/src/views/fields/properties/property_value.xml b/addons/web/static/src/views/fields/properties/property_value.xml index cd96e7c6650..019b330466e 100644 --- a/addons/web/static/src/views/fields/properties/property_value.xml +++ b/addons/web/static/src/views/fields/properties/property_value.xml @@ -31,8 +31,8 @@ @@ -40,8 +40,8 @@ diff --git a/addons/web/static/tests/views/fields/properties_field_tests.js b/addons/web/static/tests/views/fields/properties_field_tests.js index 3b2102c298d..69951567b6d 100644 --- a/addons/web/static/tests/views/fields/properties_field_tests.js +++ b/addons/web/static/tests/views/fields/properties_field_tests.js @@ -21,7 +21,15 @@ async function closePopover(target) { } async function changeType(target, propertyType) { - const TYPES_INDEX = {"datetime": 6, "selection": 7, "tags": 8, "many2one": 9, "many2many": 10}; + const TYPES_INDEX = { + "integer": 3, + "float": 4, + "datetime": 6, + "selection": 7, + "tags": 8, + "many2one": 9, + "many2many": 10, + }; const propertyTypeIndex = TYPES_INDEX[propertyType]; await click(target, ".o_field_property_definition_type input"); await nextTick(); @@ -494,6 +502,71 @@ QUnit.module("Fields", (hooks) => { ); }); + /** + * Test the float and the integer property. + */ + QUnit.test("properties: float and integer", 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: ` +
+ + + + + + +
`, + mockRPC, + }); + + const field = target.querySelector(".o_field_properties"); + assert.ok(field, "The field must be in the view"); + + // change type to float + await click(target, ".o_property_field:nth-child(2) .o_field_property_open_popover"); + await changeType(target, "float"); + await closePopover(target); + + const editValue = async (newValue, expected) => { + await editInput( + target, + ".o_property_field:nth-child(2) .o_field_property_input", + newValue, + ); + // click away + await click(target, ".o_form_sheet_bg"); + const input = target.querySelector(".o_property_field:nth-child(2) .o_field_property_input"); + assert.strictEqual(input.value, expected); + } + + await editValue("2", "2.00"); + await editValue("2.11", "2.11"); + await editValue("2.1234567", "2.12", "Decimal precision is 2"); + await editValue("azerty", "", "Wrong float value should be interpreted as empty"); + await editValue("1,2,3,4,5,6.1,2,3,5", "123456.12"); + + // change type to integer + await click(target, ".o_property_field:nth-child(2) .o_field_property_open_popover"); + await changeType(target, "integer"); + await closePopover(target); + + await editValue("2", "2"); + await editValue("2.11", ""); + await editValue("azerty", "", "Wrong integer value should be interpreted as empty"); + await editValue("1,2,3,4,5,6", "123456"); + await editValue("1,2,3,4,5,6.1,2,3", ""); + }); + /** * Test the properties re-arrangement */