uni-popup-dialog.vue 5.7 KB

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