-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix input field bugs #166
Merged
ebma
merged 13 commits into
polygon-prototype-staging
from
158-fix-transaction-sheet-dump-always-creating-new-sheet
Oct 9, 2024
Merged
Fix input field bugs #166
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4854d82
fix input field bugs
gianfra-t bd47eac
remove extra log
gianfra-t d0635ba
improvements to input form
gianfra-t 84b81ec
set toAmount to 0 when no data available
gianfra-t 09073f2
stabilize more toAmount display
gianfra-t afbacef
replacing fixes with portal implementation
gianfra-t 3d15ae6
handle leading zeros, empty string
gianfra-t 47fea00
cleanup
gianfra-t 8d85ebc
handle leading zeros on paste
gianfra-t e9534fe
remove unused function
gianfra-t 48c29cd
add empty string condition
gianfra-t 022edbf
Merge branch 'polygon-prototype-staging' into 158-fix-transaction-she…
ebma 59a937c
Revert faulty change of `disabled` prop of swap submit button
ebma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const removeNonNumericCharacters = (value: string): string => value.replace(/[^0-9.]/g, ''); | ||
|
||
const removeExtraDots = (value: string): string => value.replace(/(\..*?)\./g, '$1'); | ||
|
||
function sanitizeNumericInput(value: string): string { | ||
return removeExtraDots(removeNonNumericCharacters(value)); | ||
} | ||
|
||
export function trimToMaxDecimals(value: string, maxDecimals: number): string { | ||
const [integer, decimal] = value.split('.'); | ||
return decimal ? `${integer}.${decimal.slice(0, maxDecimals)}` : value; | ||
} | ||
|
||
const replaceCommasWithDots = (value: string): string => value.replace(/,/g, '.'); | ||
|
||
/** | ||
* Handles the input change event to ensure the value does not exceed the maximum number of decimal places, | ||
* replaces commas with dots, and removes invalid non-numeric characters. | ||
* | ||
* @param e - The keyboard event triggered by the input. | ||
* @param maxDecimals - The maximum number of decimal places allowed. | ||
*/ | ||
export function handleOnChangeNumericInput(e: KeyboardEvent, maxDecimals: number): void { | ||
const target = e.target as HTMLInputElement; | ||
|
||
target.value = replaceCommasWithDots(target.value); | ||
|
||
target.value = sanitizeNumericInput(target.value); | ||
|
||
target.value = trimToMaxDecimals(target.value, maxDecimals); | ||
|
||
target.value = handleLeadingZeros(target.value); | ||
|
||
target.value = replaceInvalidOrEmptyString(target.value); | ||
} | ||
|
||
function replaceInvalidOrEmptyString(value: string): string { | ||
if (value === '' || value === '.') { | ||
return '0'; | ||
} | ||
return value; | ||
} | ||
|
||
function handleLeadingZeros(value: string): string { | ||
if (Number(value) >= 1) { | ||
return value.replace(/^0+/, ''); | ||
} | ||
|
||
// Add leading zeros for numbers < 1 that don't start with '0' | ||
if (Number(value) < 1 && value[0] !== '0') { | ||
return '0' + value; | ||
} | ||
|
||
// No more than one leading zero | ||
return value.replace(/^0+/, '0'); | ||
} | ||
|
||
/** | ||
* Handles the paste event to ensure the value does not exceed the maximum number of decimal places, | ||
* replaces commas with dots, and removes invalid non-numeric characters. | ||
* | ||
* @param e - The clipboard event triggered by the input. | ||
* @param maxDecimals - The maximum number of decimal places allowed. | ||
* @returns The sanitized value after the paste event. | ||
*/ | ||
|
||
export function handleOnPasteNumericInput(e: ClipboardEvent, maxDecimals: number): string { | ||
const inputElement = e.target as HTMLInputElement; | ||
const { value, selectionStart, selectionEnd } = inputElement; | ||
|
||
const clipboardData = sanitizeNumericInput(e.clipboardData?.getData('text/plain') || ''); | ||
|
||
const combinedValue = value.slice(0, selectionStart || 0) + clipboardData + value.slice(selectionEnd || 0); | ||
|
||
const [integerPart, ...decimalParts] = combinedValue.split('.'); | ||
const sanitizedValue = integerPart + (decimalParts.length > 0 ? '.' + decimalParts.join('') : ''); | ||
|
||
e.preventDefault(); | ||
inputElement.value = trimToMaxDecimals(sanitizedValue, maxDecimals); | ||
inputElement.value = handleLeadingZeros(inputElement.value); | ||
|
||
const newCursorPosition = | ||
(selectionStart || 0) + clipboardData.length - (combinedValue.length - sanitizedValue.length); | ||
inputElement.setSelectionRange(newCursorPosition, newCursorPosition); | ||
|
||
return trimToMaxDecimals(sanitizedValue, maxDecimals); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not used.