more doc
This commit is contained in:
parent
603efbd073
commit
9b0d887a1b
@ -31,6 +31,9 @@ import { TextField } from "../mui/TextField.js";
|
|||||||
const HIGH_DENOM_SYMBOL = ["", "K", "M", "G", "T", "P"];
|
const HIGH_DENOM_SYMBOL = ["", "K", "M", "G", "T", "P"];
|
||||||
const LOW_DENOM_SYMBOL = ["", "m", "mm", "n", "p", "f"];
|
const LOW_DENOM_SYMBOL = ["", "m", "mm", "n", "p", "f"];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show normalized value based on the currency unit
|
||||||
|
*/
|
||||||
export function AmountField({
|
export function AmountField({
|
||||||
label,
|
label,
|
||||||
handler,
|
handler,
|
||||||
@ -77,7 +80,7 @@ export function AmountField({
|
|||||||
|
|
||||||
const previousValue = Amounts.stringifyValue(handler.value, decimalPlaces);
|
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);
|
let textValue = Amounts.stringifyValue(normal, decimalPlaces);
|
||||||
if (decimalPlaces === 0) {
|
if (decimalPlaces === 0) {
|
||||||
@ -95,25 +98,25 @@ export function AmountField({
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
//remove all but last dot
|
//remove all but last dot
|
||||||
const parsed = value.replace(/(\.)(?=.*\1)/g, "");
|
const withoutDots = value.replace(/(\.)(?=.*\1)/g, "");
|
||||||
const parts = parsed.split(".");
|
const parts = withoutDots.split(".");
|
||||||
setDecimalPlaces(parts.length === 1 ? undefined : parts[1].length);
|
setDecimalPlaces(parts.length === 1 ? undefined : parts[1].length);
|
||||||
|
|
||||||
//FIXME: should normalize before parsing
|
//FIXME: should normalize before parsing
|
||||||
//parsing first add some restriction on the rage of the values
|
//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;
|
return previousValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const realNormalized = normalize(real, unit);
|
const realValue = denormalize(parsed, unit);
|
||||||
|
|
||||||
// console.log(real, unit, normal);
|
// console.log(real, unit, normal);
|
||||||
if (realNormalized && handler.onInput) {
|
if (realValue && handler.onInput) {
|
||||||
handler.onInput(realNormalized);
|
handler.onInput(realValue);
|
||||||
}
|
}
|
||||||
return parsed;
|
return withoutDots;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@ -191,7 +194,14 @@ function parseValue(currency: string, s: string): AmountJson | undefined {
|
|||||||
return { currency, fraction, value };
|
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;
|
if (unit === 1 || Amounts.isZero(amount)) return amount;
|
||||||
const result =
|
const result =
|
||||||
unit < 1
|
unit < 1
|
||||||
@ -200,7 +210,15 @@ function normalize(amount: AmountJson, unit: number): AmountJson | undefined {
|
|||||||
return result;
|
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;
|
if (unit === 1 || Amounts.isZero(amount)) return amount;
|
||||||
const result =
|
const result =
|
||||||
unit < 1
|
unit < 1
|
||||||
|
Loading…
Reference in New Issue
Block a user