Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/client/src/widgets/CurrencyInputWidget/widget/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ describe("defaultValueValidation", () => {
});
});

it("should preserve decimal precision for valid string defaults", () => {
result = defaultValueValidation(
"123.00",
{ decimals: 2 } as CurrencyInputWidgetProps,
_,
);

expect(result).toEqual({
isValid: true,
parsed: "123.00",
messages: [{ name: "", message: "" }],
});

result = defaultValueValidation(
"4.10",
{ decimals: 2 } as CurrencyInputWidgetProps,
_,
);

expect(result).toEqual({
isValid: true,
parsed: "4.10",
messages: [{ name: "", message: "" }],
});
});

it("should validate defaulttext with object value", () => {
const value = {};

Expand Down
18 changes: 17 additions & 1 deletion app/client/src/widgets/CurrencyInputWidget/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ export function defaultValueValidation(
const decimalSeperator = getLocaleDecimalSeperator();
const defaultDecimalSeperator = ".";

const getParsedString = (parsedValue: number) => {
if (!_.isString(value)) {
return String(parsedValue);
}

const trimmedValue = value.trim();

if (!trimmedValue.includes(defaultDecimalSeperator)) {
return String(parsedValue);
}

const [, fractionalPart = ""] = trimmedValue.split(defaultDecimalSeperator);

return parsedValue.toFixed(fractionalPart.length);
};

if (_.isObject(value)) {
return {
isValid: false,
Expand Down Expand Up @@ -144,7 +160,7 @@ export function defaultValueValidation(
messages = [EMPTY_ERROR_MESSAGE];
}

parsed = String(parsed);
parsed = getParsedString(parsed);
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export function defaultValueValidation(
const decimalSeperator = getLocaleDecimalSeperator();
const defaultDecimalSeperator = ".";

const getParsedString = (parsedValue: number) => {
if (!_.isString(value)) {
return String(parsedValue);
}

const trimmedValue = value.trim();

if (!trimmedValue.includes(defaultDecimalSeperator)) {
return String(parsedValue);
}

const [, fractionalPart = ""] = trimmedValue.split(defaultDecimalSeperator);

return parsedValue.toFixed(fractionalPart.length);
};

if (_.isObject(value)) {
return {
isValid: false,
Expand Down Expand Up @@ -100,7 +116,7 @@ export function defaultValueValidation(
messages = [EMPTY_ERROR_MESSAGE];
}

parsed = String(parsed);
parsed = getParsedString(parsed);
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ describe("defaultValueValidation", () => {
});
});

it("should preserve decimal precision for valid string defaults", () => {
result = defaultValueValidation(
"123.00",
{ decimals: 2 } as CurrencyInputWidgetProps,
_,
);

expect(result).toEqual({
isValid: true,
parsed: "123.00",
messages: [{ name: "", message: "" }],
});

result = defaultValueValidation(
"4.10",
{ decimals: 2 } as CurrencyInputWidgetProps,
_,
);

expect(result).toEqual({
isValid: true,
parsed: "4.10",
messages: [{ name: "", message: "" }],
});
});

it("should validate defaulttext with object value", () => {
const value = {};

Expand Down