Commit fccefd06 by Jaxon

feat: 新增通用描述列表组件及工具函数

- 新增 src/components/descriptions/index.vue 组件,支持通过 schema 配置动态渲染描述列表
- 新增 src/libs/number.js 工具函数,用于处理数值和零值的显示逻辑
- 组件支持插槽、自定义渲染、tooltip 提示和条件隐藏等功能
1 parent 54213a74
<template>
<el-row class="description-wrapper">
<template v-for="item in schema">
<el-col v-if="!isHideSchemaItem(item)"
:key="item._key"
:span="item.col || 12">
<slot :name="item.slot || item.name ">
<div class="form-item" :style="item.itemStyle || {}">
<span class="label" :style="{ width: item.labelWidth || '150px' }">
<slot :name="`${item.slot || item.name}-label`">
{{ item.label }}{{ item.hideColon ? "" : ":" }}
</slot>
</span>
<span class="value">
<div class="text-hide" :style="{ width: item.valueWidth || '100%' }">
<template v-if="item.render">
<RenderProxy :render="item.render"
:row="{ data, schemaItem: item, schema }" />
</template>
<template v-else>
<template v-if="item.tooltip && (getValue(item.descKey || item.name))">
<el-tooltip :content="isZeroOrNumber(getValue(item.descKey || item.name)) " v-bind="item.tooltipProps">
<div class="text-hide">
{{ isZeroOrNumber(getValue(item.descKey || item.name)) }}
</div>
</el-tooltip>
</template>
<template v-else>
<slot :name="`${item.slot || item.name}-value`">
{{ isZeroOrNumber(getValue(item.descKey || item.name)) }}
</slot>
</template>
</template>
</div>
</span>
</div>
</slot>
</el-col>
</template>
</el-row>
</template>
<script>
import { get, isFunction } from "lodash-es";
import { nanoid } from "nanoid";
import RenderProxy from "@/components/renderProxy/index.vue";
import { isZeroOrNumber } from "@/libs/number.js";
export default {
name: "Descriptions",
components: {
RenderProxy,
},
props: {
// hideColon 是否隐藏colon , 默认false
// name : - 唯一key , 数据的键值
// label : 文案
// col : 占24分之几
// valueWidth : 值的宽度,默认为100%
// labelWidth : 标签宽度,默认为150px
// slot : 整体的插槽名,优先级高于name
// itemStyle: 每一项的样式
// tooltip: boolean 是否用tooltip 展示data数据
// tooltipProps: el-tooltip 的传参
// render ?:(h,{ data,schemaItem,schema } )=>{} 自定义渲染
// isHide 是否隐藏 (data)=>boolean || boolean
schema: {
type: Array,
default: () => ([]),
},
data: {
type: Object,
default: () => ({}),
},
},
computed: {
getSchema() {
return this.schema.map(item => ({
...item,
_key: nanoid(),
}));
},
},
methods: {
isZeroOrNumber,
getValue(key) {
return get(this.data, key);
},
isHideSchemaItem(schemaItem) {
if (isFunction(schemaItem.isHide)) {
return schemaItem.isHide({ formModel: this.data });
}
return schemaItem.isHide;
},
},
render(h, ctx) {
return ctx.props.render(h, ctx.props.row);
},
};
</script>
<style scoped lang="scss">
.description-wrapper {
font-size: 14px;
.form-item {
display: flex;
flex-direction: row;
line-height: 32px;
margin-bottom: 20px;
> .label {
text-align: right;
color: #666666;
font-size: 14px;
flex-shrink: 0;
}
> .value {
flex-shrink: 0;
flex: 1;
overflow: hidden;
}
}
}
</style>
export function isZeroOrNumber(val) {
const valid = (val === 0 || !Number.isNaN(val)) && val !== undefined && val !== null && val !== "";
return valid ? val : "-";
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!