|
| 1 | +import React from "react"; |
| 2 | +import { Input, Typography, Button } from "@material-tailwind/react"; |
| 3 | + |
| 4 | +export function InputCreditCard() { |
| 5 | + const [cvv, setCvv] = React.useState(""); |
| 6 | + const [cardNumber, setCardNumber] = React.useState(""); |
| 7 | + const [expirationDate, setExpirationDate] = React.useState(""); |
| 8 | + const [cardholderName, setCardholderName] = React.useState(""); |
| 9 | + return ( |
| 10 | + <div className="w-full max-w-sm"> |
| 11 | + <Typography |
| 12 | + variant="small" |
| 13 | + color="blue-gray" |
| 14 | + className="mb-1 block font-medium" |
| 15 | + > |
| 16 | + Cardholder Name |
| 17 | + </Typography> |
| 18 | + <Input |
| 19 | + placeholder="e.g John Doe" |
| 20 | + className="appearance-none !border-t-blue-gray-200 placeholder:text-blue-gray-300 placeholder:opacity-100 focus:!border-t-gray-900 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none" |
| 21 | + labelProps={{ |
| 22 | + className: "before:content-none after:content-none", |
| 23 | + }} |
| 24 | + containerProps={{ |
| 25 | + className: "min-w-0", |
| 26 | + }} |
| 27 | + value={cardholderName} |
| 28 | + onChange={(e) => setCardholderName(e.target.value)} |
| 29 | + /> |
| 30 | + |
| 31 | + <Typography |
| 32 | + variant="small" |
| 33 | + color="blue-gray" |
| 34 | + className="mb-1 mt-4 block font-medium" |
| 35 | + > |
| 36 | + Card Number |
| 37 | + </Typography> |
| 38 | + <Input |
| 39 | + placeholder="1234 5678 9012 3456" |
| 40 | + maxLength={19} |
| 41 | + className="appearance-none !border-t-blue-gray-200 placeholder:text-blue-gray-300 placeholder:opacity-100 focus:!border-t-gray-900 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none" |
| 42 | + labelProps={{ |
| 43 | + className: "before:content-none after:content-none", |
| 44 | + }} |
| 45 | + containerProps={{ |
| 46 | + className: "min-w-0", |
| 47 | + }} |
| 48 | + value={cardNumber |
| 49 | + .replace(/\s/g, "") |
| 50 | + .replace(/(\d{4})/g, "$1 ") |
| 51 | + .trim()} |
| 52 | + onChange={(e) => setCardNumber(e.target.value)} |
| 53 | + /> |
| 54 | + |
| 55 | + <div className="mt-4 flex"> |
| 56 | + <div className="mr-4 w-full md:w-8/12"> |
| 57 | + <Typography |
| 58 | + variant="small" |
| 59 | + color="blue-gray" |
| 60 | + className="mb-1 block font-medium" |
| 61 | + > |
| 62 | + Expiration Date |
| 63 | + </Typography> |
| 64 | + <Input |
| 65 | + placeholder="MM/YY" |
| 66 | + maxLength={5} |
| 67 | + pattern="\d{2}/\d{2}" |
| 68 | + className="appearance-none !border-t-blue-gray-200 placeholder:text-blue-gray-300 placeholder:opacity-100 focus:!border-t-gray-900 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none" |
| 69 | + labelProps={{ |
| 70 | + className: "before:content-none after:content-none", |
| 71 | + }} |
| 72 | + containerProps={{ |
| 73 | + className: "!min-w-0", |
| 74 | + }} |
| 75 | + value={expirationDate |
| 76 | + .replace(/[^0-9]/g, "") |
| 77 | + .replace(/(\d{2})(\d{1,2})/, "$1/$2") |
| 78 | + .substring(0, 5)} |
| 79 | + onChange={(e) => setExpirationDate(e.target.value)} |
| 80 | + /> |
| 81 | + </div> |
| 82 | + <div className="w-full md:w-4/12"> |
| 83 | + <Typography |
| 84 | + variant="small" |
| 85 | + color="blue-gray" |
| 86 | + className="mb-1 block font-medium" |
| 87 | + > |
| 88 | + CVV |
| 89 | + </Typography> |
| 90 | + <Input |
| 91 | + placeholder="123" |
| 92 | + maxLength={3} |
| 93 | + pattern="\d{3}" |
| 94 | + className="appearance-none !border-t-blue-gray-200 placeholder:text-blue-gray-300 placeholder:opacity-100 focus:!border-t-gray-900 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none" |
| 95 | + labelProps={{ |
| 96 | + className: "before:content-none after:content-none", |
| 97 | + }} |
| 98 | + containerProps={{ |
| 99 | + className: "!min-w-0", |
| 100 | + }} |
| 101 | + value={cvv.replace(/[^0-9]/g, "").replace(/(\..*)\./g, "$1")} |
| 102 | + onChange={(e) => setCvv(e.target.value)} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + <Button className="mt-4" fullWidth> |
| 107 | + Purchase Now |
| 108 | + </Button> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
0 commit comments