ReduxFormWrap.js 1.7 KB
import React,{PropTypes} from 'react';   
import {connect} from 'react-redux';
import { Field, reduxForm,formValueSelector,getFormValues,destroy,submit} from 'redux-form';
import cx from 'classnames';


class FormWrap extends React.Component {
  constructor(props) {
    super(props);
    this.state={
    }
  }
  static propTypes = { 
  } 
  componentDidMount() {
  }
  componentWillReceiveProps(nextProps){
  }
  componentWillUnmount(){
  }
  render(){ 
    return(
      <div className={cx('xxxyy')}>
         {this.props.children}
      </div>
    )
  }
}






class ReduxFormWrap extends React.Component { 
 
    constructor(props) { 
      super(props);  
      this.state = { 
        component: null,
      }; 
    } 
    static propTypes = {
        form: PropTypes.string,  
        onSubmit: PropTypes.func,
        initialValues: PropTypes.object 
    }
    static defaultProps={ 
        form:'@@redux-form-wrap',    
        onSubmit   :(values)=>{},
        initialValues:{} 
    }
    componentDidMount() {   
      const { form,initialValues,onSubmit} = this.props;
      let MyFormWrap = reduxForm(this.props)(FormWrap);
      this.setState({ 
        component: MyFormWrap 
      }); 
    } 
    componentWillReceiveProps(nextProps){  
      const { form,initialValues,onSubmit } = nextProps;
      const C = this.state.component; 
      if(!C){
        let MyFormWrap = reduxForm(nextProps)(FormWrap);
        this.setState({ 
          component: MyFormWrap 
        }); 
      } 
    }   
    render() { 
      const C = this.state.component;  
      return C 
        ? <C {...this.props}>
          {this.props.children}
        </C>
        : null; 
    } 
 
  } 
 
export default ReduxFormWrap;