Merge pull request #2123 from BlueWallet/amountregex

FIX: Set '0.' on BTC Amount
This commit is contained in:
GLaDOS 2020-11-17 17:00:40 +00:00 committed by GitHub
commit af1412d4f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2290,19 +2290,19 @@ export class BlueBitcoinAmount extends Component {
secondaryDisplayCurrency = formatBalanceWithoutSuffix(sat, BitcoinUnit.LOCAL_CURRENCY, false); secondaryDisplayCurrency = formatBalanceWithoutSuffix(sat, BitcoinUnit.LOCAL_CURRENCY, false);
break; break;
case BitcoinUnit.SATS: case BitcoinUnit.SATS:
secondaryDisplayCurrency = formatBalanceWithoutSuffix(amount.toString(), BitcoinUnit.LOCAL_CURRENCY, false); secondaryDisplayCurrency = formatBalanceWithoutSuffix((isNaN(amount) ? 0 : amount).toString(), BitcoinUnit.LOCAL_CURRENCY, false);
break; break;
case BitcoinUnit.LOCAL_CURRENCY: case BitcoinUnit.LOCAL_CURRENCY:
secondaryDisplayCurrency = currency.fiatToBTC(parseFloat(amount)); secondaryDisplayCurrency = currency.fiatToBTC(parseFloat(isNaN(amount) ? 0 : amount));
if (BlueBitcoinAmount.conversionCache[amount + BitcoinUnit.LOCAL_CURRENCY]) { if (BlueBitcoinAmount.conversionCache[isNaN(amount) ? 0 : amount + BitcoinUnit.LOCAL_CURRENCY]) {
// cache hit! we reuse old value that supposedly doesnt have rounding errors // cache hit! we reuse old value that supposedly doesn't have rounding errors
const sats = BlueBitcoinAmount.conversionCache[amount + BitcoinUnit.LOCAL_CURRENCY]; const sats = BlueBitcoinAmount.conversionCache[isNaN(amount) ? 0 : amount + BitcoinUnit.LOCAL_CURRENCY];
secondaryDisplayCurrency = currency.satoshiToBTC(sats); secondaryDisplayCurrency = currency.satoshiToBTC(sats);
} }
break; break;
} }
if (amount === BitcoinUnit.MAX) secondaryDisplayCurrency = ''; // we dont want to display NaN if (amount === BitcoinUnit.MAX) secondaryDisplayCurrency = ''; // we don't want to display NaN
return ( return (
<TouchableWithoutFeedback disabled={this.props.pointerEvents === 'none'} onPress={() => this.textInput.focus()}> <TouchableWithoutFeedback disabled={this.props.pointerEvents === 'none'} onPress={() => this.textInput.focus()}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}> <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
@ -2342,16 +2342,12 @@ export class BlueBitcoinAmount extends Component {
} else { } else {
text = `${parseInt(split[0], 10)}`; text = `${parseInt(split[0], 10)}`;
} }
text = this.state.unit === BitcoinUnit.BTC ? text.replace(/[^0-9.]/g, '') : text.replace(/[^0-9]/g, ''); text = this.state.unit === BitcoinUnit.BTC ? text.replace(/[^0-9.]/g, '') : text.replace(/[^0-9]/g, '');
text = text.replace(/(\..*)\./g, '$1');
if (text.startsWith('.')) { if (text.startsWith('.')) {
text = '0.'; text = '0.';
} }
text = text.replace(/(0{1,}.)\./g, '$1');
if (this.state.unit !== BitcoinUnit.BTC) {
text = text.replace(/[^0-9.]/g, '');
}
} else if (this.state.unit === BitcoinUnit.LOCAL_CURRENCY) { } else if (this.state.unit === BitcoinUnit.LOCAL_CURRENCY) {
text = text.replace(/,/gi, ''); text = text.replace(/,/gi, '');
if (text.split('.').length > 2) { if (text.split('.').length > 2) {
@ -2367,9 +2363,9 @@ export class BlueBitcoinAmount extends Component {
} }
text = rez; text = rez;
} }
text = text.replace(/[^\d.,-]/g, ''); // remove all but numberd, dots & commas text = text.replace(/[^\d.,-]/g, ''); // remove all but numbers, dots & commas
text = text.replace(/(\..*)\./g, '$1');
} }
this.props.onChangeText(text); this.props.onChangeText(text);
}} }}
onBlur={() => { onBlur={() => {
@ -2382,7 +2378,7 @@ export class BlueBitcoinAmount extends Component {
maxLength={this.maxLength()} maxLength={this.maxLength()}
ref={textInput => (this.textInput = textInput)} ref={textInput => (this.textInput = textInput)}
editable={!this.props.isLoading && !this.props.disabled} editable={!this.props.isLoading && !this.props.disabled}
value={parseFloat(amount) > 0 || amount === BitcoinUnit.MAX ? amount : undefined} value={parseFloat(amount) >= 0 || amount === BitcoinUnit.MAX ? amount : undefined}
placeholderTextColor={ placeholderTextColor={
this.props.disabled ? BlueCurrentTheme.colors.buttonDisabledTextColor : BlueCurrentTheme.colors.alternativeTextColor2 this.props.disabled ? BlueCurrentTheme.colors.buttonDisabledTextColor : BlueCurrentTheme.colors.alternativeTextColor2
} }