u-alert.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <u-transition mode="fade" :show="show">
  3. <view
  4. class="u-alert"
  5. :class="[`u-alert--${type}--${effect}`]"
  6. @tap.stop="clickHandler"
  7. :style="[$u.addStyle(customStyle)]"
  8. >
  9. <view class="u-alert__icon" v-if="showIcon">
  10. <u-icon :name="iconName" size="18" :color="iconColor"></u-icon>
  11. </view>
  12. <view
  13. class="u-alert__content"
  14. :style="[
  15. {
  16. paddingRight: closable ? '20px' : 0
  17. }
  18. ]"
  19. >
  20. <text
  21. class="u-alert__content__title"
  22. v-if="title"
  23. :style="[
  24. {
  25. fontSize: $u.addUnit(fontSize),
  26. textAlign: center ? 'center' : 'left'
  27. }
  28. ]"
  29. :class="[
  30. effect === 'dark'
  31. ? 'u-alert__text--dark'
  32. : `u-alert__text--${type}--light`
  33. ]"
  34. >{{ title }}</text
  35. >
  36. <text
  37. class="u-alert__content__desc"
  38. v-if="description"
  39. :style="[
  40. {
  41. fontSize: $u.addUnit(fontSize),
  42. textAlign: center ? 'center' : 'left'
  43. }
  44. ]"
  45. :class="[
  46. effect === 'dark'
  47. ? 'u-alert__text--dark'
  48. : `u-alert__text--${type}--light`
  49. ]"
  50. >{{ description }}</text
  51. >
  52. </view>
  53. <view class="u-alert__close" v-if="closable" @tap.stop="closeHandler">
  54. <u-icon name="close" :color="iconColor" size="15"></u-icon>
  55. </view>
  56. </view>
  57. </u-transition>
  58. </template>
  59. <script>
  60. import props from './props.js'
  61. /**
  62. * Alert 警告提示
  63. * @description 警告提示,展现需要关注的信息。
  64. * @tutorial https://www.uviewui.com/components/alertTips.html
  65. *
  66. * @property {String} title 显示的文字
  67. * @property {String} type 使用预设的颜色 (默认 'warning' )
  68. * @property {String} description 辅助性文字,颜色比title浅一点,字号也小一点,可选
  69. * @property {Boolean} closable 关闭按钮(默认为叉号icon图标) (默认 false )
  70. * @property {Boolean} showIcon 是否显示左边的辅助图标 ( 默认 false )
  71. * @property {String} effect 多图时,图片缩放裁剪的模式 (默认 'light' )
  72. * @property {Boolean} center 文字是否居中 (默认 false )
  73. * @property {String | Number} fontSize 字体大小 (默认 14 )
  74. * @property {Object} customStyle 定义需要用到的外部样式
  75. * @event {Function} click 点击组件时触发
  76. * @example <u-alert :title="title" type = "warning" :closable="closable" :description = "description"></u-alert>
  77. */
  78. export default {
  79. name: 'u-alert',
  80. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  81. data () {
  82. return {
  83. show: true
  84. }
  85. },
  86. computed: {
  87. iconColor () {
  88. return this.effect === 'light' ? this.type : '#fff'
  89. },
  90. // 不同主题对应不同的图标
  91. iconName () {
  92. switch (this.type) {
  93. case 'success':
  94. return 'checkmark-circle-fill'
  95. break
  96. case 'error':
  97. return 'close-circle-fill'
  98. break
  99. case 'warning':
  100. return 'error-circle-fill'
  101. break
  102. case 'info':
  103. return 'info-circle-fill'
  104. break
  105. case 'primary':
  106. return 'more-circle-fill'
  107. break
  108. default:
  109. return 'error-circle-fill'
  110. }
  111. }
  112. },
  113. methods: {
  114. // 点击内容
  115. clickHandler () {
  116. this.$emit('click')
  117. },
  118. // 点击关闭按钮
  119. closeHandler () {
  120. this.show = false
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss" scoped>
  126. @import '../../libs/css/components.scss';
  127. .u-alert {
  128. position: relative;
  129. background-color: $u-primary;
  130. padding: 8px 10px;
  131. @include flex(row);
  132. align-items: center;
  133. border-top-left-radius: 4px;
  134. border-top-right-radius: 4px;
  135. border-bottom-left-radius: 4px;
  136. border-bottom-right-radius: 4px;
  137. &--primary--dark {
  138. background-color: $u-primary;
  139. }
  140. &--primary--light {
  141. background-color: #ecf5ff;
  142. }
  143. &--error--dark {
  144. background-color: $u-error;
  145. }
  146. &--error--light {
  147. background-color: #fef0f0;
  148. }
  149. &--success--dark {
  150. background-color: $u-success;
  151. }
  152. &--success--light {
  153. background-color: #f5fff0;
  154. }
  155. &--warning--dark {
  156. background-color: $u-warning;
  157. }
  158. &--warning--light {
  159. background-color: #fdf6ec;
  160. }
  161. &--info--dark {
  162. background-color: $u-info;
  163. }
  164. &--info--light {
  165. background-color: #f4f4f5;
  166. }
  167. &__icon {
  168. margin-right: 5px;
  169. }
  170. &__content {
  171. @include flex(column);
  172. flex: 1;
  173. &__title {
  174. color: $u-main-color;
  175. font-size: 32rpx;
  176. font-weight: bold;
  177. color: #fff;
  178. margin-bottom: 2px;
  179. }
  180. &__desc {
  181. color: $u-main-color;
  182. font-size: 32rpx;
  183. flex-wrap: wrap;
  184. color: #fff;
  185. }
  186. }
  187. &__title--dark,
  188. &__desc--dark {
  189. color: #ffffff;
  190. }
  191. &__text--primary--light,
  192. &__text--primary--light {
  193. color: $u-primary;
  194. }
  195. &__text--success--light,
  196. &__text--success--light {
  197. color: $u-success;
  198. }
  199. &__text--warning--light,
  200. &__text--warning--light {
  201. color: $u-warning;
  202. }
  203. &__text--error--light,
  204. &__text--error--light {
  205. color: $u-error;
  206. }
  207. &__text--info--light,
  208. &__text--info--light {
  209. color: $u-info;
  210. }
  211. &__close {
  212. position: absolute;
  213. top: 11px;
  214. right: 10px;
  215. }
  216. }
  217. </style>