暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PersonalInfo.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { ErrorMessage, Field, Form, Formik } from "formik";
  2. import * as Yup from "yup";
  3. const initialValues = {
  4. fullName: "",
  5. email: "",
  6. address: "",
  7. city: "",
  8. country: "",
  9. postal: "",
  10. };
  11. const PersonalInfoSchema = Yup.object().shape({
  12. fullName: Yup.string().required("Full is required"),
  13. email: Yup.string().email("Email is invalid").required("Email is required"),
  14. address: Yup.string().required("Present Address is required"),
  15. city: Yup.string().required("City is required"),
  16. postal: Yup.string().required("Post code is required"),
  17. country: Yup.string().required("country is required"),
  18. });
  19. function PersonalInfo() {
  20. return (
  21. <>
  22. <Formik
  23. initialValues={initialValues}
  24. validationSchema={PersonalInfoSchema}
  25. onSubmit={(fields) => {
  26. alert(
  27. "SUCCESS!! :-)\n\n" + JSON.stringify(fields, null, 4)
  28. );
  29. }}
  30. >
  31. {({ errors, status, touched }) => (
  32. <Form>
  33. <div className="row">
  34. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  35. <label className="form-label">Full Name</label>
  36. <Field
  37. name="fullName"
  38. type="text"
  39. className={
  40. "form-control" +
  41. (errors.fullName && touched.fullName
  42. ? " is-invalid"
  43. : "")
  44. }
  45. />
  46. <ErrorMessage
  47. name="fullName"
  48. component="div"
  49. className="invalid-feedback"
  50. />
  51. </div>
  52. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  53. <label className="form-label">Email</label>
  54. <Field
  55. name="email"
  56. type="text"
  57. className={
  58. "form-control" +
  59. (errors.email && touched.email
  60. ? " is-invalid"
  61. : "")
  62. }
  63. />
  64. <ErrorMessage
  65. name="email"
  66. component="div"
  67. className="invalid-feedback"
  68. />
  69. </div>
  70. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  71. <label className="form-label">Address</label>
  72. <Field
  73. name="address"
  74. type="text"
  75. className={
  76. "form-control" +
  77. (errors.address && touched.address
  78. ? " is-invalid"
  79. : "")
  80. }
  81. />
  82. <ErrorMessage
  83. name="address"
  84. component="div"
  85. className="invalid-feedback"
  86. />
  87. </div>
  88. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  89. <label className="form-label">City</label>
  90. <Field
  91. name="city"
  92. type="text"
  93. className={
  94. "form-control" +
  95. (errors.city && touched.city
  96. ? " is-invalid"
  97. : "")
  98. }
  99. />
  100. <ErrorMessage
  101. name="city"
  102. component="div"
  103. className="invalid-feedback"
  104. />
  105. </div>
  106. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  107. <label className="form-label">Post Code</label>
  108. <Field
  109. name="postal"
  110. type="text"
  111. className={
  112. "form-control" +
  113. (errors.postal && touched.postal
  114. ? " is-invalid"
  115. : "")
  116. }
  117. />
  118. <ErrorMessage
  119. name="postal"
  120. component="div"
  121. className="invalid-feedback"
  122. />
  123. </div>
  124. <div className="col-xxl-6 col-xl-6 col-lg-6 mb-3">
  125. <label className="form-label">Country</label>
  126. <Field
  127. name="country"
  128. as="select"
  129. className={
  130. "form-control" +
  131. (errors.country && touched.country
  132. ? " is-invalid"
  133. : "")
  134. }
  135. >
  136. <option value="Bangladesh">
  137. Bangladesh
  138. </option>
  139. <option value="United States">
  140. United States
  141. </option>
  142. <option value="United Kingdom">
  143. United Kingdom
  144. </option>
  145. </Field>
  146. <ErrorMessage
  147. name="country"
  148. component="div"
  149. className="invalid-feedback"
  150. />
  151. </div>
  152. </div>
  153. <div className="mt-3">
  154. <button
  155. type="submit"
  156. className="btn btn-primary mr-2"
  157. >
  158. Save
  159. </button>
  160. </div>
  161. </Form>
  162. )}
  163. </Formik>
  164. </>
  165. );
  166. }
  167. export default PersonalInfo;