index.uvue 3.01 KB
<template>
  <!-- 引用示例:<CustomEmpty preset="contract" :icon="'/static/empty/icon-base-empty.png'" :title="'自定义文案'" :customStyle="{ backgroundColor: '#999999', width: '300rpx' }"/> -->
  <view class="empty-wrapper" :style="wrapperStyle">
    <!-- 图标插槽,支持完全自定义图标 -->
    <slot name="icon">
      <image
        v-if="showIcon"
        class="empty-icon"
        :src="iconSrc"
        mode="heightFix"
      />
    </slot>

    <!-- 文字与自定义内容插槽 -->
    <slot>
      <view class="empty-text">
        <text v-if="showTitle" class="empty-title">{{ displayTitle }}</text>
        <text v-if="showDescription" class="empty-desc">{{ props.description }}</text>
      </view>
    </slot>
  </view>
</template>

<script setup lang="uts">
  import { computed } from 'vue'
  import {
    EMPTY_FALLBACK_TITLE,
    EMPTY_PROP_DEFAULT_DESCRIPTION,
    EMPTY_PROP_DEFAULT_ICON,
    EMPTY_PROP_DEFAULT_PRESET,
    EMPTY_PROP_DEFAULT_TITLE,
    getEmptyPreset,
    TYPE_ICON_BASE_PATH,
    type EmptyTypeConfig
  } from './presets.uts'

  type Props = {
    preset?: String
    icon?: String
    title?: String
    description?: String
    customStyle?: any | null
  }

  const props = withDefaults(defineProps<Props>(), {
    preset: EMPTY_PROP_DEFAULT_PRESET,
    icon: EMPTY_PROP_DEFAULT_ICON,
    title: EMPTY_PROP_DEFAULT_TITLE,
    description: EMPTY_PROP_DEFAULT_DESCRIPTION,
    customStyle: null
  })

  const typeConfig = computed<EmptyTypeConfig | null>(() => {
    return getEmptyPreset(props.preset)
  })

  const iconSrc = computed<String>(() => {
    if (props.icon != EMPTY_PROP_DEFAULT_ICON) {
      return props.icon
    }
    const config = typeConfig.value
    if (config == null) {
      return EMPTY_PROP_DEFAULT_ICON
    }
    return `${TYPE_ICON_BASE_PATH}${config.icon}`
  })

  const displayTitle = computed<String>(() => {
    if (props.title != EMPTY_PROP_DEFAULT_TITLE) {
      return props.title
    }
    const cfg = typeConfig.value
    if (cfg == null) {
      return EMPTY_FALLBACK_TITLE
    }
    return cfg.title
  })

  const wrapperStyle = computed<any>(() => {
    return props.customStyle ?? {}
  })

  const showIcon = computed<boolean>(() => {
    return iconSrc.value != EMPTY_PROP_DEFAULT_ICON
  })

  const showTitle = computed<boolean>(() => {
    return displayTitle.value != EMPTY_PROP_DEFAULT_TITLE
  })

  const showDescription = computed<boolean>(() => {
    return props.description != EMPTY_PROP_DEFAULT_DESCRIPTION
  })
</script>

<style>
.empty-wrapper {
  /* 默认宽高占满容器,居中显示,适配手机与 Pad */
  width: 100%;
  min-height: 300rpx;
  padding: 40rpx 32rpx;
  box-sizing: border-box;

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  height: 104px;
  width: auto;
}

.empty-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
  font-weight: 400;
}
.empty-title {
  color: #999999;
}
.empty-desc {
  color: #999999;
  margin-top: 12rpx;
}
</style>