Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use left/right arrow keys to move cursor in IOUAmountPage #3850

Merged
merged 9 commits into from
Jul 2, 2021
32 changes: 16 additions & 16 deletions src/pages/iou/steps/IOUAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ class IOUAmountPage extends React.Component {
this.unsubscribe();
}

/**
* Check if amount is a decimal upto 3 digits
*
* @param {String} amount
* @returns {boolean} Return `true` if amount is a decimal upto 3 digits, else `false`
rushatgabhane marked this conversation as resolved.
Show resolved Hide resolved
*/
validateAmount(amount) {
const decimalNumberRegex = new RegExp(/^\d+(\.\d{0,3})?$/, 'i');
return amount === '' || decimalNumberRegex.test(amount);
}

/**
* Update amount with number or Backspace pressed for BigNumberPad.
* Validate new amount with decimal number regex up to 6 digits and 2 decimal digit to enable Next button
Expand All @@ -113,23 +124,15 @@ class IOUAmountPage extends React.Component {
if (key === '<') {
roryabraham marked this conversation as resolved.
Show resolved Hide resolved
if (this.state.amount.length > 0) {
this.setState(prevState => ({
amount: prevState.amount.substring(0, prevState.amount.length - 1),
amount: prevState.amount.slice(0, -1),
}));
}
return;
}

this.setState((prevState) => {
const newValue = `${prevState.amount}${key}`;

// Regex to validate decimal number with up to 3 decimal numbers
const decimalNumberRegex = new RegExp(/^\d+(\.\d{0,3})?$/, 'i');
if (!decimalNumberRegex.test(newValue)) {
return prevState;
}
return {
amount: newValue,
};
const amount = `${prevState.amount}${key}`;
return this.validateAmount(amount) ? {amount} : prevState;
});
}

Expand All @@ -140,12 +143,9 @@ class IOUAmountPage extends React.Component {
* @param {String} amount
*/
updateAmount(amount) {
// Regex to validate decimal number with up to 3 decimal numbers
const decimalNumberRegex = new RegExp(/^\d+(\.\d{0,3})?$/, 'i');
if (amount !== '' && !decimalNumberRegex.test(amount)) {
return;
if (this.validateAmount(amount)) {
this.setState({amount});
}
this.setState({amount});
}

render() {
Expand Down