Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import * as React from 'react'
  2. import {formValidationConfigList} from './constants.FormValidations'
  3.  
  4. const ACTION_TYPES = {
  5. SET_FORM_VALUE: 'SET_FORM_VALUE',
  6. }
  7.  
  8. const reducer = (state, action) => {
  9. switch(action.type) {
  10. case ACTION_TYPES.SET_FORM_VALUE:
  11. return {
  12. ...state,
  13. modalData: action.modalData,
  14. }
  15. default:
  16. return state
  17. }
  18. }
  19.  
  20. const initialState = {
  21. modalData: {}
  22. }
  23.  
  24. const FormValidation = (props) => {
  25. const [state, dispatch] = React.useReducer(reducer, initialState)
  26.  
  27. const {modalData} = state
  28.  
  29. const handleChange = (value, apiName) => {
  30. modalData[apiName] = value
  31. dispatch({
  32. type: ACTION_TYPES.SET_FORM_VALUE,
  33. modalData,
  34. })
  35. }
  36.  
  37. return (
  38. <div className="form-validation">
  39. {
  40. formValidationConfigList.map((eachConfig, index) => {
  41. return (
  42. <React.Fragment key={index}>
  43. <label for={eachConfig.apiName}>{eachConfig.label}</label>
  44. <input
  45. type={eachConfig.dataType}
  46. id={eachConfig.id}
  47. required={eachConfig.required}
  48. value={modalData[eachConfig.apiName]}
  49. onChange={(e) => handleChange(e.target.value, eachConfig.apiName)}
  50. />
  51. {
  52. eachConfig.hasError && (
  53. <div className="error-message>
  54. {
  55. eachConfig.errorMessage
  56. }
  57. </div>
  58. )
  59. }
  60. <br/>
  61. </React.Fragment>
  62. )
  63. })
  64. }
  65. </div>
  66. )
  67. }
  68.  
  69. export default FormValidation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement