Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import React from 'react';
  2. import { Field, reduxForm } from 'redux-form';
  3.  
  4.  
  5. function validate(values) {
  6. const errors = {}
  7. if (!values.firstName) {
  8. errors.firstName = 'Required'
  9. }
  10. if (!values.lastName) {
  11. errors.lastName = 'Required'
  12. }
  13. if (!values.email) {
  14. errors.email = 'Required'
  15. }
  16. return errors
  17. }
  18.  
  19. const renderField = ({
  20. input,
  21. label,
  22. type,
  23. meta: { touched, error }
  24. }) => (
  25. <div>
  26. <label>{label}</label>
  27. <div>
  28. <input {...input} placeholder={label} type={type} />
  29. {touched && error && <span>{error}</span>}
  30. </div>
  31. </div>
  32. )
  33.  
  34. const SimpleForm = props => {
  35. const { handleSubmit, pristine, reset, submitting } = props;
  36. return (
  37. <form onSubmit={handleSubmit}>
  38. <div>
  39. <label>First Name</label>
  40. <div>
  41. <Field
  42. name="firstName"
  43. component={renderField}
  44. type="text"
  45. />
  46. </div>
  47. </div>
  48. <div>
  49. <label>Last Name</label>
  50. <div>
  51. <Field
  52. name="lastName"
  53. component={renderField}
  54. type="text"
  55. />
  56. </div>
  57. </div>
  58. <div>
  59. <label>Email</label>
  60. <div>
  61. <Field
  62. name="email"
  63. component={renderField}
  64. type="email"
  65. />
  66. </div>
  67. </div>
  68. <div>
  69. <label>Sex</label>
  70. <div>
  71. <label>
  72. <Field name="sex" component="input" type="radio" value="male" />
  73. {' '}
  74. Male
  75. </label>
  76. <label>
  77. <Field name="sex" component="input" type="radio" value="female" />
  78. {' '}
  79. Female
  80. </label>
  81. </div>
  82. </div>
  83. <div>
  84. <label>Favorite Color</label>
  85. <div>
  86. <Field name="favoriteColor" component="select">
  87. <option />
  88. <option value="ff0000">Red</option>
  89. <option value="00ff00">Green</option>
  90. <option value="0000ff">Blue</option>
  91. </Field>
  92. </div>
  93. </div>
  94. <div>
  95. <label htmlFor="employed">Employed</label>
  96. <div>
  97. <Field
  98. name="employed"
  99. id="employed"
  100. component="input"
  101. type="checkbox"
  102. />
  103. </div>
  104. </div>
  105. <div>
  106. <label>Notes</label>
  107. <div>
  108. <Field name="notes" component="textarea" />
  109. </div>
  110. </div>
  111. <div>
  112. <button type="submit" disabled={pristine || submitting}>Submit</button>
  113. <button type="button" disabled={pristine || submitting} onClick={reset}>
  114. Clear Values
  115. </button>
  116. </div>
  117. </form>
  118. );
  119. };
  120.  
  121. export default reduxForm({
  122. form: 'simple', // a unique identifier for this form
  123. validate
  124. })(SimpleForm);
Add Comment
Please, Sign In to add comment