将Sweet Alert弹出窗口添加到React组件中的按钮

时间:2016-12-06 22:25:29

标签: javascript twitter-bootstrap reactjs sweetalert

我为Bootstrap和React找到了这个完美的Sweet Alert模块(我在Meteor应用程序中使用它):

http://djorg83.github.io/react-bootstrap-sweetalert/

但我不明白你如何在React组件中包含这些代码。

当有人点击我的应用中的“删除”按钮时,我想要一个Sweet Alert提示弹出要求确认。

以下是“删除”按钮的组件:

import React, {Component} from 'react';
import Goals from '/imports/collections/goals/goals.js'
import SweetAlert from 'react-bootstrap-sweetalert';

export default class DeleteGoalButton extends Component {

  deleteThisGoal(){
    console.log('Goal deleted!');
    // Meteor.call('goals.remove', this.props.goalId);
  }

  render(){
    return(
      <div className="inline">
          <a onClick={this.deleteThisGoal()} href={`/students/${this.props.studentId}/}`}
          className='btn btn-danger'><i className="fa fa-trash" aria-hidden="true"></i> Delete Goal</a>
      </div>
    )
  }
}

以下是我从Sweet Alert示例中复制的代码:

<SweetAlert
    warning
    showCancel
    confirmBtnText="Yes, delete it!"
    confirmBtnBsStyle="danger"
    cancelBtnBsStyle="default"
    title="Are you sure?"
    onConfirm={this.deleteFile}
    onCancel={this.cancelDelete}
>
    You will not be able to recover this imaginary file!
</SweetAlert>

任何人都知道怎么做?

2 个答案:

答案 0 :(得分:10)

基于您的代码http://www.webpackbin.com/VJTK2XgQM

的工作示例

您应该使用this.setState()并在<SweetAlert ... />上创建onClick。您可以使用胖箭头或.bind()或任何其他方法来确保使用正确的上下文。

import React, {Component} from 'react';
import SweetAlert from 'react-bootstrap-sweetalert';

export default class HelloWorld extends Component {

  constructor(props) {
    super(props);

    this.state = {
      alert: null
    };
  } 

  deleteThisGoal() {
    const getAlert = () => (
      <SweetAlert 
        success 
        title="Woot!" 
        onConfirm={() => this.hideAlert()}
      >
        Hello world!
      </SweetAlert>
    );

    this.setState({
      alert: getAlert()
    });
  }

  hideAlert() {
    console.log('Hiding alert...');
    this.setState({
      alert: null
    });
  }

  render() {
    return (
      <div style={{ padding: '20px' }}>
          <a 
            onClick={() => this.deleteThisGoal()}
            className='btn btn-danger'
          >
            <i className="fa fa-trash" aria-hidden="true"></i> Delete Goal
        </a>
        {this.state.alert}
      </div>
    );
  }
}

答案 1 :(得分:2)

如果它不像您公开@hinok解决方案那样对某人有效,那么您可以像这样修改此函数:

deleteThisGoal() {    
this.setState({
    alert: ( <
        SweetAlert success title = "Woot!"
        onConfirm = {
            () => this.hideAlert()
        } >
        Hello world!
        <
        /SweetAlert>
    )
});

};

这是我写的代码:

showAlert(title, message, callBack, style) {
    this.setState({
        alert: (
            <SweetAlert 
                warning
                showCancel
                confirmBtnText = "Sí"
                cancelBtnText = "No"
                confirmBtnBsStyle= {style ? style : "warning"}
                cancelBtnBsStyle = "default"
                customIcon = "thumbs-up.jpg"
                title = {title}
                onConfirm = {callBack()}
                onCancel = {this.hideAlert}
            >
                {message}
            </SweetAlert>
        )            
    });
}

hideAlert = () => {
    this.setState({
        alert: null
    });
}

updateCustomer = () => {..."a few lines of code here"}

这是从按钮调用的:

{<Button color="primary" disabled={this.state.notChange} onClick={() => this.showAlert('Save changes for client', '¿Are you sure?', () => this.updateCustomer, null) } >Save changes</Button>}

Saludos !!