Tôi đang sử dụng phản ứng-bootstrap để khởi chạy biểu mẫu phương thức.Phương pháp gọi phản hồi-bootstrap của thành phần bên ngoài
Để làm được điều mà tôi tạo ra một thành phần modal PopupForm
, một thành phần hình thức ProductForm
, một thành phần sản phẩm, trong thành phần sản phẩm mà tôi gọi
<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 onClick
editer này, Tôi muốn gọi phương thức submit
của thành phần ProductForm
, thành phần ProductForm
được gửi đến PopupForm
trong hình thức prop, tôi hiển thị nó như thế này {this.props.form}
, nhưng tôi không thể gọi phương thức {this.props.form.submit()}
Thực ra tôi muốn sử dụng nút phương thức để kích hoạt phương thức ProductForm nếu không, tôi sẽ sử dụng nút gửi bên trong ProductForm.
Đây là tôi 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>
);
}
});
Cảm ơn trước.
Đây là 'cách phản ứng' khi thực hiện. Bất cứ điều gì bạn cần được chia sẻ bởi nhiều thành phần (hành động hoặc dữ liệu) nên đến từ một phụ huynh phổ biến –