Skip to content

Commit

Permalink
fix: change logic to upscale amount in input field
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanielcruzz authored and fbwoolf committed Aug 7, 2023
1 parent da7b51b commit 086c329
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ChangeEvent } from 'react';
import { useCallback, useEffect, useState } from 'react';

import { Box, Flex, Input, Stack, Text, color } from '@stacks/ui';
Expand Down Expand Up @@ -52,6 +53,7 @@ export function AmountField({
tokenSymbol,
}: AmountFieldProps) {
const [field, meta, helpers] = useField('amount');

const showError = useShowFieldError('amount');
const [fontSize, setFontSize] = useState(maxFontSize);
const [previousTextLength, setPreviousTextLength] = useState(1);
Expand All @@ -62,19 +64,30 @@ export function AmountField({
const fontSizeModifier = (maxFontSize - minFontSize) / maxLength;
const subtractedLengthToPositionPrefix = 0.5;

useEffect(() => {
// case, when e.g token doesn't have symbol
if (symbol.length > 4) setFontSize(minFontSize);
const onChange = (event: ChangeEvent<HTMLInputElement>) => {
const value = event.currentTarget.value;

// Typing
if (field.value.length === symbol.length) setFontSize(maxFontSize);
if (field.value.length > symbol.length && previousTextLength > field.value.length) {
if (value.length <= symbol.length) {
setFontSize(maxFontSize);
} else if (previousTextLength > value.length) {
const textSize = Math.ceil(fontSize + fontSizeModifier);
fontSize < maxFontSize && setFontSize(textSize);
} else if (field.value.length > symbol.length && previousTextLength < field.value.length) {
if (fontSize < maxFontSize) {
setFontSize(textSize);
}
} else if (previousTextLength < value.length) {
const textSize = Math.ceil(fontSize - fontSizeModifier);
fontSize > minFontSize && setFontSize(textSize);
if (fontSize > minFontSize) {
setFontSize(textSize);
}
}

field.onChange(event);
};

useEffect(() => {
// case, when e.g token doesn't have symbol
if (symbol.length > 4) setFontSize(minFontSize);

// Copy/paste
if (field.value.length > symbol.length && field.value.length > previousTextLength + 2) {
const modifiedFontSize = getAmountModifiedFontSize({
Expand Down Expand Up @@ -139,6 +152,7 @@ export function AmountField({
fontWeight={500}
autoComplete={autoComplete}
{...field}
onChange={onChange}
/>
<Text fontSize={fontSize + 'px'} pl="tight">
{symbol.toUpperCase()}
Expand Down

0 comments on commit 086c329

Please sign in to comment.