Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import React from 'react';
  2. import { withFormik } from 'formik';
  3.  
  4. const MyForm = props => {
  5. const {
  6. values,
  7. touched,
  8. errors,
  9. handleChange,
  10. handleBlur,
  11. handleSubmit,
  12. } = props;
  13. return (
  14. <form onSubmit={handleSubmit}>
  15. <input
  16. type="text"
  17. onChange={handleChange}
  18. onBlur={handleBlur}
  19. value={values.name}
  20. name="name"
  21. />
  22. {errors.name && touched.name && <div id="feedback">{errors.name}</div>}
  23. <button type="submit">Submit</button>
  24. </form>
  25. );
  26. };
  27.  
  28. const MyEnhancedForm = withFormik({
  29. mapPropsToValues: () => ({ name: '' }),
  30.  
  31. // Custom sync validation
  32. validate: values => {
  33. const errors = {};
  34.  
  35. if (!values.name) {
  36. errors.name = 'Required';
  37. }
  38.  
  39. return errors;
  40. },
  41.  
  42. handleSubmit: (values, { setSubmitting }) => {
  43. setTimeout(() => {
  44. alert(JSON.stringify(values, null, 2));
  45. setSubmitting(false);
  46. }, 1000);
  47. },
  48.  
  49. displayName: 'BasicForm',
  50. })(MyForm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement