Skip to content

Commit 8d6d966

Browse files
committed
fix: reapply thousand separators before passing input to appendDecimals
This ensures that the input going into `appendDecimals` is not malformed when the `hideFraction` option is On, otherwise when hitting delete on the text `"1,234,567"`, it will result in the text `"1,234,56"` which the formatter will parse as `1234.56`. This doesn't happen when `hideFraction` is off since hitting delete on the text `"12,345.67"` results in `"12,345.6"`, which `appendDecimals` will happily handle in a separate case to provide `"1234.56"` as the input into `currencyToAmount`.
1 parent 9952412 commit 8d6d966

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

packages/desktop-client/src/components/mobile/transactions/FocusableAmountInput.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
amountToCurrency,
2020
appendDecimals,
2121
currencyToAmount,
22+
reapplyThousandSeparators,
2223
} from 'loot-core/shared/util';
2324

2425
import { makeAmountFullStyle } from '@desktop-client/components/budget/util';
@@ -113,6 +114,7 @@ const AmountInput = memo(function AmountInput({
113114
};
114115

115116
const onChangeText = (text: string) => {
117+
text = reapplyThousandSeparators(text);
116118
text = appendDecimals(text, String(hideFraction) === 'true');
117119
setEditing(true);
118120
setText(text);

packages/desktop-client/src/components/util/AmountInput.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { View } from '@actual-app/components/view';
1818
import { css, cx } from '@emotion/css';
1919

2020
import { evalArithmetic } from 'loot-core/shared/arithmetic';
21-
import { amountToInteger, appendDecimals } from 'loot-core/shared/util';
21+
import {
22+
amountToInteger,
23+
appendDecimals,
24+
reapplyThousandSeparators,
25+
} from 'loot-core/shared/util';
2226

2327
import { useFormat } from '@desktop-client/hooks/useFormat';
2428
import { useMergedRefs } from '@desktop-client/hooks/useMergedRefs';
@@ -96,6 +100,7 @@ export function AmountInput({
96100
}
97101

98102
function onInputTextChange(val) {
103+
val = reapplyThousandSeparators(val);
99104
val = autoDecimals
100105
? appendDecimals(val, String(hideFraction) === 'true')
101106
: val;

packages/loot-core/src/shared/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ export function titleFirst(str: string | null | undefined) {
214214
return str[0].toUpperCase() + str.slice(1);
215215
}
216216

217+
export function reapplyThousandSeparators(amountText: string) {
218+
const { decimalSeparator, thousandsSeparator } = getNumberFormat();
219+
const [integerPartRaw, decimalPart = ''] = amountText.split(decimalSeparator);
220+
const integerPart = Number(integerPartRaw.replaceAll(thousandsSeparator, ''))
221+
.toLocaleString('en-US')
222+
.replaceAll(',', thousandsSeparator);
223+
return decimalPart
224+
? integerPart + decimalSeparator + decimalPart
225+
: integerPart;
226+
}
227+
217228
export function appendDecimals(
218229
amountText: string,
219230
hideDecimals = false,

upcoming-release-notes/5220.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: Bugfix
3+
authors: [intagaming]
4+
---
5+
6+
Reapply thousand separators before passing input to appendDecimals

0 commit comments

Comments
 (0)