You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactForm.tsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Box, Button, Paper, TextField } from '@mui/material';
  2. import { useFormik } from 'formik';
  3. import React, { useState } from 'react';
  4. import { contactSchema } from '../../../schemas/contactSchema';
  5. import { useCheckoutData } from '../../../store/checkout-context';
  6. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  7. const ContactForm = ({ submitHandler }) => {
  8. const [error] = useState({ hasError: false, errorMessage: '' });
  9. const { checkoutStorage } = useCheckoutData();
  10. const handleSubmit = async (values) => {
  11. submitHandler(values.email);
  12. };
  13. const formik = useFormik({
  14. initialValues: {
  15. email: checkoutStorage ? checkoutStorage.userInfo.email : '',
  16. },
  17. validationSchema: contactSchema,
  18. onSubmit: handleSubmit,
  19. validateOnBlur: true,
  20. enableReinitialize: true,
  21. });
  22. return (
  23. <Paper
  24. sx={{ p: 3, width: '90%', ml: 12, mt: 2, backgroundColor: '#f2f2f2' }}
  25. elevation={3}
  26. >
  27. <Box
  28. sx={{
  29. width: '100%',
  30. display: 'flex',
  31. flexDirection: 'column',
  32. }}
  33. >
  34. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  35. <Box
  36. component="form"
  37. onSubmit={formik.handleSubmit}
  38. sx={{ position: 'relative', mt: 1, p: 1 }}
  39. >
  40. <TextField
  41. name="email"
  42. label="Email"
  43. margin="normal"
  44. value={formik.values.email}
  45. onChange={formik.handleChange}
  46. error={formik.touched.email && Boolean(formik.errors.email)}
  47. helperText={formik.touched.email && formik.errors.email}
  48. fullWidth
  49. />
  50. <Button
  51. type="submit"
  52. variant="contained"
  53. sx={{
  54. mt: 3,
  55. mb: 2,
  56. backgroundColor: '#CBA213',
  57. height: 50,
  58. width: 150,
  59. textTransform: 'none',
  60. color: 'white',
  61. }}
  62. >
  63. Submit Details
  64. </Button>
  65. </Box>
  66. </Box>
  67. </Paper>
  68. );
  69. };
  70. export default ContactForm;