爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
6.3 KiB

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