Commit b79b60bc by Lin

feat: 缺省占位组件开发

1 parent c492c30e
<template>
<!-- 引用示例:<CustomEmpty type="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'
type EmptyTypeConfig = {
icon: string
title: string
}
/** 预设类型 key,扩展时在此类型和 PRESET_TYPES 中同步添加 */
type PresetTypesMap = {
base: EmptyTypeConfig
home: EmptyTypeConfig
contract: EmptyTypeConfig
function:EmptyTypeConfig
}
const TYPE_ICON_BASE_PATH: string = '/static/empty/'
/** 预设类型配置,图标需放在 /static/empty/ 下 */
const PRESET_TYPES: PresetTypesMap = {
base: {
icon: 'icon-base-empty.png',
title: '暂无数据'
},
home: {
icon: 'icon-home-empty.png',
title: '暂无数据'
},
contract: {
icon: 'icon-contract-empty.png',
title: '请先选择合同,签约个人信息自动带出'
},
function: {
icon: 'icon-function-empty.png',
title: '请先选择合同,签约个人信息自动带出'
}
}
interface CustomEmptyProps {
/**
* 预设类型:base | home | contract | function,展示对应图标与默认文案;扩展时在 PRESET_TYPES 中增加配置即可
*/
type: string
/**
* 直接传入图标地址,优先级高于 type
*/
icon: string
/**
* 主标题文案,不传时使用当前类型的默认文案
*/
title: string
/**
* 副标题/描述文案
*/
description: string
/**
* 外层容器样式,支持字符串或对象,覆盖默认居中样式
*/
customStyle: any | null
}
const props = withDefaults(defineProps<CustomEmptyProps>(), {
type: 'base',
icon: '',
title: '',
description: '',
customStyle: null
})
const getPreset = (type: string): EmptyTypeConfig | null => {
if (type == 'base') return PRESET_TYPES.base
if (type == 'home') return PRESET_TYPES.home
if (type == 'contract') return PRESET_TYPES.contract
if (type == 'function') return PRESET_TYPES.function
return null
}
const typeConfig = computed<EmptyTypeConfig | null>(() => {
return getPreset(props.type)
})
const iconSrc = computed<string>(() => {
if (props.icon != '') {
return props.icon
}
const config = typeConfig.value
if (config == null) {
return ''
}
return TYPE_ICON_BASE_PATH + config.icon
})
const displayTitle = computed<string>(() => {
if (props.title != '') {
return props.title
}
return typeConfig.value?.title ?? '暂无数据'
})
const wrapperStyle = computed<any>(() => {
return props.customStyle ?? {}
})
const showIcon = computed<boolean>(() => {
return iconSrc.value != ''
})
const showTitle = computed<boolean>(() => {
return displayTitle.value != ''
})
const showDescription = computed<boolean>(() => {
return props.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>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!