Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ShippingDetailsForm.jsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { Box, Button, Paper, TextField } from '@mui/material';
  2. import { useFormik } from 'formik';
  3. import PropType from 'prop-types';
  4. import { useState } from 'react';
  5. import { shippingDetailsSchema } from '../../../schemas/shippingDetailsSchema';
  6. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  7. const ShippingDetailsForm = ({ backBtn = false }) => {
  8. const [error] = useState({ hasError: false, errorMessage: '' });
  9. const submitHandler = async (values) => {
  10. console.log(values);
  11. };
  12. const formik = useFormik({
  13. initialValues: {
  14. fullName: '',
  15. email: '',
  16. address: '',
  17. address2: '',
  18. city: '',
  19. country: '',
  20. poostalCode: '',
  21. },
  22. validationSchema: shippingDetailsSchema,
  23. onSubmit: submitHandler,
  24. validateOnBlur: true,
  25. enableReinitialize: true,
  26. });
  27. return (
  28. <Paper
  29. sx={{ p: 3, width: '90%', ml: 12, backgroundColor: '#f2f2f2' }}
  30. elevation={3}
  31. >
  32. <Box
  33. sx={{
  34. width: '100%',
  35. display: 'flex',
  36. flexDirection: 'column',
  37. }}
  38. >
  39. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  40. <Box
  41. component="form"
  42. onSubmit={formik.handleSubmit}
  43. sx={{ position: 'relative', mt: 1, p: 1 }}
  44. >
  45. <TextField
  46. name="email"
  47. label="Email"
  48. margin="normal"
  49. value={formik.values.email}
  50. onChange={formik.handleChange}
  51. error={formik.touched.email && Boolean(formik.errors.email)}
  52. helperText={formik.touched.email && formik.errors.email}
  53. autoFocus
  54. fullWidth
  55. />
  56. <TextField
  57. name="fullName"
  58. label="Name"
  59. margin="normal"
  60. value={formik.values.fullName}
  61. onChange={formik.handleChange}
  62. error={formik.touched.fullName && Boolean(formik.errors.fullName)}
  63. helperText={formik.touched.fullName && formik.errors.fullName}
  64. fullWidth
  65. />
  66. <TextField
  67. name="address"
  68. label="Address"
  69. margin="normal"
  70. value={formik.values.address}
  71. onChange={formik.handleChange}
  72. error={formik.touched.address && Boolean(formik.errors.address)}
  73. helperText={formik.touched.address && formik.errors.address}
  74. fullWidth
  75. />
  76. <TextField
  77. name="address2"
  78. label="Address Line 2"
  79. margin="normal"
  80. value={formik.values.address2}
  81. onChange={formik.handleChange}
  82. error={formik.touched.address2 && Boolean(formik.errors.address2)}
  83. helperText={formik.touched.address2 && formik.errors.address2}
  84. fullWidth
  85. />
  86. <TextField
  87. name="city"
  88. label="City"
  89. margin="normal"
  90. value={formik.values.city}
  91. onChange={formik.handleChange}
  92. error={formik.touched.city && Boolean(formik.errors.city)}
  93. helperText={formik.touched.city && formik.errors.city}
  94. fullWidth
  95. />
  96. <Box sx={{ display: 'flex' }}>
  97. <TextField
  98. name="country"
  99. label="Country"
  100. margin="normal"
  101. value={formik.values.country}
  102. onChange={formik.handleChange}
  103. error={formik.touched.country && Boolean(formik.errors.country)}
  104. helperText={formik.touched.country && formik.errors.country}
  105. fullWidth
  106. sx={{ mr: 1.5 }}
  107. />
  108. <TextField
  109. name="city"
  110. label="City"
  111. margin="normal"
  112. value={formik.values.city}
  113. onChange={formik.handleChange}
  114. error={formik.touched.city && Boolean(formik.errors.city)}
  115. helperText={formik.touched.city && formik.errors.city}
  116. fullWidth
  117. />
  118. </Box>
  119. {backBtn && (
  120. <Button
  121. variant="contained"
  122. sx={{
  123. mt: 3,
  124. mb: 2,
  125. height: 50,
  126. width: 150,
  127. textTransform: 'none',
  128. backgroundColor: 'primary.main',
  129. color: 'white',
  130. mr: 2,
  131. }}
  132. >
  133. Back to cart
  134. </Button>
  135. )}
  136. <Button
  137. type="submit"
  138. variant="contained"
  139. sx={{
  140. mt: 3,
  141. mb: 2,
  142. backgroundColor: '#CBA213',
  143. height: 50,
  144. width: 150,
  145. textTransform: 'none',
  146. color: 'white',
  147. }}
  148. >
  149. Submit Details
  150. </Button>
  151. </Box>
  152. </Box>
  153. </Paper>
  154. );
  155. };
  156. ShippingDetailsForm.propTypes = {
  157. backBtn: PropType.Boolean,
  158. };
  159. export default ShippingDetailsForm;