u-alert-tips.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="u-alert-tips" v-if="show" :class="[
  3. !show ? 'u-close-alert-tips': '',
  4. type ? 'u-alert-tips--bg--' + type + '-light' : '',
  5. type ? 'u-alert-tips--border--' + type + '-disabled' : '',
  6. ]" :style="{
  7. backgroundColor: bgColor,
  8. borderColor: borderColor
  9. }">
  10. <view class="u-icon-wrap">
  11. <u-icon v-if="showIcon" :name="uIcon" :size="description ? 40 : 32" class="u-icon" :color="uIconType" :custom-style="iconStyle"></u-icon>
  12. </view>
  13. <view class="u-alert-content" @tap.stop="click">
  14. <view class="u-alert-title" :style="[uTitleStyle]">
  15. {{title}}
  16. </view>
  17. <view v-if="description" class="u-alert-desc" :style="[descStyle]">
  18. {{description}}
  19. </view>
  20. </view>
  21. <view class="u-icon-wrap">
  22. <u-icon @click="close" v-if="closeAble && !closeText" hoverClass="u-type-error-hover-color" name="close" color="#c0c4cc"
  23. :size="22" class="u-close-icon" :style="{
  24. top: description ? '18rpx' : '24rpx'
  25. }"></u-icon>
  26. </view>
  27. <text v-if="closeAble && closeText" class="u-close-text" :style="{
  28. top: description ? '18rpx' : '24rpx'
  29. }">{{closeText}}</text>
  30. </view>
  31. </template>
  32. <script>
  33. /**
  34. * alertTips 警告提示
  35. * @description 警告提示,展现需要关注的信息
  36. * @tutorial https://uviewui.com/components/alertTips.html
  37. * @property {String} title 显示的标题文字
  38. * @property {String} description 辅助性文字,颜色比title浅一点,字号也小一点,可选
  39. * @property {String} type 关闭按钮(默认为叉号icon图标)
  40. * @property {String} icon 图标名称
  41. * @property {Object} icon-style 图标的样式,对象形式
  42. * @property {Object} title-style 标题的样式,对象形式
  43. * @property {Object} desc-style 描述的样式,对象形式
  44. * @property {String} close-able 用文字替代关闭图标,close-able为true时有效
  45. * @property {Boolean} show-icon 是否显示左边的辅助图标
  46. * @property {Boolean} show 显示或隐藏组件
  47. * @event {Function} click 点击组件时触发
  48. * @event {Function} close 点击关闭按钮时触发
  49. */
  50. export default {
  51. name: 'u-alert-tips',
  52. props: {
  53. // 显示文字
  54. title: {
  55. type: String,
  56. default: ''
  57. },
  58. // 主题,success/warning/info/error
  59. type: {
  60. type: String,
  61. default: 'warning'
  62. },
  63. // 辅助性文字
  64. description: {
  65. type: String,
  66. default: ''
  67. },
  68. // 是否可关闭
  69. closeAble: {
  70. type: Boolean,
  71. default: false
  72. },
  73. // 关闭按钮自定义文本
  74. closeText: {
  75. type: String,
  76. default: ''
  77. },
  78. // 是否显示图标
  79. showIcon: {
  80. type: Boolean,
  81. default: false
  82. },
  83. // 文字颜色,如果定义了color值,icon会失效
  84. color: {
  85. type: String,
  86. default: ''
  87. },
  88. // 背景颜色
  89. bgColor: {
  90. type: String,
  91. default: ''
  92. },
  93. // 边框颜色
  94. borderColor: {
  95. type: String,
  96. default: ''
  97. },
  98. // 是否显示
  99. show: {
  100. type: Boolean,
  101. default: true
  102. },
  103. // 左边显示的icon
  104. icon: {
  105. type: String,
  106. default: ''
  107. },
  108. // icon的样式
  109. iconStyle: {
  110. type: Object,
  111. default() {
  112. return {}
  113. }
  114. },
  115. // 标题的样式
  116. titleStyle: {
  117. type: Object,
  118. default() {
  119. return {}
  120. }
  121. },
  122. // 描述文字的样式
  123. descStyle: {
  124. type: Object,
  125. default() {
  126. return {}
  127. }
  128. },
  129. },
  130. data() {
  131. return {
  132. }
  133. },
  134. computed: {
  135. uTitleStyle() {
  136. let style = {};
  137. // 如果有描述文字的话,标题进行加粗
  138. style.fontWeight = this.description ? 500 : 'normal';
  139. // 将用户传入样式对象和style合并,传入的优先级比style高,同属性会被覆盖
  140. return this.$u.deepMerge(style, this.titleStyle);
  141. },
  142. uIcon() {
  143. // 如果有设置icon名称就使用,否则根据type主题,推定一个默认的图标
  144. return this.icon ? this.icon : this.$u.type2icon(this.type);
  145. },
  146. uIconType() {
  147. // 如果有设置图标的样式,优先使用,没有的话,则用type的样式
  148. return Object.keys(this.iconStyle).length ? '' : this.type;
  149. }
  150. },
  151. methods: {
  152. // 点击内容
  153. click() {
  154. this.$emit('click');
  155. },
  156. // 点击关闭按钮
  157. close() {
  158. this.$emit('close');
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss" scoped>
  164. @import "../../libs/css/style.components.scss";
  165. .u-alert-tips {
  166. @include vue-flex;
  167. align-items: center;
  168. padding: 16rpx 30rpx;
  169. border-radius: 8rpx;
  170. position: relative;
  171. transition: all 0.3s linear;
  172. border: 1px solid #fff;
  173. &--bg--primary-light {
  174. background-color: $u-type-primary-light;
  175. }
  176. &--bg--info-light {
  177. background-color: $u-type-info-light;
  178. }
  179. &--bg--success-light {
  180. background-color: $u-type-success-light;
  181. }
  182. &--bg--warning-light {
  183. background-color: $u-type-warning-light;
  184. }
  185. &--bg--error-light {
  186. background-color: $u-type-error-light;
  187. }
  188. &--border--primary-disabled {
  189. border-color: $u-type-primary-disabled;
  190. }
  191. &--border--success-disabled {
  192. border-color: $u-type-success-disabled;
  193. }
  194. &--border--error-disabled {
  195. border-color: $u-type-error-disabled;
  196. }
  197. &--border--warning-disabled {
  198. border-color: $u-type-warning-disabled;
  199. }
  200. &--border--info-disabled {
  201. border-color: $u-type-info-disabled;
  202. }
  203. }
  204. .u-close-alert-tips {
  205. opacity: 0;
  206. visibility: hidden;
  207. }
  208. .u-icon {
  209. margin-right: 16rpx;
  210. }
  211. .u-alert-title {
  212. font-size: 28rpx;
  213. color: $u-main-color;
  214. }
  215. .u-alert-desc {
  216. font-size: 26rpx;
  217. text-align: left;
  218. color: $u-content-color;
  219. }
  220. .u-close-icon {
  221. position: absolute;
  222. top: 20rpx;
  223. right: 20rpx;
  224. }
  225. .u-close-hover {
  226. color: red;
  227. }
  228. .u-close-text {
  229. font-size: 24rpx;
  230. color: $u-tips-color;
  231. position: absolute;
  232. top: 20rpx;
  233. right: 20rpx;
  234. line-height: 1;
  235. }
  236. </style>