5

I'm using react-bootstrap to launch a modal form.

To do that I created a modal component PopupForm, a form component ProductForm, a product component, in the product component I call

<ModalTrigger 
    modal={<PopupForm form={<ProductForm ref="pform" data={this.props.prod_data} />}/>}>
       <Button bsStyle='primary' bsSize='small' style={{marginRight:'5px'}} >
            <span className="glyphicon glyphicon-pencil" />
       </Button>
</ModalTrigger>

PopupForm:

var PopupForm = React.createClass({
    render: function(){
        return (
            <Modal {...this.props} bsStyle='primary' 
                   style={{width:200}} title='Edition' animation={false}>
            <div className='modal-body'>
                {this.props.form}
            </div>
            <div className='modal-footer'>
              <Button onClick={this.props.form.submit()}>Editer</Button>
              <Button onClick={this.props.onRequestHide}>Close</Button>
            </div>
          </Modal>
        )
    }
});

On this onClick Editer, I would like to call the method submit of the ProductForm component, the ProductForm component is send to PopupForm in the prop form, I display it like this {this.props.form}, but I can't call the method {this.props.form.submit()} Actually I'd like to use the modal button to trigger ProductForm method if it's not possible I will use a submit button inside the ProductForm.

Here is my ProductForm:

var ProductForm = React.createClass({

    componentDidMount: function() {
        this.props.submit = this.submit;
    },


    getInitialState: function () {
        return {canSubmit: false};
     },

    enableButton: function () {
      this.setState({
        canSubmit: true
      });
    },
    disableButton: function () {
      this.setState({
        canSubmit: false
      });
    },
    submit: function (model) {
      alert('ok');
    },
    render: function () {
      return (

            <Formsy.Form className="" name="test" onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>


              <CsInput value={this.props.data.name} 
                label="Nom" id="product_name" 
                name={this.props.data.name}
                validations={{matchRegexp: /^[A-Za-z0-9]{1,30}$/}} 
                validationError={validations_errors[1]} required/>



              {/*<button type="submit" disabled={!this.state.canSubmit}>Submit</button>*/}


            </Formsy.Form>
      );
    }
  });

Thanks in advance.

1 Answer 1

2
+25

if you have nested components you can call the other one's function like so:

Child:

var Component1 = React.createClass({
    render: function() {
        return (
            <div><button onClick={this.props.callback}>click me</button></div>
        )
    }
})

Parent:

var Component2 = React.createClass({
    doSomethingInParent: function() {
        console.log('I called from component 2');
    },
    render: function() {
        return (
            <div><component1 callback={this.doSomethingInParent} /></div>
        )
    }
})

Same goes in your case. It wasn't very clear in your code so I couldn't help you with the code Itself. If you get messy with this please post your entire code In a Hierarchically way so It'll be more readable.

1
  • This is the 'react way' of doing it. Anything which you need which is shared by multiple components (actions or data) should come from a common parent
    – undefined
    Commented Jun 27, 2015 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.