u-empty.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view
  3. class="u-empty"
  4. :style="[emptyStyle]"
  5. v-if="show"
  6. >
  7. <up-icon
  8. v-if="!isSrc"
  9. :name="mode === 'message' ? 'chat' : `empty-${mode}`"
  10. :size="iconSize"
  11. :color="iconColor"
  12. margin-top="14"
  13. ></up-icon>
  14. <image
  15. v-else
  16. :style="{
  17. width: addUnit(width),
  18. height: addUnit(height),
  19. }"
  20. :src="icon"
  21. mode="widthFix"
  22. ></image>
  23. <text
  24. class="u-empty__text"
  25. :style="[textStyle]"
  26. >{{text ? text : icons[mode]}}</text>
  27. <view class="u-empty__wrap" v-if="$slots.default || $slots.$default">
  28. <slot />
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import { props } from './props';
  34. import { mpMixin } from '../../libs/mixin/mpMixin';
  35. import { mixin } from '../../libs/mixin/mixin';
  36. import { addUnit, addStyle, deepMerge } from '../../libs/function/index';
  37. import { t } from '../../libs/i18n'
  38. /**
  39. * empty 内容为空
  40. * @description 该组件用于需要加载内容,但是加载的第一页数据就为空,提示一个"没有内容"的场景, 我们精心挑选了十几个场景的图标,方便您使用。
  41. * @tutorial https://ijry.github.io/uview-plus/components/empty.html
  42. * @property {String} icon 内置图标名称,或图片路径,建议绝对路径
  43. * @property {String} text 提示文字
  44. * @property {String} textColor 文字颜色 (默认 '#c0c4cc' )
  45. * @property {String | Number} textSize 文字大小 (默认 14 )
  46. * @property {String} iconColor 图标的颜色 (默认 '#c0c4cc' )
  47. * @property {String | Number} iconSize 图标的大小 (默认 90 )
  48. * @property {String} mode 选择预置的图标类型 (默认 'data' )
  49. * @property {String | Number} width 图标宽度,单位px (默认 160 )
  50. * @property {String | Number} height 图标高度,单位px (默认 160 )
  51. * @property {Boolean} show 是否显示组件 (默认 true )
  52. * @property {String | Number} marginTop 组件距离上一个元素之间的距离,默认px单位 (默认 0 )
  53. * @property {Object} customStyle 定义需要用到的外部样式
  54. *
  55. * @event {Function} click 点击组件时触发
  56. * @event {Function} close 点击关闭按钮时触发
  57. * @example <u-empty text="所谓伊人,在水一方" mode="list"></u-empty>
  58. */
  59. export default {
  60. name: "u-empty",
  61. mixins: [mpMixin, mixin, props],
  62. data() {
  63. return {
  64. icons: {
  65. car: t("up.empty.car"),
  66. page: t("up.empty.page"),
  67. search: t("up.empty.search"),
  68. address: t("up.empty.address"),
  69. wifi: t("up.empty.wifi"),
  70. order: t("up.empty.order"),
  71. coupon: t("up.empty.coupon"),
  72. favor: t("up.empty.favor"),
  73. permission: t("up.empty.permission"),
  74. history: t("up.empty.history"),
  75. news: t("up.empty.news"),
  76. message: t("up.empty.message"),
  77. list: t("up.empty.list"),
  78. data: t("up.empty.data"),
  79. comment: t("up.empty.comment"),
  80. }
  81. }
  82. },
  83. computed: {
  84. // 组件样式
  85. emptyStyle() {
  86. const style = {}
  87. style.marginTop = addUnit(this.marginTop)
  88. // 合并customStyle样式,此参数通过mixin中的props传递
  89. return deepMerge(addStyle(this.customStyle), style)
  90. },
  91. // 文本样式
  92. textStyle() {
  93. const style = {}
  94. style.color = this.textColor
  95. style.fontSize = addUnit(this.textSize)
  96. return style
  97. },
  98. // 判断icon是否图片路径
  99. isSrc() {
  100. return this.icon.indexOf('/') >= 0
  101. }
  102. },
  103. methods: {
  104. addUnit
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. $u-empty-text-margin-top:20rpx !default;
  110. $u-empty-slot-margin-top:20rpx !default;
  111. .u-empty {
  112. @include flex;
  113. flex-direction: column;
  114. justify-content: center;
  115. align-items: center;
  116. &__text {
  117. @include flex;
  118. justify-content: center;
  119. align-items: center;
  120. margin-top: $u-empty-text-margin-top;
  121. }
  122. }
  123. .u-slot-wrap {
  124. @include flex;
  125. justify-content: center;
  126. align-items: center;
  127. margin-top:$u-empty-slot-margin-top;
  128. }
  129. </style>