index.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { Component } from 'react';
  2. import styles from './index.less';
  3. import OldPortal from './OldPortal';
  4. import NewPortal from './NewPortal'
  5. import PropTypes from 'prop-types';
  6. import close from "./img/close.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. okColor: PropTypes.string,
  43. okBorderColor: PropTypes.string,
  44. oKBg: PropTypes.string,
  45. cancelText: PropTypes.string,
  46. cancelColor: PropTypes.string,
  47. cancelBorderColor: PropTypes.string,
  48. cancelBg: PropTypes.string,
  49. }
  50. const defaultProps = {
  51. titleBg: '#fff',
  52. noFooter: false,
  53. mask: true,
  54. maskClosable: true,
  55. closable: true,
  56. okText: '确认',
  57. okColor: 'red',
  58. okBorderColor: 'red',
  59. oKBg:'#fff',
  60. cancelText: '取消',
  61. cancelColor: '#414141',
  62. cancelBorderColor: '#414141',
  63. cancelBg: '#fff',
  64. width: '300px',
  65. height: '200px',
  66. };
  67. class ConfirmModal extends Component {
  68. constructor(props) {
  69. super(props);
  70. this.confirm = this.confirm.bind(this);
  71. this.cancel = this.cancel.bind(this);
  72. this.maskClick = this.maskClick.bind(this);
  73. this.closeModal = this.closeModal.bind(this)
  74. }
  75. closeModal() {
  76. const { close } = this.props
  77. close && close()
  78. }
  79. confirm() {
  80. const { confirm } = this.props
  81. confirm && confirm()
  82. }
  83. cancel() {
  84. const { cancel } = this.props
  85. cancel && cancel()
  86. }
  87. maskClick() {
  88. const { close } = this.props
  89. close && close()
  90. }
  91. render() {
  92. const {
  93. visible,
  94. title,
  95. titleBg,
  96. children,
  97. mask,
  98. maskClosable,
  99. okText,
  100. okColor,
  101. okBorderColor,
  102. oKBg,
  103. cancelText,
  104. cancelColor,
  105. cancelBorderColor,
  106. cancelBg,
  107. width,
  108. height,
  109. closable,
  110. noFooter,
  111. icon
  112. } = this.props;
  113. const marginLeft = -parseInt(width)/2
  114. const marginTop = -parseInt(height)/2
  115. return (
  116. <NewPortal visible={visible}>
  117. <div className={styles['modal-wrapper']} id='confirm'>
  118. <div className={styles[['modal']]} style = {{width: width, marginLeft:marginLeft, minHeight:height, marginTop:marginTop}}>
  119. <div className={styles['modal-title']} style={{background: titleBg}}>
  120. {icon?<img src={icon} className={styles['icon']}/>:''}
  121. {title ? title : ''}
  122. {closable ? <img onClick={this.closeModal} className={styles['modal-close']} src = {close}/> : false}
  123. </div>
  124. <div className={styles['modal-content']}>{children}</div>
  125. {noFooter ? '' : <div className={styles['modal-operator']+' clearfix'}>
  126. <div className={styles['modal-btn-box']}>
  127. <div
  128. onClick={this.confirm}
  129. className={styles['modal-operator-confirm']}
  130. style={{borderColor: okBorderColor, background: oKBg, color: okColor}}>
  131. { okText}
  132. </div>
  133. </div>
  134. <div className={styles['modal-btn-box']}>
  135. <div
  136. onClick={this.cancel}
  137. className={styles['modal-operator-close']}
  138. style={{borderColor: cancelBorderColor, background: cancelBg, color: cancelColor}}>
  139. { cancelText}
  140. </div>
  141. </div>
  142. </div>}
  143. </div>
  144. {mask ? <div className={styles['mask']}
  145. onClick = {maskClosable === true ? this.maskClick : ()=>{}}
  146. ></div> : ''}
  147. </div>
  148. </NewPortal>
  149. )
  150. }
  151. }
  152. ConfirmModal.propTypes = propTypes;
  153. ConfirmModal.defaultProps = defaultProps;
  154. export default ConfirmModal;