5

我正在使用 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编辑器上,我想调用组件的方法,submit组件以prop形式发送到,我这样显示,但我不能调用方法 实际上我想使用模态按钮来如果不可能,则触发 ProductForm 方法我将在 ProductForm 中使用提交按钮。ProductFormProductFormPopupForm{this.props.form}{this.props.form.submit()}

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

提前致谢。

4

1 回答 1

2

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

孩子:

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

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

于 2015-06-22T19:43:22.533 回答