uni-popup-dialog.vue 4.8 KB

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