u-alert.vue 5.2 KB

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