|
@@ -22,6 +22,7 @@ interface Props extends BareProps {
|
|
|
autoFocus?: boolean;
|
|
|
bitLength?: BitLength;
|
|
|
defaultValue?: BN | string;
|
|
|
+ defaultSi?: SiDef;
|
|
|
help?: React.ReactNode;
|
|
|
isDisabled?: boolean;
|
|
|
isError?: boolean;
|
|
@@ -110,22 +111,26 @@ function inputToBn (input: string, si: SiDef | null, props: Props): [BN, boolean
|
|
|
const [siPower, basePower, siUnitPower] = getSiPowers(si);
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
|
|
|
- const isDecimalValue = input.match(/^(\d+)\.(\d+)$/);
|
|
|
+ const decimalMatch = input.match(/^(\d+)[.,](\d+)$/);
|
|
|
|
|
|
let result;
|
|
|
|
|
|
- if (isDecimalValue) {
|
|
|
- if (siUnitPower - isDecimalValue[2].length < -basePower) {
|
|
|
+ if (decimalMatch) {
|
|
|
+ const intString = decimalMatch[1];
|
|
|
+ const fractionString = decimalMatch[2];
|
|
|
+ if (siUnitPower - fractionString.length < -basePower) {
|
|
|
result = new BN(-1);
|
|
|
}
|
|
|
|
|
|
- const div = new BN(input.replace(/\.\d*$/, ''));
|
|
|
- const modString = input.replace(/^\d+\./, '');
|
|
|
- const mod = new BN(modString);
|
|
|
+ const div = new BN(intString);
|
|
|
+ const mod = new BN(fractionString);
|
|
|
|
|
|
- result = div
|
|
|
- .mul(TEN.pow(siPower))
|
|
|
- .add(mod.mul(TEN.pow(new BN(basePower + siUnitPower - modString.length))));
|
|
|
+ // multiply whole number part with 10^SI
|
|
|
+ const divMul = div.mul(TEN.pow(new BN(siUnitPower)));
|
|
|
+ // multiply the fractional part with 10^(SI-length)
|
|
|
+ const modMul = mod.mul(TEN.pow(new BN(siUnitPower - fractionString.length)));
|
|
|
+
|
|
|
+ result = divMul.add(modMul);
|
|
|
} else {
|
|
|
result = new BN(input.replace(/[^\d]/g, ''))
|
|
|
.mul(TEN.pow(siPower));
|
|
@@ -195,7 +200,7 @@ function isNewPropsValue (propsValue: BN | string, value: string, valueBn: BN):
|
|
|
|
|
|
function InputNumber (props: Props): React.ReactElement<Props> {
|
|
|
const { t } = useTranslation();
|
|
|
- const { bitLength = DEFAULT_BITLENGTH, className, defaultValue = ZERO, help, isDecimal, isFull, isSi, isDisabled, isError = false, maxLength, maxValue, onChange, onEnter, onEscape, placeholder, style, value: propsValue } = props;
|
|
|
+ const { bitLength = DEFAULT_BITLENGTH, className, defaultValue = ZERO, defaultSi, help, isDecimal, isFull, isSi, isDisabled, isError = false, maxLength, maxValue, onChange, onEnter, onEscape, placeholder, style, value: propsValue } = props;
|
|
|
|
|
|
const [si, setSi] = useState<SiDef | null>(isSi ? formatBalance.findSi('-') : null);
|
|
|
const [isPreKeyDown, setIsPreKeyDown] = useState(false);
|
|
@@ -254,6 +259,11 @@ function InputNumber (props: Props): React.ReactElement<Props> {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ useEffect((): void => {
|
|
|
+ if (defaultSi) {
|
|
|
+ setSi(defaultSi);
|
|
|
+ }
|
|
|
+ }, [defaultSi]);
|
|
|
const _onSelectSiUnit = (siUnit: string): void => {
|
|
|
setSi(formatBalance.findSi(siUnit));
|
|
|
};
|
|
@@ -294,7 +304,7 @@ function InputNumber (props: Props): React.ReactElement<Props> {
|
|
|
{t('Max')}
|
|
|
</Button>
|
|
|
) */}
|
|
|
- {!!si && (
|
|
|
+ {isSi && !!si && (
|
|
|
<Dropdown
|
|
|
dropdownClassName='ui--SiDropdown'
|
|
|
isButton
|