uni-popup-dialog.vue 5.3 KB

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