-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (75 loc) · 2.29 KB
/
index.js
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
import styles from "./Pledge.module.scss";
import { motion } from "framer-motion";
import { useRef, useState } from "react";
import clsx from "clsx";
export const Pledge = ({ minimumAmount, id }) => {
const [lowValueErrorMessage, setLowValueErrorMessage] = useState("");
const [buttonDisabled, setButtonDisabled] = useState(true);
const amountInputRef = useRef();
const validatePledgeAmount = () => {
if (Number(amountInputRef.current.value) < minimumAmount) {
setLowValueErrorMessage(
`Value must be greater than or equal to $${minimumAmount}`
);
setButtonDisabled(true);
} else {
setLowValueErrorMessage(null);
setButtonDisabled(false);
}
};
const handleChange = (event) => {
event.target.value = event.target.value.replace(/\D/g, "");
validatePledgeAmount();
};
const inputValidationClassName =
buttonDisabled && minimumAmount !== ""
? styles.invalidInput
: styles.validInput;
const variants = {
hidden: { opacity: 0 },
visible: { opacity: 1 },
};
return (
<div className={styles.pledgeCardAndErrorMessage}>
<motion.div
initial="hidden"
animate="visible"
variants={variants}
transition={{ duration: 1 }}
className={styles.pledgeCard}
data-testid="pledge-input"
>
<p className={styles.pledgeTitle}>Enter your pledge</p>
<div className={styles.pledgeAndSubmit}>
<input
type="text"
ref={amountInputRef}
className={`${styles.pledgeInput} ${inputValidationClassName}`}
name="pledgeAmount"
required
onChange={handleChange}
id={id}
data-testid="input-value"
/>
<label htmlFor={id} className={styles.placeholder}>
$
</label>
<button
className={clsx(
styles.pledgeButton,
buttonDisabled && styles.buttonDisabled
)}
disabled={buttonDisabled}
>
Continue
</button>
</div>
</motion.div>
{lowValueErrorMessage && minimumAmount !== "" ? (
<p data-testid="low-value-error" className={styles.errorMessage}>
{lowValueErrorMessage}
</p>
) : null}
</div>
);
};