/* @flow */
import React from 'react'

import createErrorComponent from './createErrorComponent'
import createValidation from '../createValidation'
import {
  assoc,
  assocPath,
  curry,
  map,
  prop,
} from 'ramda'

// default ErrorComponent
const DefaultErrorComponent = ({errorMsg}) => <div className='error'>{errorMsg}</div>

function Revalidation(
  initialState,
  validationRules,
  errorComponent,
  Component
) {

  const validate = createValidation(createErrorComponent(errorComponent || DefaultErrorComponent))

  return class extends React.Component {

    state: {
      form: Object,
      errors: Array<any>,
    }

    onChange: Function

    constructor(props) {
      super(props)
      this.state = initialState
      this.onChange = curry((name, value) =>
        this.setState(state => {
          const newState = assocPath(['form', name], value, state)
          const errors = validate(prop('form', newState), validationRules)
          return assoc('errors', errors, newState)
        })
      )
    }

    render() {
      const {form, errors} = this.state

      return (
        <Component
          {...this.props}
          form={form}
          errors={errors}
          onChange={this.onChange}
        />
      )
    }
  }
}

export default curry(Revalidation)

@@ -32,16 +32,55 @@
 react'%0A%0A
+import isValid from '../utils/isValid'%0A
 import c
@@ -129,16 +129,16 @@
 ponent'%0A
-
 import c
@@ -224,22 +224,14 @@
 ry,%0A
-  map,%0A
   prop
-,
 %0A%7D f
@@ -748,73 +748,151 @@
 e = 
-initialState%0A      this.onChange = curry((name, value) =%3E
+%7Bform: props.form %7C%7C initialState, errors: %7B%7D%7D%0A      this.onChange = this.onChange.bind(this)%0A    %7D%0A%0A    onChange(name) %7B
 %0A      
- 
+return value =%3E
  thi
@@ -925,19 +925,21 @@
     
-  
 const 
-new
+updated
 Stat
@@ -986,26 +986,24 @@
 te)%0A        
-  
 const errors
@@ -1027,19 +1027,23 @@
 'form', 
-new
+updated
 State), 
@@ -1063,26 +1063,24 @@
 es)%0A        
-  
 return assoc
@@ -1102,11 +1102,15 @@
 rs, 
-new
+updated
 Stat
@@ -1122,19 +1122,107 @@
     
-  %7D)%0A      
+%7D)%0A    %7D%0A%0A    validate(form) %7B%0A      this.setState(state =%3E (%7B ...state, ...this.validation(form)%7D)
 )%0A  
@@ -1280,16 +1280,77 @@
 is.state
+%0A      const valid = isValid(validate(form, validationRules))
 %0A%0A      
@@ -1461,23 +1461,47 @@
         
-onChang
+valid=%7Bvalid%7D%0A          validat
 e=%7Bthis.

Add valid prop to React Higher Order Component