uni-popup-dialog.vue 5.5 KB

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