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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With