uni-popup-dialog.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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" type="text" :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. },
  134. methods: {
  135. /**
  136. * 点击确认按钮
  137. */
  138. onOk() {
  139. if (this.mode === 'input'){
  140. this.$emit('confirm', this.val)
  141. }else{
  142. this.$emit('confirm')
  143. }
  144. if(this.beforeClose) return
  145. this.popup.close()
  146. },
  147. /**
  148. * 点击取消按钮
  149. */
  150. closeDialog() {
  151. this.$emit('close')
  152. if(this.beforeClose) return
  153. this.popup.close()
  154. },
  155. close(){
  156. this.popup.close()
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="scss" >
  162. .uni-popup-dialog {
  163. width: 300px;
  164. border-radius: 11px;
  165. background-color: #fff;
  166. }
  167. .uni-dialog-title {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. flex-direction: row;
  172. justify-content: center;
  173. padding-top: 25px;
  174. }
  175. .uni-dialog-title-text {
  176. font-size: 16px;
  177. font-weight: 500;
  178. }
  179. .uni-dialog-content {
  180. /* #ifndef APP-NVUE */
  181. display: flex;
  182. /* #endif */
  183. flex-direction: row;
  184. justify-content: center;
  185. align-items: center;
  186. padding: 20px;
  187. }
  188. .uni-dialog-content-text {
  189. font-size: 14px;
  190. color: #6C6C6C;
  191. }
  192. .uni-dialog-button-group {
  193. /* #ifndef APP-NVUE */
  194. display: flex;
  195. /* #endif */
  196. flex-direction: row;
  197. border-top-color: #f5f5f5;
  198. border-top-style: solid;
  199. border-top-width: 1px;
  200. }
  201. .uni-dialog-button {
  202. /* #ifndef APP-NVUE */
  203. display: flex;
  204. /* #endif */
  205. flex: 1;
  206. flex-direction: row;
  207. justify-content: center;
  208. align-items: center;
  209. height: 45px;
  210. }
  211. .uni-border-left {
  212. border-left-color: #f0f0f0;
  213. border-left-style: solid;
  214. border-left-width: 1px;
  215. }
  216. .uni-dialog-button-text {
  217. font-size: 16px;
  218. color: #333;
  219. }
  220. .uni-button-color {
  221. color: #007aff;
  222. }
  223. .uni-dialog-input {
  224. flex: 1;
  225. font-size: 14px;
  226. border: 1px #eee solid;
  227. height: 40px;
  228. padding: 0 10px;
  229. border-radius: 5px;
  230. color: #555;
  231. }
  232. .uni-popup__success {
  233. color: #4cd964;
  234. }
  235. .uni-popup__warn {
  236. color: #f0ad4e;
  237. }
  238. .uni-popup__error {
  239. color: #dd524d;
  240. }
  241. .uni-popup__info {
  242. color: #909399;
  243. }
  244. </style>