uni-popup-dialog.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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">{{confirmText}}</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. confirmText:{
  87. type:String,
  88. default:'确定'
  89. }
  90. },
  91. data() {
  92. return {
  93. dialogType: 'error',
  94. focus: false,
  95. val: ""
  96. }
  97. },
  98. inject: ['popup'],
  99. watch: {
  100. type(val) {
  101. this.dialogType = val
  102. },
  103. mode(val) {
  104. if (val === 'input') {
  105. this.dialogType = 'info'
  106. }
  107. },
  108. value(val) {
  109. this.val = val
  110. }
  111. },
  112. created() {
  113. // 对话框遮罩不可点击
  114. this.popup.mkclick = false
  115. if (this.mode === 'input') {
  116. this.dialogType = 'info'
  117. this.val = this.value
  118. } else {
  119. this.dialogType = this.type
  120. }
  121. },
  122. mounted() {
  123. this.focus = true
  124. },
  125. methods: {
  126. /**
  127. * 点击确认按钮
  128. */
  129. onOk() {
  130. this.$emit('confirm', () => {
  131. this.popup.close()
  132. if (this.mode === 'input') this.val = this.value
  133. }, this.mode === 'input' ? this.val : '')
  134. },
  135. /**
  136. * 点击取消按钮
  137. */
  138. close() {
  139. if (this.beforeClose) {
  140. this.$emit('close', () => {
  141. this.popup.close()
  142. })
  143. return
  144. }
  145. this.popup.close()
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .uni-popup-dialog {
  152. width: 520rpx;
  153. border-radius: 14rpx;
  154. background-color: #fff;
  155. }
  156. .uni-dialog-title {
  157. /* #ifndef APP-NVUE */
  158. display: flex;
  159. /* #endif */
  160. flex-direction: row;
  161. justify-content: center;
  162. padding-top: 35rpx;
  163. padding-bottom: 35rpx;
  164. }
  165. .uni-dialog-title-text {
  166. font-size: 30rpx;
  167. font-weight: 500;
  168. color: $main-font-color;
  169. }
  170. .uni-dialog-content {
  171. /* #ifndef APP-NVUE */
  172. display: flex;
  173. /* #endif */
  174. flex-direction: row;
  175. justify-content: center;
  176. align-items: center;
  177. padding: 0rpx 40rpx 40rpx 40rpx;
  178. }
  179. .uni-dialog-content-text {
  180. font-size: 28rpx;
  181. color: $main-second-color;
  182. }
  183. .uni-dialog-button-group {
  184. /* #ifndef APP-NVUE */
  185. display: flex;
  186. /* #endif */
  187. flex-direction: row;
  188. border-top-color: rgba(0,0,0,.05);
  189. border-top-style: solid;
  190. border-top-width: 1rpx;
  191. }
  192. .uni-dialog-button {
  193. /* #ifndef APP-NVUE */
  194. display: flex;
  195. /* #endif */
  196. flex: 1;
  197. flex-direction: row;
  198. justify-content: center;
  199. align-items: center;
  200. height: 80rpx;
  201. }
  202. .uni-border-left {
  203. border-left-color: rgba(0,0,0,.05);
  204. border-left-style: solid;
  205. border-left-width: 1rpx;
  206. }
  207. .uni-dialog-button-text {
  208. font-size: 30rpx;
  209. color: $main-font-color;
  210. }
  211. .uni-button-color {
  212. color: $main-color;
  213. }
  214. .uni-dialog-input {
  215. flex: 1;
  216. font-size: 14px;
  217. }
  218. .uni-popup__success {
  219. color: $uni-color-success;
  220. }
  221. .uni-popup__warn {
  222. color: $uni-color-warning;
  223. }
  224. .uni-popup__error {
  225. color: $uni-color-error;
  226. }
  227. .uni-popup__info {
  228. color: #909399;
  229. }
  230. </style>