u-alert.vue 5.3 KB

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