u-alert-tips.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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" @click.stop="click">
  14. <view class="u-alert-title" :style="[uTitleStyle]">
  15. <slot>{{title}}</slot>
  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 @click="close" 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. emits: ["click", "close"],
  53. props: {
  54. // 显示文字
  55. title: {
  56. type: String,
  57. default: ''
  58. },
  59. // 主题,success/warning/info/error
  60. type: {
  61. type: String,
  62. default: 'warning'
  63. },
  64. // 辅助性文字
  65. description: {
  66. type: String,
  67. default: ''
  68. },
  69. // 是否可关闭
  70. closeAble: {
  71. type: Boolean,
  72. default: false
  73. },
  74. // 关闭按钮自定义文本
  75. closeText: {
  76. type: String,
  77. default: ''
  78. },
  79. // 是否显示图标
  80. showIcon: {
  81. type: Boolean,
  82. default: false
  83. },
  84. // 文字颜色,如果定义了color值,icon会失效
  85. color: {
  86. type: String,
  87. default: ''
  88. },
  89. // 背景颜色
  90. bgColor: {
  91. type: String,
  92. default: ''
  93. },
  94. // 边框颜色
  95. borderColor: {
  96. type: String,
  97. default: ''
  98. },
  99. // 是否显示
  100. show: {
  101. type: Boolean,
  102. default: true
  103. },
  104. // 左边显示的icon
  105. icon: {
  106. type: String,
  107. default: ''
  108. },
  109. // icon的样式
  110. iconStyle: {
  111. type: Object,
  112. default() {
  113. return {}
  114. }
  115. },
  116. // 标题的样式
  117. titleStyle: {
  118. type: Object,
  119. default() {
  120. return {}
  121. }
  122. },
  123. // 描述文字的样式
  124. descStyle: {
  125. type: Object,
  126. default() {
  127. return {}
  128. }
  129. },
  130. },
  131. data() {
  132. return {
  133. }
  134. },
  135. computed: {
  136. uTitleStyle() {
  137. let style = {};
  138. // 如果有描述文字的话,标题进行加粗
  139. style.fontWeight = this.description ? 500 : 'normal';
  140. // 将用户传入样式对象和style合并,传入的优先级比style高,同属性会被覆盖
  141. return this.$u.deepMerge(style, this.titleStyle);
  142. },
  143. uIcon() {
  144. // 如果有设置icon名称就使用,否则根据type主题,推定一个默认的图标
  145. return this.icon ? this.icon : this.$u.type2icon(this.type);
  146. },
  147. uIconType() {
  148. // 如果有设置图标的样式,优先使用,没有的话,则用type的样式
  149. return Object.keys(this.iconStyle).length ? '' : this.type;
  150. }
  151. },
  152. methods: {
  153. // 点击内容
  154. click() {
  155. this.$emit('click');
  156. },
  157. // 点击关闭按钮
  158. close() {
  159. this.$emit('close');
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang="scss" scoped>
  165. @import "../../libs/css/style.components.scss";
  166. .u-alert-tips {
  167. @include vue-flex;
  168. align-items: center;
  169. padding: 16rpx 30rpx;
  170. border-radius: 8rpx;
  171. position: relative;
  172. transition: all 0.3s linear;
  173. border: 1px solid #fff;
  174. &--bg--primary-light {
  175. background-color: $u-type-primary-light;
  176. }
  177. &--bg--info-light {
  178. background-color: $u-type-info-light;
  179. }
  180. &--bg--success-light {
  181. background-color: $u-type-success-light;
  182. }
  183. &--bg--warning-light {
  184. background-color: $u-type-warning-light;
  185. }
  186. &--bg--error-light {
  187. background-color: $u-type-error-light;
  188. }
  189. &--border--primary-disabled {
  190. border-color: $u-type-primary-disabled;
  191. }
  192. &--border--success-disabled {
  193. border-color: $u-type-success-disabled;
  194. }
  195. &--border--error-disabled {
  196. border-color: $u-type-error-disabled;
  197. }
  198. &--border--warning-disabled {
  199. border-color: $u-type-warning-disabled;
  200. }
  201. &--border--info-disabled {
  202. border-color: $u-type-info-disabled;
  203. }
  204. }
  205. .u-close-alert-tips {
  206. opacity: 0;
  207. visibility: hidden;
  208. }
  209. .u-icon {
  210. margin-right: 16rpx;
  211. }
  212. .u-alert-title {
  213. font-size: 28rpx;
  214. color: $u-main-color;
  215. }
  216. .u-alert-desc {
  217. font-size: 26rpx;
  218. text-align: left;
  219. color: $u-content-color;
  220. }
  221. .u-close-icon {
  222. position: absolute;
  223. top: 20rpx;
  224. right: 20rpx;
  225. }
  226. .u-close-hover {
  227. color: red;
  228. }
  229. .u-close-text {
  230. font-size: 24rpx;
  231. color: $u-tips-color;
  232. position: absolute;
  233. top: 20rpx;
  234. right: 20rpx;
  235. line-height: 1;
  236. }
  237. .u-alert-content {
  238. width: 100%;
  239. }
  240. </style>