uni-popup-dialog.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. this.$emit('confirm', () => {
  127. this.popup.close()
  128. if (this.mode === 'input') this.val = this.value
  129. }, this.mode === 'input' ? this.val : '')
  130. },
  131. /**
  132. * 点击取消按钮
  133. */
  134. close() {
  135. if (this.beforeClose) {
  136. this.$emit('close', () => {
  137. this.popup.close()
  138. })
  139. return
  140. }
  141. this.popup.close()
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .uni-popup-dialog {
  148. width: 300px;
  149. border-radius: 15px;
  150. background-color: #fff;
  151. }
  152. .uni-dialog-title {
  153. /* #ifndef APP-NVUE */
  154. display: flex;
  155. /* #endif */
  156. flex-direction: row;
  157. justify-content: center;
  158. padding-top: 15px;
  159. padding-bottom: 5px;
  160. }
  161. .uni-dialog-title-text {
  162. font-size: 16px;
  163. font-weight: 500;
  164. }
  165. .uni-dialog-content {
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. flex-direction: row;
  170. justify-content: center;
  171. align-items: center;
  172. padding: 5px 15px 15px 15px;
  173. }
  174. .uni-dialog-content-text {
  175. font-size: 14px;
  176. color: #6e6e6e;
  177. }
  178. .uni-dialog-button-group {
  179. /* #ifndef APP-NVUE */
  180. display: flex;
  181. /* #endif */
  182. flex-direction: row;
  183. border-top-color: #f5f5f5;
  184. border-top-style: solid;
  185. border-top-width: 1px;
  186. }
  187. .uni-dialog-button {
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. flex: 1;
  192. flex-direction: row;
  193. justify-content: center;
  194. align-items: center;
  195. height: 45px;
  196. }
  197. .uni-border-left {
  198. border-left-color: #f0f0f0;
  199. border-left-style: solid;
  200. border-left-width: 1px;
  201. }
  202. .uni-dialog-button-text {
  203. font-size: 14px;
  204. }
  205. .uni-button-color {
  206. color: $uni-color-primary;
  207. }
  208. .uni-dialog-input {
  209. flex: 1;
  210. font-size: 14px;
  211. }
  212. .uni-popup__success {
  213. color: $uni-color-success;
  214. }
  215. .uni-popup__warn {
  216. color: $uni-color-warning;
  217. }
  218. .uni-popup__error {
  219. color: $uni-color-error;
  220. }
  221. .uni-popup__info {
  222. color: #909399;
  223. }
  224. </style>