index.vue
5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<template>
<el-row align="top"
class="description-wrapper"
style="width:100%;overflow:hidden;flex-wrap: wrap"
type="flex"
:gutter="gutter">
<template v-for="item in getSchema">
<el-col v-if="!isHideSchemaItem(item)"
:key="item._key"
:span="item.col || 12">
<template v-if="item.divider">
<el-divider v-bind="item.dividerProps"></el-divider>
</template>
<template v-else>
<slot :name="item.slot || item.name ">
<div class="form-item" :style="item.itemStyle || {}">
<div class="label text-hide"
:style="{ width: item.labelWidth || '150px' }"
:title="item.label">
<slot :name="`${item.slot || item.name}-label`">
{{ item.label }}{{ item.hideColon ? "" : ":" }}
</slot>
</div>
<div class="value">
<el-tooltip :disabled="!item.tooltip"
v-bind="item.tooltipProps">
<template #content>
<template v-if="item.tooltipProps.render">
<RenderProxy :render="item.tooltipProps.render"
:row="{ data, schemaItem: item, schema }" />
</template>
<template v-else>
{{ isZeroOrNumber(getValue(item.descKey || item.name, item)) }}
</template>
</template>
<div :class="item.parentProps.className" :style="{ width: item.valueWidth, ...item.parentProps.style }">
<template v-if="item.render">
<RenderProxy :render="item.render"
:row="{ data, schemaItem: item, schema }" />
</template>
<template v-else>
<slot :name="`${item.slot || item.name}-value`">
{{ isZeroOrNumber(getValue(item.descKey || item.name, item)) }}
</slot>
</template>
</div>
</el-tooltip>
</div>
</div>
</slot>
</template>
</el-col>
</template>
</el-row>
</template>
<script>
import { defaults, 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: {
// 全局colon, 优先级小于 schema中的hideColon 大于默认值
hideColon: {
type: Boolean,
default: false,
},
gutter: {
type: Number,
default: 0,
},
// schema = {
// "hideColon": false, // 是否隐藏colon , 默认false
// "name": "", // 唯一key , 数据的键值
// "label": "", // 文案
// "labelAlign": "right", // 'right' | 'left' | 'center' 默认right
// "col": 24, // 占24分之几默认24
// "valueWidth": "100%", // 值的宽度,默认为100%
// "labelWidth": "150px", // 标签宽度,默认为150px
// "slot": "", // 整体的插槽名,优先级高于name
// "itemStyle": {}, // 每一项的样式
// "tooltip": false, // 是否用tooltip 展示data数据
// "tooltipProps": {}, // el-tooltip 的传参
// "render": null, // 自定义渲染 ?:(h,{ data,schemaItem,schema } )=>{}
// "isHide": false, // 是否隐藏 (data)=>boolean || boolean
// "divider ":false, // true 为分割线, 不用设置其他属性
// "dividerProps": {}, // el-divider 的传参
// "defaultValue": "", // data里没有数据时, 展示默认值
// "meta" : {}// 存储其他的数据
// "parentProps": {
// "className": "text-hide" // 默认
// }
// }
schema: {
type: Array,
default: () => ([]),
},
data: {
type: Object,
default: () => ({}),
},
},
computed: {
getSchema() {
return this.schema.map(item => ({
...item,
hideColon: item.hideColon ?? this.hideColon ?? false,
divider: item.divider || false,
dividerProps: item.dividerProps || {},
defaultValue: item.defaultValue || "",
meta: item.meta || {},
labelAlign: item.labelAlign || "right",
col: item.col ?? 24,
labelWidth: item.labelWidth || "150px",
valueWidth: item.valueWidth || "100%",
parentProps: defaults(item.parentProps || {}, { className: "text-hide" }),
tooltipProps: defaults(item.tooltipProps || {}, { defaultClassName: "text-hide" }),
_key: nanoid(),
}));
},
},
methods: {
isZeroOrNumber,
getValue(key, schemaItem) {
return get(this.data, key) || schemaItem.defaultValue;
},
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">
@import "~/src/assets/style/defaultElementUI/el-tag.scss";
.description-wrapper {
font-size: 14px;
::v-deep .el-tag {
width: auto;
}
.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;
flex-basis: 0;
overflow: hidden;
}
}
}
</style>