uni-popup-dialog.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // #ifdef VUE2
  178. this.val = this.value;
  179. // #endif
  180. // #ifdef VUE3
  181. this.val = this.modelValue;
  182. // #endif
  183. } else {
  184. this.dialogType = this.type
  185. }
  186. },
  187. methods: {
  188. /**
  189. * 点击确认按钮
  190. */
  191. onOk() {
  192. if (this.mode === 'input') {
  193. this.$emit('confirm', this.val)
  194. } else {
  195. this.$emit('confirm')
  196. }
  197. if (this.beforeClose) return
  198. this.popup.close()
  199. },
  200. /**
  201. * 点击取消按钮
  202. */
  203. closeDialog() {
  204. this.$emit('close')
  205. if (this.beforeClose) return
  206. this.popup.close()
  207. },
  208. close() {
  209. this.popup.close()
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss">
  215. .uni-popup-dialog {
  216. width: 300px;
  217. border-radius: 11px;
  218. background-color: #fff;
  219. }
  220. .uni-dialog-title {
  221. /* #ifndef APP-NVUE */
  222. display: flex;
  223. /* #endif */
  224. flex-direction: row;
  225. justify-content: center;
  226. padding-top: 25px;
  227. }
  228. .uni-dialog-title-text {
  229. font-size: 16px;
  230. font-weight: 500;
  231. }
  232. .uni-dialog-content {
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. /* #endif */
  236. flex-direction: row;
  237. justify-content: center;
  238. align-items: center;
  239. padding: 20px;
  240. }
  241. .uni-dialog-content-text {
  242. font-size: 14px;
  243. color: #6C6C6C;
  244. }
  245. .uni-dialog-button-group {
  246. /* #ifndef APP-NVUE */
  247. display: flex;
  248. /* #endif */
  249. flex-direction: row;
  250. border-top-color: #f5f5f5;
  251. border-top-style: solid;
  252. border-top-width: 1px;
  253. }
  254. .uni-dialog-button {
  255. /* #ifndef APP-NVUE */
  256. display: flex;
  257. /* #endif */
  258. flex: 1;
  259. flex-direction: row;
  260. justify-content: center;
  261. align-items: center;
  262. height: 45px;
  263. }
  264. .uni-border-left {
  265. border-left-color: #f0f0f0;
  266. border-left-style: solid;
  267. border-left-width: 1px;
  268. }
  269. .uni-dialog-button-text {
  270. font-size: 16px;
  271. color: #333;
  272. }
  273. .uni-button-color {
  274. color: #007aff;
  275. }
  276. .uni-dialog-input {
  277. flex: 1;
  278. font-size: 14px;
  279. border: 1px #eee solid;
  280. height: 40px;
  281. padding: 0 10px;
  282. border-radius: 5px;
  283. color: #555;
  284. }
  285. .uni-popup__success {
  286. color: #4cd964;
  287. }
  288. .uni-popup__warn {
  289. color: #f0ad4e;
  290. }
  291. .uni-popup__error {
  292. color: #dd524d;
  293. }
  294. .uni-popup__info {
  295. color: #909399;
  296. }
  297. </style>