uni-popup-confirm.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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" :maxlength="maxlength" 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. maxlength:{
  48. type: Number,
  49. default: 10
  50. },
  51. placeholder: {
  52. type: [String, Number],
  53. default: '请输入内容'
  54. },
  55. /**
  56. * 对话框主题 success/warning/info/error 默认 success
  57. */
  58. type: {
  59. type: String,
  60. default: 'error'
  61. },
  62. /**
  63. * 对话框模式 base/input
  64. */
  65. mode: {
  66. type: String,
  67. default: 'base'
  68. },
  69. /**
  70. * 对话框标题
  71. */
  72. title: {
  73. type: String,
  74. default: '提示'
  75. },
  76. /**
  77. * 对话框内容
  78. */
  79. content: {
  80. type: String,
  81. default: ''
  82. },
  83. /**
  84. * 拦截取消事件 ,如果拦截取消事件,必须监听close事件,执行 done()
  85. */
  86. beforeClose: {
  87. type: Boolean,
  88. default: false
  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') {
  133. this.val = this.value;
  134. return this.val;
  135. }
  136. }, this.mode === 'input' ? this.val : '')
  137. },
  138. /**
  139. * 点击取消按钮
  140. */
  141. close() {
  142. if (this.beforeClose) {
  143. this.$emit('close', () => {
  144. this.popup.close()
  145. })
  146. return
  147. }
  148. this.popup.close()
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .uni-popup-dialog {
  155. width: 300px;
  156. border-radius: 5px;
  157. background-color: #fff;
  158. }
  159. .uni-dialog-title {
  160. /* #ifndef APP-NVUE */
  161. display: flex;
  162. /* #endif */
  163. flex-direction: row;
  164. justify-content: flex-start;
  165. padding: 20px 0 10px 15px;
  166. }
  167. .uni-dialog-title-text {
  168. font-size: 16px;
  169. font-weight: 500;
  170. }
  171. .uni-dialog-content {
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. flex-direction: row;
  176. justify-content: center;
  177. align-items: center;
  178. padding: 5px 15px 15px 15px;
  179. }
  180. .uni-dialog-content-text {
  181. font-size: 14px;
  182. color: #6e6e6e;
  183. }
  184. .uni-dialog-button-group {
  185. /* #ifndef APP-NVUE */
  186. display: flex;
  187. /* #endif */
  188. flex-direction: row;
  189. border-top-color: #bdbdbd;
  190. border-top-style: solid;
  191. border-top-width: 1px;
  192. margin-top:5px;
  193. }
  194. .uni-dialog-button {
  195. /* #ifndef APP-NVUE */
  196. display: flex;
  197. /* #endif */
  198. flex: 1;
  199. flex-direction: row;
  200. justify-content: center;
  201. align-items: center;
  202. height: 55px;
  203. }
  204. .uni-border-left {
  205. border-left-color: #bdbdbd;
  206. border-left-style: solid;
  207. border-left-width: 1px;
  208. }
  209. .uni-dialog-button-text {
  210. font-size: 14px;
  211. }
  212. .uni-button-color {
  213. }
  214. .uni-dialog-input {
  215. flex: 1;
  216. font-size: 14px;
  217. overflow: visible !important;
  218. line-height :1.9rem !important;
  219. height:1.9rem !important;
  220. border-radius: 10px;
  221. padding-left:2px;
  222. background-color: #eeeeee;
  223. }
  224. .uni-popup__success {
  225. color: $uni-color-success;
  226. }
  227. .uni-popup__warn {
  228. color: $uni-color-warning;
  229. }
  230. .uni-popup__error {
  231. color: $uni-color-error;
  232. }
  233. .uni-popup__info {
  234. color: #909399;
  235. }
  236. </style>