Alert.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <u-transition mode="fade-down" :show="show">
  3. <view
  4. class="u-custom-alert"
  5. :class="[`u-custom-alert--${type}--${effect}`]"
  6. @tap.stop="clickHandler"
  7. >
  8. <view class="u-custom-alert__icon" v-if="showIcon">
  9. <u-icon :name="iconName" size="18" :color="iconColor"></u-icon>
  10. </view>
  11. <view
  12. class="u-custom-alert__content"
  13. :style="[
  14. {
  15. paddingRight: closable ? '20px' : 0
  16. }
  17. ]"
  18. >
  19. <text
  20. class="u-custom-alert__content__title"
  21. v-if="title"
  22. :style="[
  23. {
  24. fontSize: $u.addUnit(fontSize),
  25. textAlign: center ? 'center' : 'left'
  26. }
  27. ]"
  28. :class="[
  29. effect === 'dark'
  30. ? 'u-custom-alert__text--dark'
  31. : `u-custom-alert__text--${type}--light`
  32. ]"
  33. >{{ title }}</text
  34. >
  35. <view class="u-custom-alert__content__row-space-end">
  36. <text
  37. class="u-custom-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-custom-alert__text--dark'
  48. : `u-custom-alert__text--${type}--light`
  49. ]"
  50. >{{ description }}</text
  51. >
  52. <text class="u-custom-alert__content__more"> 更多》</text>
  53. </view>
  54. </view>
  55. <view
  56. class="u-custom-alert__close"
  57. v-if="closable"
  58. @tap.stop="closeHandler"
  59. >
  60. <u-icon name="close" :color="iconColor" size="15"></u-icon>
  61. </view>
  62. </view>
  63. </u-transition>
  64. </template>
  65. <script>
  66. /**
  67. * Alert 警告提示
  68. * @description 警告提示,展现需要关注的信息。
  69. * @tutorial https://www.uviewui.com/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-custom-alert',
  85. props: {
  86. // 显示文字
  87. title: {
  88. type: String,
  89. default: uni.$u.props.alert.title
  90. },
  91. // 主题,success/warning/info/error
  92. type: {
  93. type: String,
  94. default: uni.$u.props.alert.type
  95. },
  96. // 辅助性文字
  97. description: {
  98. type: String,
  99. default: uni.$u.props.alert.description
  100. },
  101. // 是否可关闭
  102. closable: {
  103. type: Boolean,
  104. default: uni.$u.props.alert.closable
  105. },
  106. // 是否显示图标
  107. showIcon: {
  108. type: Boolean,
  109. default: uni.$u.props.alert.showIcon
  110. },
  111. // 浅或深色调,light-浅色,dark-深色
  112. effect: {
  113. type: String,
  114. default: uni.$u.props.alert.effect
  115. },
  116. // 文字是否居中
  117. center: {
  118. type: Boolean,
  119. default: uni.$u.props.alert.center
  120. },
  121. // 字体大小
  122. fontSize: {
  123. type: [String, Number],
  124. default: uni.$u.props.alert.fontSize
  125. }
  126. },
  127. data() {
  128. return {
  129. show: true
  130. };
  131. },
  132. computed: {
  133. iconColor() {
  134. // return this.effect === 'light' ? this.type : '#fff';
  135. return '#fff';
  136. },
  137. // 不同主题对应不同的图标
  138. iconName() {
  139. switch (this.type) {
  140. case 'success':
  141. return 'checkmark-circle-fill';
  142. break;
  143. case 'error':
  144. return 'close-circle-fill';
  145. break;
  146. case 'warning':
  147. return 'warning-fill';
  148. break;
  149. case 'info':
  150. return 'info-circle-fill';
  151. break;
  152. case 'primary':
  153. return 'more-circle-fill';
  154. break;
  155. default:
  156. return 'error-circle-fill';
  157. }
  158. }
  159. },
  160. methods: {
  161. // 点击内容
  162. clickHandler() {
  163. this.$emit('click');
  164. },
  165. // 点击关闭按钮
  166. closeHandler() {
  167. console.log('closeHandler', '------------------------- ');
  168. this.show = false;
  169. this.$emit('close');
  170. }
  171. }
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. .u-custom-alert {
  176. position: relative;
  177. padding: 8px 10px;
  178. align-items: center;
  179. border-top-left-radius: 16rpx;
  180. border-top-right-radius: 16rpx;
  181. border-bottom-left-radius: 16rpx;
  182. border-bottom-right-radius: 16rpx;
  183. pointer-events: auto;
  184. &--primary--dark {
  185. }
  186. &--primary--light {
  187. background-color: #ecf5ff;
  188. }
  189. &--error--dark {
  190. }
  191. &--error--light {
  192. background-color: #fef0f0;
  193. }
  194. &--success--dark {
  195. }
  196. &--success--light {
  197. background-color: #f5fff0;
  198. }
  199. &--warning--dark {
  200. }
  201. &--warning--light {
  202. background-color: rgba(238, 172, 25, 0.8);
  203. }
  204. &--info--dark {
  205. }
  206. &--info--light {
  207. background-color: #f4f4f5;
  208. }
  209. &__icon {
  210. margin-right: 5px;
  211. }
  212. &__content {
  213. flex: 1;
  214. &__title {
  215. font-size: 14px;
  216. font-weight: bold;
  217. color: #fff;
  218. margin-bottom: 2px;
  219. }
  220. &__desc {
  221. font-size: 14px;
  222. flex-wrap: wrap;
  223. color: #fff;
  224. }
  225. &__more {
  226. color: #fff;
  227. font-size: 14px;
  228. }
  229. &__row-space-end {
  230. display: flex;
  231. justify-content: space-between;
  232. align-items: center;
  233. padding-right: 20px;
  234. }
  235. }
  236. &__title--dark,
  237. &__desc--dark {
  238. color: #ffffff;
  239. }
  240. &__text--primary--light,
  241. &__text--primary--light {
  242. }
  243. &__text--success--light,
  244. &__text--success--light {
  245. }
  246. &__text--warning--light,
  247. &__text--warning--light {
  248. color: #fff;
  249. }
  250. &__text--error--light,
  251. &__text--error--light {
  252. }
  253. &__text--info--light,
  254. &__text--info--light {
  255. }
  256. &__close {
  257. position: absolute;
  258. top: 11px;
  259. right: 10px;
  260. z-index: 12;
  261. }
  262. }
  263. </style>