uni-popup-dialog.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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">{{closeText}}</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. cancelText:{
  86. type: String,
  87. default: ''
  88. },
  89. confirmText:{
  90. type: String,
  91. default: ''
  92. }
  93. },
  94. data() {
  95. return {
  96. dialogType: 'error',
  97. focus: false,
  98. val: ""
  99. }
  100. },
  101. computed: {
  102. okText() {
  103. return this.confirmText || t("uni-popup.ok")
  104. },
  105. closeText() {
  106. return this.cancelText || t("uni-popup.cancel")
  107. },
  108. placeholderText() {
  109. return this.placeholder || t("uni-popup.placeholder")
  110. },
  111. titleText() {
  112. return this.title || t("uni-popup.title")
  113. }
  114. },
  115. watch: {
  116. type(val) {
  117. this.dialogType = val
  118. },
  119. mode(val) {
  120. if (val === 'input') {
  121. this.dialogType = 'info'
  122. }
  123. },
  124. value(val) {
  125. this.val = val
  126. }
  127. },
  128. created() {
  129. // 对话框遮罩不可点击
  130. this.popup.disableMask()
  131. // this.popup.closeMask()
  132. if (this.mode === 'input') {
  133. this.dialogType = 'info'
  134. this.val = this.value
  135. } else {
  136. this.dialogType = this.type
  137. }
  138. },
  139. mounted() {
  140. this.focus = true
  141. },
  142. methods: {
  143. /**
  144. * 点击确认按钮
  145. */
  146. onOk() {
  147. if (this.mode === 'input'){
  148. this.$emit('confirm', this.val)
  149. }else{
  150. this.$emit('confirm')
  151. }
  152. if(this.beforeClose) return
  153. this.popup.close()
  154. },
  155. /**
  156. * 点击取消按钮
  157. */
  158. closeDialog() {
  159. this.$emit('close')
  160. if(this.beforeClose) return
  161. this.popup.close()
  162. },
  163. close(){
  164. this.popup.close()
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" >
  170. .uni-popup-dialog {
  171. width: 300px;
  172. border-radius: 11px;
  173. background-color: #fff;
  174. }
  175. .uni-dialog-title {
  176. /* #ifndef APP-NVUE */
  177. display: flex;
  178. /* #endif */
  179. flex-direction: row;
  180. justify-content: center;
  181. padding-top: 25px;
  182. }
  183. .uni-dialog-title-text {
  184. font-size: 16px;
  185. font-weight: 500;
  186. }
  187. .uni-dialog-content {
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. flex-direction: row;
  192. justify-content: center;
  193. align-items: center;
  194. padding: 20px;
  195. }
  196. .uni-dialog-content-text {
  197. font-size: 14px;
  198. color: #6C6C6C;
  199. }
  200. .uni-dialog-button-group {
  201. /* #ifndef APP-NVUE */
  202. display: flex;
  203. /* #endif */
  204. flex-direction: row;
  205. border-top-color: #f5f5f5;
  206. border-top-style: solid;
  207. border-top-width: 1px;
  208. }
  209. .uni-dialog-button {
  210. /* #ifndef APP-NVUE */
  211. display: flex;
  212. /* #endif */
  213. flex: 1;
  214. flex-direction: row;
  215. justify-content: center;
  216. align-items: center;
  217. height: 45px;
  218. }
  219. .uni-border-left {
  220. border-left-color: #f0f0f0;
  221. border-left-style: solid;
  222. border-left-width: 1px;
  223. }
  224. .uni-dialog-button-text {
  225. font-size: 16px;
  226. color: #333;
  227. }
  228. .uni-button-color {
  229. color: #007aff;
  230. }
  231. .uni-dialog-input {
  232. flex: 1;
  233. font-size: 14px;
  234. border: 1px #eee solid;
  235. height: 40px;
  236. padding: 0 10px;
  237. border-radius: 5px;
  238. color: #555;
  239. }
  240. .uni-popup__success {
  241. color: #4cd964;
  242. }
  243. .uni-popup__warn {
  244. color: #f0ad4e;
  245. }
  246. .uni-popup__error {
  247. color: #dd524d;
  248. }
  249. .uni-popup__info {
  250. color: #909399;
  251. }
  252. </style>