Merge pull request #4685

7b52b92 qml: fix broken leading-zero stripping in amount fields (Cole Munz)

ACKs: plowsof*, jpk68, selsta
This commit is contained in:
tobtoht committed 2026-10-25 06:49:23 +00:00
commit a488add012
4 files changed
+34 -46

No files matched your search

+18
View File
@@ -122,6 +122,24 @@ function removeTrailingZeros(value) {
return (value + '').replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '');
}
// Strips a redundant leading zero from an amount field as it's typed
// (007 -> 7, 05 -> 5) and adjusts the cursor to stay in place. Also
// turns a leading "." into "0." so the field doesn't start on a dot.
// Takes the TextInput's current text/cursorPosition and returns the
// corrected pair; the caller assigns both back to the field.
function stripLeadingZeros(text, cursorPosition) {
const match = text.match(/^0+(\d.*)/);
if (match) {
const removedCharacters = text.length - match[1].length;
text = match[1];
cursorPosition = Math.max(cursorPosition - removedCharacters, 0);
} else if (text.indexOf('.') === 0) {
text = '0' + text;
cursorPosition = text.length > 2 ? 1 : text.length;
}
return { text: text, cursorPosition: cursorPosition };
}
function parseDateStringOrRestoreHeightAsInteger(value) {
// Parse date string or restore height as integer
var restoreHeight = 0;
+7 -22
View File
@@ -44,6 +44,7 @@ import moneroComponents.TransactionHistoryModel 1.0
import moneroComponents.Subaddress 1.0
import moneroComponents.SubaddressModel 1.0
import "../js/TxUtils.js" as TxUtils
import "../js/Utils.js" as Utils
Rectangle {
id: pageReceive
@@ -262,17 +263,9 @@ Rectangle {
}
onTextEdited: {
text = text.trim().replace(",", ".");
const match = text.match(/^0+(\d.*)/);
if (match) {
const cursorPosition = cursorPosition;
text = match[1];
cursorPosition = Math.max(cursorPosition, 1) - 1;
} else if(text.indexOf('.') === 0){
text = '0' + text;
if (text.length > 2) {
cursorPosition = 1;
}
}
const corrected = Utils.stripLeadingZeros(text, cursorPosition);
text = corrected.text;
cursorPosition = corrected.cursorPosition;
if (amountToReceiveFiat.text == "") {
amountToReceiveXMR.text = "";
} else {
@@ -322,17 +315,9 @@ Rectangle {
}
onTextEdited: {
text = text.trim().replace(",", ".");
const match = text.match(/^0+(\d.*)/);
if (match) {
const cursorPosition = cursorPosition;
text = match[1];
cursorPosition = Math.max(cursorPosition, 1) - 1;
} else if(text.indexOf('.') === 0){
text = '0' + text;
if (text.length > 2) {
cursorPosition = 1;
}
}
const corrected = Utils.stripLeadingZeros(text, cursorPosition);
text = corrected.text;
cursorPosition = corrected.cursorPosition;
if (amountToReceiveXMR.text == "") {
amountToReceiveFiat.text = "";
} else {
+3 -11
View File
@@ -504,17 +504,9 @@ Rectangle {
text: amount
onTextChanged: {
text = text.trim().replace(",", ".");
const match = text.match(/^0+(\d.*)/);
if (match) {
const cursorPosition = cursorPosition;
text = match[1];
cursorPosition = Math.max(cursorPosition, 1) - 1;
} else if(text.indexOf('.') === 0){
text = '0' + text;
if (text.length > 2) {
cursorPosition = 1;
}
}
const corrected = Utils.stripLeadingZeros(text, cursorPosition);
text = corrected.text;
cursorPosition = corrected.cursorPosition;
error = walletManager.amountFromString(text) > appWindow.getUnlockedBalance();
amount = text;
+6 -13
View File
@@ -34,6 +34,7 @@ import "../components" as MoneroComponents
import moneroComponents.Clipboard 1.0
import "../js/TxUtils.js" as TxUtils
import "../js/Utils.js" as Utils
Rectangle {
color: "transparent"
@@ -111,19 +112,11 @@ Rectangle {
enabled: getProofAddressLine.text.length === 0 && getProofTxIdLine.text.length === 0
onTextChanged: {
text = text.trim().replace(",", ".");
const match = text.match(/^0+(\d.*)/);
if (match) {
const cursorPosition = cursorPosition;
text = match[1];
cursorPosition = Math.max(cursorPosition, 1) - 1;
} else if(text.indexOf('.') === 0) {
text = '0' + text;
if (text.length > 2) {
cursorPosition = 1;
}
}
error = walletManager.amountFromString(text) > appWindow.getUnlockedBalance();
}
const corrected = Utils.stripLeadingZeros(text, cursorPosition);
text = corrected.text;
cursorPosition = corrected.cursorPosition;
error = walletManager.amountFromString(text) > appWindow.getUnlockedBalance();
}
validator: RegularExpressionValidator {
regularExpression: /^\s*(\d{1,8})?([\.,]\d{1,12})?\s*$/
}