uni-popup-dialog.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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]">
  5. {{ title }}
  6. </text>
  7. </view>
  8. <view class="uni-dialog-content">
  9. <template v-if="!$slots.default && !$slots.$default">
  10. <text v-if="mode === 'base'" class="uni-dialog-content-text">
  11. {{ content }}
  12. </text>
  13. <input
  14. v-else
  15. v-model="val"
  16. class="uni-dialog-input"
  17. type="text"
  18. placeholder-class="wk-placeholder"
  19. :placeholder="placeholder"
  20. :focus="focus"
  21. @input="inputChange">
  22. <view v-if="requiredError" class="required-error">
  23. {{ requiredText }}
  24. </view>
  25. </template>
  26. <slot v-else />
  27. </view>
  28. <view class="uni-dialog-button-group">
  29. <view v-if="!hidenCancel" class="uni-dialog-button" @click="close">
  30. <text class="uni-dialog-button-text">
  31. {{ cancelText }}
  32. </text>
  33. </view>
  34. <view :class="{'uni-border-left': !hidenCancel}" class="uni-dialog-button" @click="onOk">
  35. <text class="uni-dialog-button-text uni-button-color">
  36. {{ confirmText }}
  37. </text>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. /**
  44. * PopUp 弹出层-对话框样式
  45. * Modified By yxk
  46. * @description 弹出层-对话框样式
  47. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  48. * @property {String} value input 模式下的默认值
  49. * @property {String} placeholder input 模式下输入提示
  50. * @property {String} type = [success|warning|info|error] 主题样式
  51. * @value success 成功
  52. * @value warning 提示
  53. * @value info 消息
  54. * @value error 错误
  55. * @property {String} mode = [base|input] 模式、
  56. * @value base 基础对话框
  57. * @value input 可输入对话框
  58. * @property {String} content 对话框内容
  59. * @property {Boolean} beforeClose 是否拦截取消事件
  60. * @property {Boolean} hidenCancel 是否隐藏取消按钮
  61. *
  62. * @property {String} cancelText 取消按钮文本
  63. * @property {String} confirmText 确认按钮文本
  64. * @property {Boolean} required input模式时是否必填
  65. * @property {String} requiredText input模式时必填提示信息
  66. * @property {Function} checkFunc input模式时校验方法,必须返回一个Boolean值
  67. *
  68. * @event {Function} confirm 点击确认按钮触发
  69. * @event {Function} close 点击取消按钮触发
  70. */
  71. export default {
  72. name: 'UniPopupDialog',
  73. inject: ['popup'],
  74. props: {
  75. value: {
  76. type: [String, Number],
  77. default: ''
  78. },
  79. placeholder: {
  80. type: [String, Number],
  81. default: '请输入内容'
  82. },
  83. /**
  84. * 对话框主题 success/warning/info/error 默认 success
  85. */
  86. type: {
  87. type: String,
  88. default: 'error'
  89. },
  90. /**
  91. * 对话框模式 base/input
  92. */
  93. mode: {
  94. type: String,
  95. default: 'base'
  96. },
  97. /**
  98. * 对话框标题
  99. */
  100. title: {
  101. type: String,
  102. default: '提示'
  103. },
  104. /**
  105. * 对话框内容
  106. */
  107. content: {
  108. type: String,
  109. default: ''
  110. },
  111. /**
  112. * 拦截取消事件 ,如果拦截取消事件,必须监听close事件,执行 done()
  113. */
  114. beforeClose: {
  115. type: Boolean,
  116. default: false
  117. },
  118. hidenCancel: {
  119. type: Boolean,
  120. default: false
  121. },
  122. cancelText: {
  123. type: String,
  124. default: '取消'
  125. },
  126. confirmText: {
  127. type: String,
  128. default: '确定'
  129. },
  130. required: {
  131. type: Boolean,
  132. default: false
  133. },
  134. checkFunc: {
  135. type: Function,
  136. default: null
  137. },
  138. requiredText: {
  139. type: String,
  140. default: '内容不能为空'
  141. }
  142. },
  143. data() {
  144. return {
  145. dialogType: 'error',
  146. focus: false,
  147. val: '',
  148. requiredError: false
  149. }
  150. },
  151. watch: {
  152. type(val) {
  153. this.dialogType = val
  154. },
  155. mode(val) {
  156. if (val === 'input') {
  157. this.dialogType = 'info'
  158. }
  159. },
  160. value(val) {
  161. this.val = val
  162. }
  163. },
  164. created() {
  165. // 对话框遮罩不可点击
  166. this.popup.mkclick = false
  167. if (this.mode === 'input') {
  168. this.dialogType = 'info'
  169. this.val = this.value
  170. } else {
  171. this.dialogType = this.type
  172. }
  173. },
  174. mounted() {
  175. this.focus = true
  176. },
  177. methods: {
  178. inputChange() {
  179. if (this.val) {
  180. this.requiredError = false
  181. }
  182. },
  183. /**
  184. * 点击确认按钮
  185. */
  186. onOk() {
  187. if (
  188. this.mode === 'input' &&
  189. this.required &&
  190. !this.val
  191. ) {
  192. this.requiredError = true
  193. return
  194. }
  195. if (this.checkFunc) {
  196. this.requiredError = !this.checkFunc(this.val)
  197. if (this.requiredError) return
  198. }
  199. this.$emit('confirm', () => {
  200. this.popup.close()
  201. if (this.mode === 'input') this.val = this.value
  202. }, this.val)
  203. },
  204. /**
  205. * 点击取消按钮
  206. */
  207. close() {
  208. if (this.beforeClose) {
  209. this.$emit('close', () => {
  210. this.popup.close()
  211. })
  212. return
  213. }
  214. this.popup.close()
  215. }
  216. }
  217. }
  218. </script>
  219. <style scoped>
  220. .uni-popup-dialog {
  221. width: 300px;
  222. border-radius: 15px;
  223. background-color: #fff;
  224. }
  225. .uni-dialog-title {
  226. /* #ifndef APP-NVUE */
  227. display: flex;
  228. /* #endif */
  229. flex-direction: row;
  230. justify-content: center;
  231. padding-top: 15px;
  232. padding-bottom: 5px;
  233. }
  234. .uni-dialog-title-text {
  235. font-size: 16px;
  236. font-weight: 500;
  237. }
  238. .uni-dialog-content {
  239. /* #ifndef APP-NVUE */
  240. display: flex;
  241. /* #endif */
  242. flex-direction: column;
  243. justify-content: center;
  244. align-items: center;
  245. padding: 5px 15px 15px 15px;
  246. }
  247. .uni-dialog-content-text {
  248. width: 100%;
  249. font-size: 14px;
  250. color: #6e6e6e;
  251. }
  252. .required-error {
  253. width: 100%;
  254. font-size: 12px;
  255. color: #FF6767;
  256. padding-top: 8px;
  257. }
  258. .uni-dialog-button-group {
  259. /* #ifndef APP-NVUE */
  260. display: flex;
  261. /* #endif */
  262. flex-direction: row;
  263. border-top-color: #f5f5f5;
  264. border-top-style: solid;
  265. border-top-width: 1px;
  266. }
  267. .uni-dialog-button {
  268. /* #ifndef APP-NVUE */
  269. display: flex;
  270. /* #endif */
  271. flex: 1;
  272. flex-direction: row;
  273. justify-content: center;
  274. align-items: center;
  275. height: 45px;
  276. }
  277. .uni-border-left {
  278. border-left-color: #f0f0f0;
  279. border-left-style: solid;
  280. border-left-width: 1px;
  281. }
  282. .uni-dialog-button-text {
  283. font-size: 14px;
  284. }
  285. .uni-button-color {
  286. color: #007aff;
  287. }
  288. .uni-dialog-input {
  289. width: 100%;
  290. flex: 1;
  291. font-size: 14px;
  292. }
  293. .uni-popup__success {
  294. color: #4cd964;
  295. }
  296. .uni-popup__warn {
  297. color: #f0ad4e;
  298. }
  299. .uni-popup__error {
  300. color: #dd524d;
  301. }
  302. .uni-popup__info {
  303. color: #909399;
  304. }
  305. </style>