Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ContactForm.tsx 2.2KB

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