You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
5.2 KiB

5 months ago
  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. * @example <u-alert :title="title" type = "warning" :closable="closable" :description = "description"></u-alert>
  82. */
  83. export default {
  84. name: 'u-alert',
  85. mixins: [mpMixin, mixin, props],
  86. data() {
  87. return {
  88. show: true
  89. }
  90. },
  91. computed: {
  92. iconColor() {
  93. return this.effect === 'light' ? this.type : '#fff'
  94. },
  95. // 不同主题对应不同的图标
  96. iconName() {
  97. switch (this.type) {
  98. case 'success':
  99. return 'checkmark-circle-fill';
  100. break;
  101. case 'error':
  102. return 'close-circle-fill';
  103. break;
  104. case 'warning':
  105. return 'error-circle-fill';
  106. break;
  107. case 'info':
  108. return 'info-circle-fill';
  109. break;
  110. case 'primary':
  111. return 'more-circle-fill';
  112. break;
  113. default:
  114. return 'error-circle-fill';
  115. }
  116. }
  117. },
  118. emits: ["click"],
  119. methods: {
  120. addUnit,
  121. addStyle,
  122. // 点击内容
  123. clickHandler() {
  124. this.$emit('click')
  125. },
  126. // 点击关闭按钮
  127. closeHandler() {
  128. this.show = false
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. @import "../../libs/css/components.scss";
  135. .u-alert {
  136. position: relative;
  137. background-color: $u-primary;
  138. padding: 8px 10px;
  139. @include flex(row);
  140. align-items: center;
  141. border-top-left-radius: 4px;
  142. border-top-right-radius: 4px;
  143. border-bottom-left-radius: 4px;
  144. border-bottom-right-radius: 4px;
  145. &--primary--dark {
  146. background-color: $u-primary;
  147. }
  148. &--primary--light {
  149. background-color: #ecf5ff;
  150. }
  151. &--error--dark {
  152. background-color: $u-error;
  153. }
  154. &--error--light {
  155. background-color: #FEF0F0;
  156. }
  157. &--success--dark {
  158. background-color: $u-success;
  159. }
  160. &--success--light {
  161. background-color: #f5fff0;
  162. }
  163. &--warning--dark {
  164. background-color: $u-warning;
  165. }
  166. &--warning--light {
  167. background-color: #FDF6EC;
  168. }
  169. &--info--dark {
  170. background-color: $u-info;
  171. }
  172. &--info--light {
  173. background-color: #f4f4f5;
  174. }
  175. &__icon {
  176. margin-right: 5px;
  177. }
  178. &__content {
  179. @include flex(column);
  180. flex: 1;
  181. &__title {
  182. color: $u-main-color;
  183. font-size: 14px;
  184. font-weight: bold;
  185. color: #fff;
  186. margin-bottom: 2px;
  187. }
  188. &__desc {
  189. color: $u-main-color;
  190. font-size: 14px;
  191. flex-wrap: wrap;
  192. color: #fff;
  193. }
  194. }
  195. &__title--dark,
  196. &__desc--dark {
  197. color: #FFFFFF;
  198. }
  199. &__text--primary--light,
  200. &__text--primary--light {
  201. color: $u-primary;
  202. }
  203. &__text--success--light,
  204. &__text--success--light {
  205. color: $u-success;
  206. }
  207. &__text--warning--light,
  208. &__text--warning--light {
  209. color: $u-warning;
  210. }
  211. &__text--error--light,
  212. &__text--error--light {
  213. color: $u-error;
  214. }
  215. &__text--info--light,
  216. &__text--info--light {
  217. color: $u-info;
  218. }
  219. &__close {
  220. position: absolute;
  221. top: 11px;
  222. right: 10px;
  223. }
  224. }
  225. </style>