uni-popup-dialog.vue 4.6 KB

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