uni-popup-dialog.vue 5.4 KB

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