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.

133 lines
3.8 KiB

5 months ago
  1. <template>
  2. <view
  3. class="u-empty"
  4. :style="[emptyStyle]"
  5. v-if="show"
  6. >
  7. <u-icon
  8. v-if="!isSrc"
  9. :name="mode === 'message' ? 'chat' : `empty-${mode}`"
  10. :size="iconSize"
  11. :color="iconColor"
  12. margin-top="14"
  13. ></u-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. /**
  38. * empty 内容为空
  39. * @description 该组件用于需要加载内容但是加载的第一页数据就为空提示一个"没有内容"的场景 我们精心挑选了十几个场景的图标方便您使用
  40. * @tutorial https://ijry.github.io/uview-plus/components/empty.html
  41. * @property {String} icon 内置图标名称或图片路径建议绝对路径
  42. * @property {String} text 提示文字
  43. * @property {String} textColor 文字颜色 (默认 '#c0c4cc' )
  44. * @property {String | Number} textSize 文字大小 默认 14
  45. * @property {String} iconColor 图标的颜色 默认 '#c0c4cc'
  46. * @property {String | Number} iconSize 图标的大小 默认 90
  47. * @property {String} mode 选择预置的图标类型 默认 'data'
  48. * @property {String | Number} width 图标宽度单位px 默认 160
  49. * @property {String | Number} height 图标高度单位px 默认 160
  50. * @property {Boolean} show 是否显示组件 默认 true
  51. * @property {String | Number} marginTop 组件距离上一个元素之间的距离默认px单位 默认 0
  52. * @property {Object} customStyle 定义需要用到的外部样式
  53. *
  54. * @event {Function} click 点击组件时触发
  55. * @event {Function} close 点击关闭按钮时触发
  56. * @example <u-empty text="所谓伊人,在水一方" mode="list"></u-empty>
  57. */
  58. export default {
  59. name: "u-empty",
  60. mixins: [mpMixin, mixin, props],
  61. data() {
  62. return {
  63. icons: {
  64. car: '购物车为空',
  65. page: '页面不存在',
  66. search: '没有搜索结果',
  67. address: '没有收货地址',
  68. wifi: '没有WiFi',
  69. order: '订单为空',
  70. coupon: '没有优惠券',
  71. favor: '暂无收藏',
  72. permission: '无权限',
  73. history: '无历史记录',
  74. news: '无新闻列表',
  75. message: '消息列表为空',
  76. list: '列表为空',
  77. data: '数据为空',
  78. comment: '暂无评论',
  79. }
  80. }
  81. },
  82. computed: {
  83. // 组件样式
  84. emptyStyle() {
  85. const style = {}
  86. style.marginTop = addUnit(this.marginTop)
  87. // 合并customStyle样式,此参数通过mixin中的props传递
  88. return deepMerge(addStyle(this.customStyle), style)
  89. },
  90. // 文本样式
  91. textStyle() {
  92. const style = {}
  93. style.color = this.textColor
  94. style.fontSize = addUnit(this.textSize)
  95. return style
  96. },
  97. // 判断icon是否图片路径
  98. isSrc() {
  99. return this.icon.indexOf('/') >= 0
  100. }
  101. },
  102. methods: {
  103. addUnit
  104. }
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. @import '../../libs/css/components.scss';
  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>