u-alert-tips.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view class="u-alert-tips" v-if="show" :class="{'u-close-alert-tips': !show}" :style="{
  3. backgroundColor: computeBgColor,
  4. borderColor: computeBorderColor
  5. }">
  6. <view class="u-icon-wrap">
  7. <u-icon v-if="showIcon" :name="$u.type2icon(type)" :size="description ? 40 : 32" class="u-icon" :color="computeColor"></u-icon>
  8. </view>
  9. <view class="u-alert-content" @tap.stop="click">
  10. <view class="u-alert-title" :style="{fontWeight: description ? 500 : 'normal'}">
  11. {{title}}
  12. </view>
  13. <view v-if="description" class="u-alert-desc">
  14. {{description}}
  15. </view>
  16. </view>
  17. <view class="u-icon-wrap">
  18. <u-icon @click="close" v-if="closeAble && !closeText" hoverClass="u-type-error-hover-color" name="close" color="#c0c4cc"
  19. :size="22" class="u-close-icon" :style="{
  20. top: description ? '18rpx' : '24rpx'
  21. }"></u-icon>
  22. </view>
  23. <text v-if="closeAble && closeText" class="u-close-text" :style="{
  24. top: description ? '18rpx' : '24rpx'
  25. }">{{closeText}}</text>
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * alertTips 警告提示
  31. * @description 警告提示,展现需要关注的信息
  32. * @tutorial https://uviewui.com/components/alertTips.html
  33. * @property {String} title 显示的标题文字
  34. * @property {String} description 辅助性文字,颜色比title浅一点,字号也小一点,可选
  35. * @property {String} type 关闭按钮(默认为叉号icon图标)
  36. * @property {String} close-able 用文字替代关闭图标,close-able为true时有效
  37. * @property {Boolean} show-icon 是否显示左边的辅助图标
  38. * @property {Boolean} show 显示或隐藏组件
  39. * @event {Function} click 点击组件时触发
  40. * @event {Function} close 点击关闭按钮时触发
  41. */
  42. export default {
  43. name: 'u-alert-tips',
  44. props: {
  45. // 显示文字
  46. title: {
  47. type: String,
  48. default: ''
  49. },
  50. // 主题,success/warning/info/error
  51. type: {
  52. type: String,
  53. default: 'warning'
  54. },
  55. // 辅助性文字
  56. description: {
  57. type: String,
  58. default: ''
  59. },
  60. // 是否可关闭
  61. closeAble: {
  62. type: Boolean,
  63. default: false
  64. },
  65. // 关闭按钮自定义文本
  66. closeText: {
  67. type: String,
  68. default: ''
  69. },
  70. // 是否显示图标
  71. showIcon: {
  72. type: Boolean,
  73. default: false
  74. },
  75. // 文字颜色,如果定义了color值,icon会失效
  76. color: {
  77. type: String,
  78. default: ''
  79. },
  80. // 背景颜色
  81. bgColor: {
  82. type: String,
  83. default: ''
  84. },
  85. // 边框颜色
  86. borderColor: {
  87. type: String,
  88. default: ''
  89. },
  90. // 是否显示
  91. show: {
  92. type: Boolean,
  93. default: true
  94. }
  95. },
  96. data() {
  97. return {
  98. }
  99. },
  100. computed: {
  101. // 计算字体颜色,如果没有自定义的,就用uview主题颜色
  102. computeColor() {
  103. if (this.color) return this.color;
  104. else return this.$u.color[this.type];
  105. },
  106. // 计算背景颜色
  107. computeBgColor() {
  108. if (this.bgColor) return this.bgColor;
  109. return this.$u.color[this.type + 'Light'];
  110. },
  111. computeBorderColor() {
  112. if (this.borderColor) return this.borderColor;
  113. return this.$u.color[this.type + 'Disabled'];
  114. }
  115. },
  116. methods: {
  117. // 点击内容
  118. click() {
  119. this.$emit('click');
  120. },
  121. // 点击关闭按钮
  122. close() {
  123. this.$emit('close');
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .u-alert-tips {
  130. display: flex;
  131. align-items: center;
  132. padding: 16rpx 30rpx;
  133. border-radius: 8rpx;
  134. position: relative;
  135. transition: all 0.3s linear;
  136. border: 1px solid #fff;
  137. }
  138. .u-close-alert-tips {
  139. opacity: 0;
  140. visibility: hidden;
  141. }
  142. @keyframes myfirst {
  143. from {
  144. height: 100%;
  145. }
  146. to {
  147. height: 0
  148. }
  149. }
  150. .u-icon {
  151. margin-right: 16rpx;
  152. }
  153. .u-alert-title {
  154. font-size: 28rpx;
  155. color: $u-main-color;
  156. }
  157. .u-alert-desc {
  158. font-size: 26rpx;
  159. text-align: left;
  160. color: $u-content-color;
  161. }
  162. .u-close-icon {
  163. position: absolute;
  164. top: 20rpx;
  165. right: 20rpx;
  166. }
  167. .u-close-hover {
  168. color: red;
  169. }
  170. .u-close-text {
  171. font-size: 24rpx;
  172. color: $u-tips-color;
  173. position: absolute;
  174. top: 20rpx;
  175. right: 20rpx;
  176. line-height: 1;
  177. }
  178. </style>