uni-popup-dialog.vue 5.1 KB

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