Next.js template
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ContactForm.jsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {
  2. Box,
  3. Button,
  4. Container,
  5. Grid,
  6. TextField,
  7. Typography
  8. } from '@mui/material';
  9. import { useFormik } from 'formik';
  10. import { useTranslation } from 'next-i18next';
  11. import Link from 'next/link';
  12. import React from 'react';
  13. import { BASE_PAGE } from '../../../constants/pages';
  14. import { contactSchema } from '../../../schemas/contactSchema';
  15. const ContactForm = () => {
  16. const { t } = useTranslation('forms', 'contact', 'common');
  17. const handleSubmit = (values) => {
  18. console.log('Values', values);
  19. };
  20. const formik = useFormik({
  21. initialValues: {
  22. firstName: '',
  23. lastName: '',
  24. email: '',
  25. message: ''
  26. },
  27. validationSchema: contactSchema,
  28. onSubmit: handleSubmit,
  29. validateOnBlur: true,
  30. enableReinitialize: true,
  31. });
  32. return (
  33. <Container component="main" maxWidth="md">
  34. <Box
  35. sx={{
  36. marginTop: 32,
  37. display: 'flex',
  38. flexDirection: 'column',
  39. alignItems: 'center',
  40. }}
  41. >
  42. <Typography component="h1" variant="h5">
  43. {t('contact:Title')}
  44. </Typography>
  45. <Box
  46. component="form"
  47. onSubmit={formik.handleSubmit}
  48. sx={{ position: 'relative', mt: 1, p: 1 }}
  49. >
  50. <TextField
  51. name="firstName"
  52. label={t('forms:FirstName')}
  53. margin="normal"
  54. value={formik.values.firstName}
  55. onChange={formik.handleChange}
  56. error={formik.touched.firstName && Boolean(formik.errors.firstName)}
  57. helperText={formik.touched.firstName && formik.errors.firstName}
  58. autoFocus
  59. fullWidth
  60. />
  61. <TextField
  62. name="lastName"
  63. label={t('forms:LastName')}
  64. margin="normal"
  65. value={formik.values.lastName}
  66. onChange={formik.handleChange}
  67. error={formik.touched.lastName && Boolean(formik.errors.lastName)}
  68. helperText={formik.touched.lastName && formik.errors.lastName}
  69. autoFocus
  70. fullWidth
  71. />
  72. <TextField
  73. name="email"
  74. label={t('forms:Email')}
  75. margin="normal"
  76. value={formik.values.email}
  77. onChange={formik.handleChange}
  78. error={formik.touched.email && Boolean(formik.errors.email)}
  79. helperText={formik.touched.email && formik.errors.email}
  80. autoFocus
  81. fullWidth
  82. />
  83. <TextField
  84. name="message"
  85. label={t('forms:Message')}
  86. multiline
  87. margin="normal"
  88. value={formik.values.message}
  89. onChange={formik.handleChange}
  90. error={formik.touched.message && Boolean(formik.errors.message)}
  91. helperText={formik.touched.message && formik.errors.message}
  92. rows={4}
  93. autoFocus
  94. fullWidth
  95. />
  96. <Button
  97. type="submit"
  98. variant="contained"
  99. sx={{ mt: 3, mb: 2 }}
  100. fullWidth
  101. >
  102. {t('contact:SendBtn')}
  103. </Button>
  104. <Grid container justifyContent="center">
  105. <Link href={BASE_PAGE}>{t('common:Back')}</Link>
  106. </Grid>
  107. </Box>
  108. </Box>
  109. </Container>
  110. );
  111. };
  112. export default ContactForm;