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

feat(ramp): improve amount editing formatting #11613

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions app/components/UI/Ramp/Views/BuildQuote/BuildQuote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,36 @@ const BuildQuote = () => {
);
}

const displayAmount = isBuy
? formatAmount(amountNumber)
: `${amount} ${selectedAsset?.symbol}`;
// If the current view is for Sell the amount (crypto) is displayed as is
let displayAmount = `${amount} ${selectedAsset?.symbol}`;

// If the current ivew is for Buy we will format the amount
if (isBuy) {
// Split the amount to detect if it has decimals
const splitAmount = amount.split(/(\.)|(,)/);
// If the splitAmount array has more than 1 element it means that the amount has decimals
// For example:
// 100.50 -> splitAmount = ['100', '.', undefined, '50']
// 100,50 -> splitAmount = ['100', undefined, ',', '50']
// Note: this help us capture the input separator (dot or comma)
const hasDecimalsSplit = splitAmount.length > 1;

displayAmount =
isBuy && amountFocused
? // If the amount is focused (being edited) the amount integer part will be shown in groups separated by spaces
`${formatAmount(Math.trunc(amountNumber), true)}${
// If the amount has decimals the decimal part will be shown
// using the separator and the decimal part
// Note, the decimal part will be displayed even if it is being typed (ends with a separator or 0)
hasDecimalsSplit
? `${splitAmount[1] ?? splitAmount[2] ?? ''}${
splitAmount[3] ?? ''
}`
: ''
}`
: // If the amount is not focused it will be fully formatted
formatAmount(amountNumber);
}

let quickAmounts: QuickAmount[] = [];

Expand Down
12 changes: 10 additions & 2 deletions app/components/UI/Ramp/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ export const formatId = (id: string) => {
return id.startsWith('/') ? id : '/' + id;
};

export function formatAmount(amount: number) {
export function formatAmount(amount: number, useParts = false) {
try {
if (Intl?.NumberFormat) return new Intl.NumberFormat().format(amount);
if (Intl?.NumberFormat) {
if (useParts) {
return new Intl.NumberFormat()
.formatToParts(amount)
.map(({ type, value }) => (type === 'integer' ? value : ''))
.join(' ');
}
return new Intl.NumberFormat().format(amount);
}
return String(amount);
} catch (e) {
return String(amount);
Expand Down
Loading