Merge pull request #23143 from blaggacao/fix/float-input-formatting

This commit is contained in:
Suraj Shetty 2023-11-15 12:33:46 +05:30 committed by GitHub
commit 8f2db21f64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 37 deletions

View file

@ -32,10 +32,13 @@ context("Control Float", () => {
cy.wait(200);
cy.fill_field("float_number", d.input, "Float").blur();
cy.get_field("float_number", "Float").should("have.value", d.blur_expected);
cy.wait(100);
cy.get_field("float_number", "Float").focus();
cy.wait(100);
cy.get_field("float_number", "Float").blur();
cy.wait(100);
cy.get_field("float_number", "Float").focus();
cy.wait(100);
cy.get_field("float_number", "Float").should("have.value", d.focus_expected);
});
});
@ -49,17 +52,17 @@ context("Control Float", () => {
{
input: "364.87,334",
blur_expected: "36.487,334",
focus_expected: "36487.334",
focus_expected: "36.487,334",
},
{
input: "36487,334",
blur_expected: "36.487,334",
focus_expected: "36487.334",
input: "36487,335",
blur_expected: "36.487,335",
focus_expected: "36.487,335",
},
{
input: "100",
blur_expected: "100,000",
focus_expected: "100",
input: "2*(2+47)+1,5+1",
blur_expected: "100,500",
focus_expected: "100,500",
},
],
},
@ -67,19 +70,19 @@ context("Control Float", () => {
number_format: "#,###.##",
values: [
{
input: "364,87.334",
blur_expected: "36,487.334",
focus_expected: "36487.334",
input: "464,87.334",
blur_expected: "46,487.334",
focus_expected: "46,487.334",
},
{
input: "36487.334",
blur_expected: "36,487.334",
focus_expected: "36487.334",
input: "46487.335",
blur_expected: "46,487.335",
focus_expected: "46,487.335",
},
{
input: "100",
blur_expected: "100.000",
focus_expected: "100",
input: "3*(2+47)+1.5+1",
blur_expected: "149.500",
focus_expected: "149.500",
},
],
},
@ -90,13 +93,13 @@ context("Control Float", () => {
{
input: "12.345",
blur_expected: "12.345,000",
focus_expected: "12345",
focus_expected: "12.345,000",
},
{
// parseFloat would reduce 12,340 to 12,34 if this string was ever to be parsed
input: "12.340",
blur_expected: "12.340,000",
focus_expected: "12340",
focus_expected: "12.340,000",
},
],
},

View file

@ -1,17 +1,4 @@
frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlInt {
make_input() {
super.make_input();
const change_handler = (e) => {
if (this.change) this.change(e);
else {
let value = this.get_input_value();
this.parse_validate_and_set_in_model(value, e);
this.refresh();
}
};
// convert to number format on focusout since focus converts it to flt.
this.$input.on("focusout", change_handler);
}
parse(value) {
value = this.eval_expression(value);
return isNaN(parseFloat(value)) ? null : flt(value, this.get_precision());

View file

@ -13,7 +13,6 @@ frappe.ui.form.ControlInt = class ControlInt extends frappe.ui.form.ControlData
.on("focus", function () {
setTimeout(function () {
if (!document.activeElement) return;
document.activeElement.value = me.validate(document.activeElement.value);
document.activeElement.select();
}, 100);
return false;
@ -24,10 +23,19 @@ frappe.ui.form.ControlInt = class ControlInt extends frappe.ui.form.ControlData
}
eval_expression(value) {
if (typeof value === "string") {
if (value.match(/^[0-9+\-/* ]+$/)) {
const parsed_components = value.match(/[^\d.,]+|[\d.,]+/g);
var parsed_value = value;
if (parsed_components !== null) {
parsed_value = parsed_components
.map((v) => {
return isNaN(parseFloat(v)) ? v : flt(v);
})
.join("");
}
if (parsed_value.match(/^[0-9+\-/*.() ]+$/)) {
// If it is a string containing operators
try {
return eval(value);
return eval(parsed_value);
} catch (e) {
// bad expression
return value;

View file

@ -1,6 +1,6 @@
<!DOCTYPE html>
<!-- Built on Frappe. https://frappeframework.com/ -->
<html lang="en">
<html lang="{{boot.lang}}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

View file

@ -166,7 +166,7 @@ def get_home_page_via_hooks():
def get_boot_data():
return {
"lang": "en",
"lang": frappe.local.lang or "en",
"sysdefaults": {
"float_precision": cint(frappe.get_system_settings("float_precision")) or 3,
"date_format": frappe.get_system_settings("date_format") or "yyyy-mm-dd",