u-alert-tips.vue 5.9 KB

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