uni-popup-dialog.vue 6.1 KB

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