Manual Reference Source

basic/PopupConfirm.js

  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { Popup, Button, Header } from 'semantic-ui-react';
  4.  
  5. export default class PopupConfirm extends Component {
  6. constructor(props, context) {
  7. super(props, context);
  8.  
  9. this.state = {
  10. showPopup: false,
  11. canConfirm: ''
  12. };
  13. }
  14.  
  15. static propTypes = {
  16. trigger: PropTypes.any,
  17. content: PropTypes.string,
  18. onCancel: PropTypes.func,
  19. onConfirm: PropTypes.func,
  20. onCanConfirm: PropTypes.func
  21. };
  22.  
  23. static defaultProps = {
  24. onCancel: () => {},
  25. onConfirm: () => {},
  26. onCanConfirm: () => {}
  27. };
  28.  
  29. openPopup() {
  30. this.setState({ canConfirm: this.props.onCanConfirm(), showPopup: true });
  31. }
  32.  
  33. closePopup() {
  34. this.setState({ showPopup: false });
  35. }
  36.  
  37. handleCancel() {
  38. this.closePopup();
  39. this.props.onCancel();
  40. }
  41.  
  42. handleConfirm() {
  43. this.closePopup();
  44. this.props.onConfirm();
  45. }
  46.  
  47. render() {
  48. return (
  49. <Popup
  50. trigger={this.props.trigger}
  51. on="click"
  52. wide="very"
  53. hideOnScroll
  54. open={this.state.showPopup}
  55. onOpen={this.openPopup.bind(this)}
  56. onClose={this.closePopup.bind(this)}
  57. >
  58. <Header>{this.state.canConfirm ? this.state.canConfirm : this.props.content}</Header>
  59.  
  60. {this.state.canConfirm ? (
  61. <div className="rightFloated">
  62. <Button icon="checkmark" content="Ok" color="green" onClick={this.handleCancel.bind(this)} />
  63. </div>
  64. ) : (
  65. <div className="rightFloated">
  66. <Button icon="remove" content="Cancel" basic onClick={this.handleCancel.bind(this)} />
  67. <Button icon="checkmark" content="Ok" color="green" onClick={this.handleConfirm.bind(this)} />
  68. </div>
  69. )}
  70. </Popup>
  71. );
  72. }
  73. }