Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import { Box } from "@mui/material";
import { Text } from "@common/Text";
import { palette } from "@palette";
import { Dispatch, SetStateAction, useCallback, useEffect, useState } from "react";
import CustomDonationDigitInput from "@components/Fundraisers/PublicPaymentForm/CustomDonationDigitInput";
import numeral from "numeral";
import { parseAmount } from "@utils/index";
import { MAX_ALLOWED_AMOUNT } from "@constants/constants";
type CustomDonationInputProps = {
min?: number;
max?: number;
handleAdd: () => void;
handleSubtract: () => void;
handleSetValue: (value: number) => void;
value: number;
selected?: boolean;
wasModified?: boolean;
setWasModified?: Dispatch<SetStateAction<boolean>>
};
const CustomDonationInput = ({
min,
max = MAX_ALLOWED_AMOUNT,
handleSetValue,
selected,
value: amount,
wasModified,
setWasModified,
}: CustomDonationInputProps) => {
const [isInput, setIsInput] = useState<boolean>(true);
const [value, setValue] = useState<number | string>("");
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!wasModified && setWasModified) setWasModified(true);
const val = event?.target?.value;
const newValue = val.replace(/(\..*)\./g, "$1");
if (newValue.endsWith(".")) return setValue(newValue);
let amount = numeral(newValue).value() || 0;
amount = Math.min(amount, max);
setValue(amount.toLocaleString("en-US", { maximumFractionDigits: 2 }));
};
const toggleInput = useCallback(
() => setIsInput((prev) => !prev),
[setIsInput],
);
useEffect(() => {
if (!selected && min && max) {
handleSetValue(numeral(value).value() || 0);
}
setIsInput(!!selected);
}, [selected]);
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === "Enter") {
const newValue = numeral(value).value();
if (newValue) {
handleSetValue(newValue);
} else if (setWasModified) {
setWasModified(true);
handleSetValue(0);
}
toggleInput();
}
};
const handleBlur = () => {
const newValue = numeral(value).value();
if (newValue) {
handleSetValue(newValue);
} else if (setWasModified) {
setWasModified(true);
handleSetValue(0);
}
toggleInput();
}
return (
<Box sx={{ userSelect: "none", height: "29px" }}>
{isInput ? (
<CustomDonationDigitInput
min={min}
max={max}
amount={value}
handleChange={handleChange}
handleKeyDown={handleKeyDown}
handleBlur={handleBlur}
/>
) : (
<Text
sx={{
cursor: "text",
fontWeight: 350,
...(!wasModified && {color: "#B8B8B8"}),
}}
onClick={toggleInput}
color={palette.neutral[80]}
variant="h3"
lineHeight="29px"
borderRadius="90px"
fontSize="32px"
>
{parseAmount(amount)}
</Text>
)}
</Box>
);
};
export default CustomDonationInput;
|