uni-popup-alert.vue 4.4 KB

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