[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) <tde@openerp.com>
This commit is contained in:
std-odoo
2023-02-22 23:24:45 +01:00
parent 1593e982fd
commit ba381f80ed
3 changed files with 89 additions and 7 deletions
@@ -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 =
@@ -31,8 +31,8 @@
<input
t-att-id="props.id"
class="o_input o_field_property_input"
type="number"
t-att-value="propertyValue"
type="text"
t-att-value="displayValue"
t-on-change="(event) => this.onValueChange(event.target.value)"
/>
</t>
@@ -40,8 +40,8 @@
<input
t-att-id="props.id"
class="o_input o_field_property_input"
type="number"
t-att-value="propertyValue"
type="text"
t-att-value="displayValue"
t-on-change="(event) => this.onValueChange(event.target.value)"
/>
</t>
@@ -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: `
<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");
// 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
*/