uni-popup-dialog.vue 5.1 KB

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