su-dialog.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <su-popup :show="show" type="center" @close="closeDialog">
  3. <view class="uni-popup-dialog">
  4. <view class="uni-dialog-title">
  5. <text class="uni-dialog-title-text" :class="['uni-popup__' + dialogType]">
  6. {{ titleText }}
  7. </text>
  8. </view>
  9. <view v-if="mode === 'base'" class="uni-dialog-content">
  10. <slot>
  11. <text class="uni-dialog-content-text">{{ content }}</text>
  12. </slot>
  13. </view>
  14. <view v-else class="uni-dialog-content">
  15. <slot>
  16. <input
  17. class="uni-dialog-input"
  18. v-model="val"
  19. type="text"
  20. :placeholder="placeholderText"
  21. :focus="focus"
  22. />
  23. </slot>
  24. </view>
  25. <view class="uni-dialog-button-group">
  26. <view class="uni-dialog-button" @click="closeDialog">
  27. <text class="uni-dialog-button-text">{{ closeText }}</text>
  28. </view>
  29. <view class="uni-dialog-button uni-border-left" @click="onOk">
  30. <text class="uni-dialog-button-text uni-button-color">{{ okText }}</text>
  31. </view>
  32. </view>
  33. </view>
  34. </su-popup>
  35. </template>
  36. <script>
  37. /**
  38. * PopUp 弹出层-对话框样式
  39. * @description 弹出层-对话框样式
  40. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  41. * @property {String} value input 模式下的默认值
  42. * @property {String} placeholder input 模式下输入提示
  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. * @property {String} content 对话框内容
  52. * @property {Boolean} beforeClose 是否拦截取消事件
  53. * @event {Function} confirm 点击确认按钮触发
  54. * @event {Function} close 点击取消按钮触发
  55. */
  56. export default {
  57. name: 'SuDialog',
  58. emits: ['confirm', 'close'],
  59. props: {
  60. show: {
  61. type: Boolean,
  62. default: false,
  63. },
  64. value: {
  65. type: [String, Number],
  66. default: '',
  67. },
  68. placeholder: {
  69. type: [String, Number],
  70. default: '',
  71. },
  72. type: {
  73. type: String,
  74. default: 'error',
  75. },
  76. mode: {
  77. type: String,
  78. default: 'base',
  79. },
  80. title: {
  81. type: String,
  82. default: '',
  83. },
  84. content: {
  85. type: String,
  86. default: '',
  87. },
  88. beforeClose: {
  89. type: Boolean,
  90. default: false,
  91. },
  92. cancelText: {
  93. type: String,
  94. default: '',
  95. },
  96. confirmText: {
  97. type: String,
  98. default: '',
  99. },
  100. },
  101. data() {
  102. return {
  103. dialogType: 'error',
  104. focus: false,
  105. val: '',
  106. };
  107. },
  108. computed: {
  109. okText() {
  110. return this.confirmText || '确认';
  111. },
  112. closeText() {
  113. return this.cancelText || '取消';
  114. },
  115. placeholderText() {
  116. return this.placeholder || '';
  117. },
  118. titleText() {
  119. return this.title || '';
  120. },
  121. },
  122. watch: {
  123. type(val) {
  124. this.dialogType = val;
  125. },
  126. mode(val) {
  127. if (val === 'input') {
  128. this.dialogType = 'info';
  129. }
  130. },
  131. value(val) {
  132. this.val = val;
  133. },
  134. },
  135. created() {
  136. if (this.mode === 'input') {
  137. this.dialogType = 'info';
  138. this.val = this.value;
  139. } else {
  140. this.dialogType = this.type;
  141. }
  142. },
  143. mounted() {
  144. this.focus = true;
  145. },
  146. methods: {
  147. /**
  148. * 点击确认按钮
  149. */
  150. onOk() {
  151. if (this.mode === 'input') {
  152. this.$emit('confirm', this.val);
  153. } else {
  154. this.$emit('confirm');
  155. }
  156. if (this.beforeClose) return;
  157. },
  158. /**
  159. * 点击取消按钮
  160. */
  161. closeDialog() {
  162. this.$emit('close');
  163. if (this.beforeClose) return;
  164. },
  165. },
  166. };
  167. </script>
  168. <style lang="scss">
  169. .uni-popup-dialog {
  170. width: 300px;
  171. border-radius: 11px;
  172. background-color: #fff;
  173. }
  174. .uni-dialog-title {
  175. /* #ifndef APP-NVUE */
  176. display: flex;
  177. /* #endif */
  178. flex-direction: row;
  179. justify-content: center;
  180. padding-top: 25px;
  181. }
  182. .uni-dialog-title-text {
  183. font-size: 16px;
  184. font-weight: 500;
  185. }
  186. .uni-dialog-content {
  187. /* #ifndef APP-NVUE */
  188. display: flex;
  189. /* #endif */
  190. flex-direction: row;
  191. justify-content: center;
  192. align-items: center;
  193. padding: 20px;
  194. }
  195. .uni-dialog-content-text {
  196. font-size: 14px;
  197. color: #6c6c6c;
  198. }
  199. .uni-dialog-button-group {
  200. /* #ifndef APP-NVUE */
  201. display: flex;
  202. /* #endif */
  203. flex-direction: row;
  204. border-top-color: #f5f5f5;
  205. border-top-style: solid;
  206. border-top-width: 1px;
  207. }
  208. .uni-dialog-button {
  209. /* #ifndef APP-NVUE */
  210. display: flex;
  211. /* #endif */
  212. flex: 1;
  213. flex-direction: row;
  214. justify-content: center;
  215. align-items: center;
  216. height: 45px;
  217. }
  218. .uni-border-left {
  219. border-left-color: #f0f0f0;
  220. border-left-style: solid;
  221. border-left-width: 1px;
  222. }
  223. .uni-dialog-button-text {
  224. font-size: 16px;
  225. color: #333;
  226. }
  227. .uni-button-color {
  228. color: #007aff;
  229. }
  230. .uni-dialog-input {
  231. flex: 1;
  232. font-size: 14px;
  233. border: 1px #eee solid;
  234. height: 40px;
  235. padding: 0 10px;
  236. border-radius: 5px;
  237. color: #555;
  238. }
  239. .uni-popup__success {
  240. color: #4cd964;
  241. }
  242. .uni-popup__warn {
  243. color: #f0ad4e;
  244. }
  245. .uni-popup__error {
  246. color: #dd524d;
  247. }
  248. .uni-popup__info {
  249. color: #909399;
  250. }
  251. </style>