Skip to content

Commit

Permalink
Merge pull request #174 from isprojects/bugfix/NEXTPY-645-decimal-str…
Browse files Browse the repository at this point in the history
…ing-under-1-fails-when-comma-is-used-as-seperator

NEXTPY-645, Fixed bug where decimals where falsely marked as empty
  • Loading branch information
arrahimi-ispnext committed Oct 19, 2023
2 parents cc645d8 + 73be022 commit 3b7873e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.34.3

- Fixed bug where decimals where falsely marked as empty for localizations that use comma as decimal separator

# 1.34.2

- Fixed a bug with the `modelReferenceArray` converter - it was missing its isEmpty hook,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mstform",
"version": "1.34.2",
"version": "1.34.3",
"description": "mobx-state-tree powered forms",
"main": "dist/mstform.js",
"typings": "dist/src/index.d.ts",
Expand Down
20 changes: 18 additions & 2 deletions src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,32 @@ function decimalRender(

function stringDecimal(options: DecimalOptions) {
const emptyRaw = "";
function stringDecimalIsEmpty(raw: string, options: DecimalOptions): boolean {
function stringDecimalIsEmpty(
raw: string,
options: DecimalOptions,
extraOptions: StateConverterOptionsWithContext
): boolean {
if (raw === emptyRaw) {
return true;
}
if (raw) {
if (extraOptions.thousandSeparator) {
raw.replaceAll(extraOptions.thousandSeparator, "");
}
if (
extraOptions.decimalSeparator &&
extraOptions.decimalSeparator !== "."
) {
raw = raw.replaceAll(extraOptions.decimalSeparator, ".");
}
}
return options.zeroIsEmpty ? parseFloat(raw) === 0 : false;
}

return new StringConverter<string>({
emptyRaw,
isEmpty: (raw: string) => stringDecimalIsEmpty(raw, options),
isEmpty: (raw: string, extraOptions: StateConverterOptionsWithContext) =>
stringDecimalIsEmpty(raw, options, extraOptions),
emptyImpossible: (stateConverterOptions) => !options.zeroIsEmpty,
emptyValue: (stateConverterOptions) =>
options.zeroIsEmpty ? "0" : undefined,
Expand Down
29 changes: 29 additions & 0 deletions test/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,35 @@ test("zero decimal empty and required", () => {
expect(field.isEmptyAndRequired).toBeFalsy();
});

test("decimals should not be marked as empty", () => {
const M = types.model("M", {
foo: types.string,
});

const form = new Form(M, {
foo: new Field(
converters.stringDecimal({ decimalPlaces: 2, zeroIsEmpty: true }),
{ required: true }
),
});

const o = M.create({ foo: "0" });

const state = form.state(o, {
converterOptions: {
decimalSeparator: ",",
thousandSeparator: ".",
},
});
const field = state.field("foo");

field.setRaw("0,02");
expect(field.isEmptyAndRequired).toBeFalsy();

field.setRaw("0.02");
expect(field.isEmptyAndRequired).toBeFalsy();
});

test("zero decimal maybeNull empty and required", () => {
const M = types.model("M", {
foo: types.maybeNull(types.string),
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"moduleResolution": "node",
"target": "es5",
"module": "es2015",
"lib": ["es2015", "es2016", "es2017", "es2020", "dom"],
"lib": ["es2015", "es2016", "es2017", "es2020", "es2021", "dom"],
"sourceMap": true,
"strict": true,
"strictPropertyInitialization": true,
Expand Down

0 comments on commit 3b7873e

Please sign in to comment.