Skip to content

Commit

Permalink
feat: allow only two decimals when they are all zeros in currencyInpu…
Browse files Browse the repository at this point in the history
…t and using card payment
  • Loading branch information
r41ph committed Jan 6, 2025
1 parent 7b1bf03 commit df367f1
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ export const CurrencyInput = ({

const handleOnChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
// Allow only two decimals when they are all zeros
const value = event.target.value;
const decimalPart = value.split('.')?.[1];
if (
decimalPart &&
decimalPart.length > 2 &&
decimalPart.startsWith('00') &&
paymentOption === PAYMENT_OPTIONS.CARD
) {
event.target.value = value.slice(0, -1);
}

onChange(event);
handleCurrencyAmountChange(event);
},
[handleCurrencyAmountChange, onChange],
[handleCurrencyAmountChange, onChange, paymentOption],
);

const handleInput = useCallback(
Expand All @@ -78,6 +90,7 @@ export const CurrencyInput = ({
if (
(decimalPart &&
decimalPart.length > 2 &&
!decimalPart.startsWith('00') &&
paymentOption === PAYMENT_OPTIONS.CARD) ||
/^0[0-9]/.test(value)
) {
Expand Down

0 comments on commit df367f1

Please sign in to comment.