diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/taler-wallet-webextension/src/components/AmountField.tsx | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/packages/taler-wallet-webextension/src/components/AmountField.tsx b/packages/taler-wallet-webextension/src/components/AmountField.tsx index 4936e0604..786244433 100644 --- a/packages/taler-wallet-webextension/src/components/AmountField.tsx +++ b/packages/taler-wallet-webextension/src/components/AmountField.tsx @@ -31,6 +31,9 @@ import { TextField } from "../mui/TextField.js"; const HIGH_DENOM_SYMBOL = ["", "K", "M", "G", "T", "P"]; const LOW_DENOM_SYMBOL = ["", "m", "mm", "n", "p", "f"]; +/** + * Show normalized value based on the currency unit + */ export function AmountField({ label, handler, @@ -77,7 +80,7 @@ export function AmountField({ const previousValue = Amounts.stringifyValue(handler.value, decimalPlaces); - const normal = denormalize(handler.value, unit) ?? handler.value; + const normal = normalize(handler.value, unit) ?? handler.value; let textValue = Amounts.stringifyValue(normal, decimalPlaces); if (decimalPlaces === 0) { @@ -95,25 +98,25 @@ export function AmountField({ } try { //remove all but last dot - const parsed = value.replace(/(\.)(?=.*\1)/g, ""); - const parts = parsed.split("."); + const withoutDots = value.replace(/(\.)(?=.*\1)/g, ""); + const parts = withoutDots.split("."); setDecimalPlaces(parts.length === 1 ? undefined : parts[1].length); //FIXME: should normalize before parsing //parsing first add some restriction on the rage of the values - const real = parseValue(currency, parsed); + const parsed = parseValue(currency, withoutDots); - if (!real || real.value < 0) { + if (!parsed || parsed.value < 0) { return previousValue; } - const realNormalized = normalize(real, unit); + const realValue = denormalize(parsed, unit); // console.log(real, unit, normal); - if (realNormalized && handler.onInput) { - handler.onInput(realNormalized); + if (realValue && handler.onInput) { + handler.onInput(realValue); } - return parsed; + return withoutDots; } catch (e) { // do nothing } @@ -191,7 +194,14 @@ function parseValue(currency: string, s: string): AmountJson | undefined { return { currency, fraction, value }; } -function normalize(amount: AmountJson, unit: number): AmountJson | undefined { +/** + * Return the real value of a normalized unit + * If the value is 20 and the unit is kilo == 1000 the returned value will be amount * 1000 + * @param amount + * @param unit + * @returns + */ +function denormalize(amount: AmountJson, unit: number): AmountJson | undefined { if (unit === 1 || Amounts.isZero(amount)) return amount; const result = unit < 1 @@ -200,7 +210,15 @@ function normalize(amount: AmountJson, unit: number): AmountJson | undefined { return result; } -function denormalize(amount: AmountJson, unit: number): AmountJson | undefined { +/** + * Return the amount in the current unit. + * If the value is 20000 and the unit is kilo == 1000 and the returned value will be amount / unit + * + * @param amount + * @param unit + * @returns + */ +function normalize(amount: AmountJson, unit: number): AmountJson | undefined { if (unit === 1 || Amounts.isZero(amount)) return amount; const result = unit < 1 |