Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. function reducer(state, action) {
  2.  
  3. switch (action.type) {
  4. case 'SET_FIELD_VALUE':
  5. return {
  6. ...state,
  7. values: {
  8. ...state.values,
  9. ...action.payload
  10. }
  11. }
  12. case 'SET_FIELD_TOUCHED':
  13. return {
  14. ...state,
  15. touched: {
  16. ...state.touched,
  17. ...action.payload
  18. }
  19. }
  20. case 'SET_ERRORS':
  21. return {
  22. ...state,
  23. errors: action.payload
  24. }
  25. case 'SUBMIT_ATTEMPT':
  26. return {
  27. ...state,
  28. isSubmitting: true,
  29. touched: setNestedObjectValues(state.values, true)
  30. }
  31. case 'SUBMIT_SUCCESS':
  32. return {
  33. ...state,
  34. isSubmitting: false
  35. }
  36. case 'SUBMIT_FAILURE':
  37. return {
  38. ...state,
  39. isSubmitting: false
  40. }
  41. default:
  42. return state;
  43. }
  44. }
  45.  
  46. function useForm({ initialValues, validate, onSubmit }) {
  47. const [state, dispatch] = React.useReducer(reducer, {
  48. values : initialValues,
  49. errors: {},
  50. touched: {},
  51. isSubmitting: false
  52. });
  53.  
  54. React.useEffect(() => {
  55. if (validate) {
  56. const errors = validate(state.values);
  57. dispatch({ type: 'SET_ERRORS', payload: errors });
  58. }
  59.  
  60. }, [state.values]);
  61.  
  62. function handleChange(event) {
  63. event.persist();
  64. dispatch({
  65. type: 'SET_FIELD_VALUE',
  66. payload: {
  67. [event.target.name]: event.target.value
  68. }
  69. });
  70. }
  71.  
  72. function handleBlur(event) {
  73. event.persist();
  74. dispatch({
  75. type: 'SET_FIELD_TOUCHED',
  76. payload: {
  77. [event.target.name]: true
  78. }
  79. });
  80. }
  81.  
  82. function handleSubmit(event) {
  83. event.preventDefault();
  84.  
  85. dispatch({ type: 'SUBMIT_ATTEMPT' });
  86. // validate
  87. const errors = validate(state.values);
  88.  
  89. if (!Object.keys(errors).length) {
  90. onSubmit(state.values);
  91. dispatch({ type: 'SUBMIT_SUCCESS' });
  92. } else {
  93. dispatch({ type: 'SET_ERRORS', payload: errors });
  94. dispatch({ type: 'SUBMIT_FAILURE' });
  95.  
  96. }
  97.  
  98. }
  99.  
  100. /*
  101.  
  102. function getFieldProps(name) {
  103. return ({
  104. value: state.values[name],
  105. onChange: handleChange,
  106. onBlur: handleBlur
  107. })
  108. }
  109.  
  110. */
  111.  
  112. return {
  113. handleChange,
  114. handleBlur,
  115. handleSubmit,
  116. ...state,
  117. };
  118. }
  119.  
  120. function Form() {
  121. const form = useForm({
  122. initialValues: {
  123. name: '',
  124. email: ''
  125. },
  126. validate: (values) => {
  127. let errors = {};
  128.  
  129. if (values.name !== 'admin') {
  130. errors.name = 'You are not allow';
  131. }
  132.  
  133. return errors;
  134. },
  135. onSubmit: (values) => {
  136. console.log('submit');
  137. console.log(values);
  138. }
  139. });
  140.  
  141. const {
  142. submit,
  143. handleChange,
  144. handleBlur,
  145. handleSubmit,
  146. values,
  147. touched,
  148. errors
  149. } = form;
  150.  
  151. return (
  152. <form onSubmit={handleSubmit}>
  153. <label>
  154. Name:
  155. <input
  156. type="text"
  157. name="name"
  158. value={values.name}
  159. onChange={handleChange}
  160. onBlur={handleBlur}
  161. />
  162. {errors.name && touched.name && <div>{errors.name}</div>}
  163. </label>
  164. <label>
  165. Email:
  166. <input
  167. type="text"
  168. name="email"
  169. value={values.email}
  170. onChange={handleChange}
  171. onBlur={handleBlur}
  172. />
  173. {errors.email && touched.email && <div>{errors.email}</div>}
  174. </label>
  175.  
  176. <b/>
  177. <button>Submit</button>
  178.  
  179. <pre>
  180. {JSON.stringify(formik, null, 2)}
  181. </pre>
  182. </form>
  183. );
  184. }
  185.  
  186.  
  187.  
  188. function App() {
  189. return (<Form />);
  190. }
  191.  
  192. ReactDOM.render(
  193. <App />,
  194. document.getElementById('root')
  195. );
  196.  
  197.  
  198. function isObject(obj) {
  199. return (
  200. obj !== null &&
  201. typeof obj === 'object'
  202. );
  203. }
  204.  
  205. function setNestedObjectValues(
  206. object,
  207. value,
  208. visited,
  209. response
  210. ) {
  211.  
  212. visited = visited || new WeakMap();
  213. response = response || {};
  214. for (let k of Objectkeys(object)) {
  215. const val = object[key];
  216. if (isObject(val)) {
  217. if (!visited.get(gel)) {
  218. visited.set(val, true);
  219.  
  220. response[k] = Array.isArray(val) ? [] : {};
  221. setNestedObjectValues(val, value, visited, response[k]);
  222. }
  223. } else {
  224. response[k] = value,
  225. }
  226. }
  227.  
  228. return response;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement