|
| 1 | +import classNames from 'classnames' |
| 2 | +import ModalAction from './action/index' |
| 3 | +import ModalContent from './content/index' |
| 4 | +import ModalHeader from './header/index' |
| 5 | +// import Button from '../button/index' |
| 6 | +import { getEnvs } from '../../utils/common' |
| 7 | + |
| 8 | +export default { |
| 9 | + name: 'Modal', |
| 10 | + props: { |
| 11 | + title: { |
| 12 | + type: String, |
| 13 | + default: '', |
| 14 | + }, |
| 15 | + isOpened: { |
| 16 | + type: Boolean, |
| 17 | + default: false, |
| 18 | + }, |
| 19 | + closeOnClickOverlay: { |
| 20 | + type: Boolean, |
| 21 | + default: false, |
| 22 | + }, |
| 23 | + onCancel: { |
| 24 | + type: Function, |
| 25 | + default: () => () => {}, |
| 26 | + }, |
| 27 | + onConfirm: { |
| 28 | + type: Function, |
| 29 | + default: () => () => {}, |
| 30 | + }, |
| 31 | + onClose: { |
| 32 | + type: Function, |
| 33 | + default: () => () => {}, |
| 34 | + }, |
| 35 | + content: { |
| 36 | + type: String, |
| 37 | + default: '', |
| 38 | + }, |
| 39 | + cancelText: { |
| 40 | + type: String, |
| 41 | + default: '', |
| 42 | + }, |
| 43 | + confirmText: { |
| 44 | + type: String, |
| 45 | + default: '', |
| 46 | + }, |
| 47 | + }, |
| 48 | + data() { |
| 49 | + return { |
| 50 | + state: { |
| 51 | + ...getEnvs(), |
| 52 | + _isOpened: this.isOpened, |
| 53 | + }, |
| 54 | + } |
| 55 | + }, |
| 56 | + watch: { |
| 57 | + isOpened(val) { |
| 58 | + if (val !== this.state._isOpened) { |
| 59 | + this.setState({ |
| 60 | + _isOpened: val, |
| 61 | + }) |
| 62 | + } |
| 63 | + }, |
| 64 | + }, |
| 65 | + methods: { |
| 66 | + setState(newState) { |
| 67 | + const ks = Object.keys(newState) |
| 68 | + if (Array.isArray(ks)) { |
| 69 | + ks.forEach((k) => { |
| 70 | + if (k in this.state) { |
| 71 | + this.state[k] = newState[k] |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | + }, |
| 76 | + handleClickOverlay() { |
| 77 | + if (this.closeOnClickOverlay) { |
| 78 | + this.setState( |
| 79 | + { |
| 80 | + _isOpened: false, |
| 81 | + }, |
| 82 | + this.handleClose |
| 83 | + ) |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | + handleClose(event) { |
| 88 | + if (typeof this.onClose === 'function') { |
| 89 | + this.onClose(event) |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + handleCancel(event) { |
| 94 | + if (typeof this.onCancel === 'function') { |
| 95 | + this.onCancel(event) |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + handleConfirm(event) { |
| 100 | + if (typeof this.onConfirm === 'function') { |
| 101 | + this.onConfirm(event) |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + handleTouchMove(e) { |
| 106 | + e.stopPropagation() |
| 107 | + }, |
| 108 | + }, |
| 109 | + render() { |
| 110 | + const { _isOpened, isWEB } = this.state |
| 111 | + const { title, content, cancelText, confirmText } = this |
| 112 | + const rootClass = classNames( |
| 113 | + 'at-modal', |
| 114 | + { |
| 115 | + 'at-modal--active': _isOpened, |
| 116 | + }, |
| 117 | + this.className |
| 118 | + ) |
| 119 | + console.log('rootClass', rootClass) |
| 120 | + if (title || content) { |
| 121 | + const isRenderAction = cancelText || confirmText |
| 122 | + return ( |
| 123 | + <view class={rootClass}> |
| 124 | + <view onTap={this.handleClickOverlay} class="at-modal__overlay" /> |
| 125 | + <view class="at-modal__container"> |
| 126 | + {title && ( |
| 127 | + <ModalHeader> |
| 128 | + <view>{title}</view> |
| 129 | + </ModalHeader> |
| 130 | + )} |
| 131 | + {content && ( |
| 132 | + <ModalContent> |
| 133 | + <view class="content-simple"> |
| 134 | + {isWEB ? ( |
| 135 | + <view |
| 136 | + // @ts-ignore |
| 137 | + dangerouslySetInnerHTML={{ |
| 138 | + __html: content.replace(/\n/g, '<br/>'), |
| 139 | + }}></view> |
| 140 | + ) : ( |
| 141 | + <view>{content}</view> |
| 142 | + )} |
| 143 | + </view> |
| 144 | + </ModalContent> |
| 145 | + )} |
| 146 | + {isRenderAction && ( |
| 147 | + <ModalAction isSimple> |
| 148 | + {cancelText && <button onTap={this.handleCancel}>{cancelText}</button>} |
| 149 | + {confirmText && <button onTap={this.handleConfirm}>{confirmText}</button>} |
| 150 | + </ModalAction> |
| 151 | + )} |
| 152 | + </view> |
| 153 | + </view> |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + return ( |
| 158 | + <view onTouchMove={this.handleTouchMove} class={rootClass}> |
| 159 | + <view class="at-modal__overlay" onTap={this.handleClickOverlay} /> |
| 160 | + <view class="at-modal__container">{this.$slots.default}</view> |
| 161 | + </view> |
| 162 | + ) |
| 163 | + }, |
| 164 | +} |
0 commit comments