uni-popup-dialog.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <view class="uni-popup-dialog">
  3. <view class="uni-dialog-title">
  4. <text
  5. class="uni-dialog-title-text"
  6. :class="['uni-popup__' + dialogType]"
  7. >{{ titleText }}</text
  8. >
  9. </view>
  10. <view v-if="mode === 'base'" class="uni-dialog-content">
  11. <slot>
  12. <text class="uni-dialog-content-text">{{ content }}</text>
  13. </slot>
  14. </view>
  15. <view v-else class="uni-dialog-content">
  16. <slot>
  17. <input
  18. class="uni-dialog-input"
  19. v-model="val"
  20. type="text"
  21. :placeholder="placeholderText"
  22. :focus="focus"
  23. />
  24. </slot>
  25. </view>
  26. <view class="uni-dialog-button-group">
  27. <view class="uni-dialog-button" @click="closeDialog">
  28. <text class="uni-dialog-button-text">{{ closeText }}</text>
  29. </view>
  30. <view class="uni-dialog-button uni-border-left" @click="onOk">
  31. <text class="uni-dialog-button-text uni-button-color">{{
  32. okText
  33. }}</text>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import popup from '../uni-popup/popup.js'
  40. import { initVueI18n } from '@dcloudio/uni-i18n'
  41. import messages from '../uni-popup/i18n/index.js'
  42. const { t } = initVueI18n(messages)
  43. /**
  44. * PopUp 弹出层-对话框样式
  45. * @description 弹出层-对话框样式
  46. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  47. * @property {String} value input 模式下的默认值
  48. * @property {String} placeholder input 模式下输入提示
  49. * @property {String} type = [success|warning|info|error] 主题样式
  50. * @value success 成功
  51. * @value warning 提示
  52. * @value info 消息
  53. * @value error 错误
  54. * @property {String} mode = [base|input] 模式、
  55. * @value base 基础对话框
  56. * @value input 可输入对话框
  57. * @property {String} content 对话框内容
  58. * @property {Boolean} beforeClose 是否拦截取消事件
  59. * @event {Function} confirm 点击确认按钮触发
  60. * @event {Function} close 点击取消按钮触发
  61. */
  62. export default {
  63. name: 'uniPopupDialog',
  64. mixins: [popup],
  65. emits: ['confirm', 'close'],
  66. props: {
  67. value: {
  68. type: [String, Number],
  69. default: ''
  70. },
  71. placeholder: {
  72. type: [String, Number],
  73. default: ''
  74. },
  75. type: {
  76. type: String,
  77. default: 'error'
  78. },
  79. mode: {
  80. type: String,
  81. default: 'base'
  82. },
  83. title: {
  84. type: String,
  85. default: ''
  86. },
  87. content: {
  88. type: String,
  89. default: ''
  90. },
  91. beforeClose: {
  92. type: Boolean,
  93. default: false
  94. },
  95. cancelText: {
  96. type: String,
  97. default: ''
  98. },
  99. confirmText: {
  100. type: String,
  101. default: ''
  102. }
  103. },
  104. data () {
  105. return {
  106. dialogType: 'error',
  107. focus: false,
  108. val: ''
  109. }
  110. },
  111. computed: {
  112. okText () {
  113. return this.confirmText || t('uni-popup.ok')
  114. },
  115. closeText () {
  116. return this.cancelText || t('uni-popup.cancel')
  117. },
  118. placeholderText () {
  119. return this.placeholder || t('uni-popup.placeholder')
  120. },
  121. titleText () {
  122. return this.title || t('uni-popup.title')
  123. }
  124. },
  125. watch: {
  126. type (val) {
  127. this.dialogType = val
  128. },
  129. mode (val) {
  130. if (val === 'input') {
  131. this.dialogType = 'info'
  132. }
  133. },
  134. value (val) {
  135. this.val = val
  136. }
  137. },
  138. created () {
  139. // 对话框遮罩不可点击
  140. this.popup.disableMask()
  141. // this.popup.closeMask()
  142. if (this.mode === 'input') {
  143. this.dialogType = 'info'
  144. this.val = this.value
  145. } else {
  146. this.dialogType = this.type
  147. }
  148. },
  149. mounted () {
  150. this.focus = true
  151. },
  152. methods: {
  153. /**
  154. * 点击确认按钮
  155. */
  156. onOk () {
  157. if (this.mode === 'input') {
  158. this.$emit('confirm', this.val)
  159. } else {
  160. this.$emit('confirm')
  161. }
  162. if (this.beforeClose) return
  163. this.popup.close()
  164. },
  165. /**
  166. * 点击取消按钮
  167. */
  168. closeDialog () {
  169. this.$emit('close')
  170. if (this.beforeClose) return
  171. this.popup.close()
  172. },
  173. close () {
  174. this.popup.close()
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss">
  180. .uni-popup-dialog {
  181. width: 300px;
  182. border-radius: 11px;
  183. background-color: #fff;
  184. }
  185. .uni-dialog-title {
  186. /* #ifndef APP-NVUE */
  187. display: flex;
  188. /* #endif */
  189. flex-direction: row;
  190. justify-content: center;
  191. padding-top: 25px;
  192. }
  193. .uni-dialog-title-text {
  194. font-size: 16px;
  195. font-weight: 500;
  196. }
  197. .uni-dialog-content {
  198. /* #ifndef APP-NVUE */
  199. display: flex;
  200. /* #endif */
  201. flex-direction: row;
  202. justify-content: center;
  203. align-items: center;
  204. padding: 20px;
  205. }
  206. .uni-dialog-content-text {
  207. font-size: 32rpx;
  208. color: #6c6c6c;
  209. }
  210. .uni-dialog-button-group {
  211. /* #ifndef APP-NVUE */
  212. display: flex;
  213. /* #endif */
  214. flex-direction: row;
  215. border-top-color: #f5f5f5;
  216. border-top-style: solid;
  217. border-top-width: 1px;
  218. }
  219. .uni-dialog-button {
  220. /* #ifndef APP-NVUE */
  221. display: flex;
  222. /* #endif */
  223. flex: 1;
  224. flex-direction: row;
  225. justify-content: center;
  226. align-items: center;
  227. height: 45px;
  228. }
  229. .uni-border-left {
  230. border-left-color: #f0f0f0;
  231. border-left-style: solid;
  232. border-left-width: 1px;
  233. }
  234. .uni-dialog-button-text {
  235. font-size: 16px;
  236. color: #333;
  237. }
  238. .uni-button-color {
  239. color: #007aff;
  240. }
  241. .uni-dialog-input {
  242. flex: 1;
  243. font-size: 32rpx;
  244. border: 1px #eee solid;
  245. height: 40px;
  246. padding: 0 10px;
  247. border-radius: 5px;
  248. color: #555;
  249. }
  250. .uni-popup__success {
  251. color: #4cd964;
  252. }
  253. .uni-popup__warn {
  254. color: #f0ad4e;
  255. }
  256. .uni-popup__error {
  257. color: #dd524d;
  258. }
  259. .uni-popup__info {
  260. color: #909399;
  261. }
  262. </style>