uni-popup-dialog.vue 5.2 KB

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