index.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { Component } from 'react';
  2. import styles from './index.less';
  3. import NewPortal from './NewPortal'
  4. import PropTypes from 'prop-types';
  5. import close from "../../images/close1.png";
  6. import icon1 from '../../images/icon.png'
  7. /***
  8. * 弹窗组件
  9. * author:zxc@2018-11-19
  10. * 接收参数:
  11. * visible: Boolean,对话框是否可见
  12. * title: 弹窗标题, String
  13. * titleBg: 标题背景颜色
  14. * children: 包含的子组件
  15. * noFooter: 是否包含底部,Boolean, 默认为false
  16. * confirm: 点击确认回调
  17. * close:点击遮罩层或右上角叉
  18. * cancel: 点击取消按钮的回调
  19. * closable: 是否显示右上角的关闭按钮, Boolean, 默认为true
  20. * mask:是否显示遮罩, Boolean, 默认为true
  21. * maskClosable: 点击蒙层是否允许关闭, Boolean, 默认为false
  22. * okText: 确认按钮文字,String,默认为确认
  23. * okColor: 确认按钮文字颜色
  24. * okBorderColor: 确认按钮边框颜色
  25. * oKBg: 确认按钮背景颜色
  26. * cancelText: 取消按钮文字, String,默认为取消
  27. * cancelColor: 取消按钮文字颜色
  28. * cancelBorderColor:取消按钮边框颜色
  29. * cancelBg: 取消按钮背景颜色
  30. * width: 宽度, string|number, 默认为300
  31. * height: 弹窗高度, string|number, 默认为200
  32. ***/
  33. const propTypes = {
  34. visible: PropTypes.bool,
  35. confirm: PropTypes.func,
  36. cancel: PropTypes.func,
  37. close: PropTypes.func,
  38. title: PropTypes.string,
  39. titleBg: PropTypes.string,
  40. noFooter: PropTypes.bool,
  41. okText: PropTypes.string,
  42. okType:PropTypes.string,
  43. cancelText: PropTypes.string,
  44. }
  45. const defaultProps = {
  46. titleBg: '#fff',
  47. noFooter: false,
  48. mask: true,
  49. maskClosable: true,
  50. closable: true,
  51. okText: '确认',
  52. okType:'normal', //normal蓝色,danger红色
  53. cancelText: '取消',
  54. width: '400px',
  55. height: '204px',
  56. };
  57. class ConfirmModal extends Component {
  58. constructor(props) {
  59. super(props);
  60. this.confirm = this.confirm.bind(this);
  61. this.cancel = this.cancel.bind(this);
  62. this.maskClick = this.maskClick.bind(this);
  63. this.closeModal = this.closeModal.bind(this)
  64. }
  65. closeModal() {
  66. const { close } = this.props
  67. close && close()
  68. }
  69. confirm() {
  70. const { confirm } = this.props
  71. confirm && confirm()
  72. }
  73. cancel() {
  74. const { cancel } = this.props
  75. cancel && cancel()
  76. }
  77. maskClick() {
  78. const { close } = this.props
  79. close && close()
  80. }
  81. render() {
  82. const {
  83. visible,
  84. title,
  85. titleBg,
  86. children,
  87. mask,
  88. maskClosable,
  89. okText,
  90. okType,
  91. cancelText,
  92. width,
  93. height,
  94. closable,
  95. noFooter,
  96. icon,
  97. } = this.props;
  98. const marginLeft = -parseInt(width)/2
  99. const marginTop = -parseInt(height)/2
  100. return (
  101. <NewPortal visible={visible}>
  102. <div className={styles['modal-wrapper']} id='confirm'>
  103. <div className={styles[['modal']]} style = {{width: width, marginLeft:marginLeft, minHeight:height, marginTop:marginTop}}>
  104. <div className={styles['modal-title']} style={{background: titleBg}}>
  105. {icon?<img src={icon} className={styles['icon']}/>:''}
  106. {title ? title : ''}
  107. {closable ? <img onClick={this.closeModal} className={styles['modal-close']} src = {close}/> : false}
  108. </div>
  109. <div className={styles['modal-content']}><img src={icon1}/>{children}</div>
  110. {noFooter ? '' : <div className={styles['modal-operator']+' clearfix'}>
  111. <div className={styles['modal-btn-box']}>
  112. <div
  113. onClick={this.confirm}
  114. className={styles['modal-operator-confirm']+" "+styles[okType]}>
  115. { okText}
  116. </div>
  117. </div>
  118. <div className={styles['modal-btn-box']}>
  119. <div
  120. onClick={this.cancel}
  121. className={styles['modal-operator-close']}>
  122. { cancelText}
  123. </div>
  124. </div>
  125. </div>}
  126. </div>
  127. {mask ? <div className={styles['mask']}
  128. onClick = {maskClosable === true ? this.maskClick : ()=>{}}
  129. ></div> : ''}
  130. </div>
  131. </NewPortal>
  132. )
  133. }
  134. }
  135. ConfirmModal.propTypes = propTypes;
  136. ConfirmModal.defaultProps = defaultProps;
  137. export default ConfirmModal;