u-alert-tips.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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"
  12. :custom-style="iconStyle"></u-icon>
  13. </view>
  14. <view class="u-alert-content" @click.stop="click">
  15. <view class="u-alert-title" :style="[uTitleStyle]">
  16. {{title}}
  17. </view>
  18. <view v-if="description" class="u-alert-desc" :style="[descStyle]">
  19. {{description}}
  20. </view>
  21. </view>
  22. <view class="u-icon-wrap">
  23. <u-icon @click="close" v-if="closeAble && !closeText" hoverClass="u-type-error-hover-color" name="close"
  24. color="#c0c4cc" :size="22" class="u-close-icon" :style="{
  25. top: description ? '18rpx' : '24rpx'
  26. }"></u-icon>
  27. </view>
  28. <text @click="close" v-if="closeAble && closeText" class="u-close-text" :style="{
  29. top: description ? '18rpx' : '24rpx'
  30. }">{{closeText}}</text>
  31. </view>
  32. </template>
  33. <script>
  34. /**
  35. * alertTips 警告提示
  36. * @description 警告提示,展现需要关注的信息
  37. * @tutorial https://uviewui.com/components/alertTips.html
  38. * @property {String} title 显示的标题文字
  39. * @property {String} description 辅助性文字,颜色比title浅一点,字号也小一点,可选
  40. * @property {String} type 关闭按钮(默认为叉号icon图标)
  41. * @property {String} icon 图标名称
  42. * @property {Object} icon-style 图标的样式,对象形式
  43. * @property {Object} title-style 标题的样式,对象形式
  44. * @property {Object} desc-style 描述的样式,对象形式
  45. * @property {String} close-able 用文字替代关闭图标,close-able为true时有效
  46. * @property {Boolean} show-icon 是否显示左边的辅助图标
  47. * @property {Boolean} show 显示或隐藏组件
  48. * @event {Function} click 点击组件时触发
  49. * @event {Function} close 点击关闭按钮时触发
  50. */
  51. export default {
  52. name: 'u-alert-tips',
  53. emits: ["click", "close"],
  54. props: {
  55. // 显示文字
  56. title: {
  57. type: String,
  58. default: ''
  59. },
  60. // 主题,success/warning/info/error
  61. type: {
  62. type: String,
  63. default: 'warning'
  64. },
  65. // 辅助性文字
  66. description: {
  67. type: String,
  68. default: ''
  69. },
  70. // 是否可关闭
  71. closeAble: {
  72. type: Boolean,
  73. default: false
  74. },
  75. // 关闭按钮自定义文本
  76. closeText: {
  77. type: String,
  78. default: ''
  79. },
  80. // 是否显示图标
  81. showIcon: {
  82. type: Boolean,
  83. default: false
  84. },
  85. // 文字颜色,如果定义了color值,icon会失效
  86. color: {
  87. type: String,
  88. default: ''
  89. },
  90. // 背景颜色
  91. bgColor: {
  92. type: String,
  93. default: ''
  94. },
  95. // 边框颜色
  96. borderColor: {
  97. type: String,
  98. default: ''
  99. },
  100. // 是否显示
  101. show: {
  102. type: Boolean,
  103. default: true
  104. },
  105. // 左边显示的icon
  106. icon: {
  107. type: String,
  108. default: ''
  109. },
  110. // icon的样式
  111. iconStyle: {
  112. type: Object,
  113. default () {
  114. return {}
  115. }
  116. },
  117. // 标题的样式
  118. titleStyle: {
  119. type: Object,
  120. default () {
  121. return {}
  122. }
  123. },
  124. // 描述文字的样式
  125. descStyle: {
  126. type: Object,
  127. default () {
  128. return {}
  129. }
  130. },
  131. },
  132. data() {
  133. return {}
  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>