gpt4 book ai didi

forms - React-bootstrap 外部组件调用方法

转载 作者:行者123 更新时间:2023-12-03 13:37:50 25 4
gpt4 key购买 nike

我正在使用react-bootstrap来启动模式表单。

为此,我在我称为的产品组件中创建了一个模态组件 PopupForm、一个表单组件 ProductForm、一个产品组件

<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>
)
}
});

在此 onClick 编辑器上,我想调用 ProductForm 组件的 submit 方法,即 ProductForm > 组件发送到 prop 表单中的 PopupForm ,我像这样显示它 {this.props.form},但我无法调用方法 { this.props.form.submit()}实际上,我想使用模式按钮来触发 ProductForm 方法,如果不可能,我将在 ProductForm 内使用提交按钮。

这是我的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>
);
}
});

提前致谢。

最佳答案

如果您有嵌套组件,您可以像这样调用另一个组件的函数:

child :

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

父级:

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

你的情况也是如此。您的代码中不是很清楚,因此我无法帮助您处理代码本身。如果您对此感到困惑,请以分层方式发布您的整个代码,以便它更具可读性。

关于forms - React-bootstrap 外部组件调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com