Skip to content

Commit 3bb7a9b

Browse files
committed
refactor: 通用化 UsageTable 组件,最多添加至五列,支持动态渲染
1 parent e3b2d49 commit 3bb7a9b

File tree

1 file changed

+110
-28
lines changed

1 file changed

+110
-28
lines changed

.vitepress/theme/components/UsageTable.vue

Lines changed: 110 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,76 @@ import { computed } from 'vue'
44
const props = defineProps<{
55
list: string[][]
66
labels?: {
7-
left?: string
8-
right?: string
7+
first?: string
8+
second?: string
9+
third?: string
10+
fourth?: string
11+
fifth?: string
912
}
1013
colors?: {
11-
inputBg?: string
12-
outputBg?: string
14+
firstBg?: string
15+
secondBg?: string
16+
thirdBg?: string
17+
fourthBg?: string
18+
fifthBg?: string
1319
}
1420
}>()
1521
1622
const renderList = computed(() => props.list)
23+
24+
const hasColumn = (index: number) => props.list.some(item => item[index] !== undefined && item[index] !== null)
1725
</script>
1826

1927
<template>
2028
<div class="usage-table-container">
2129
<div class="wrapper">
22-
<!-- 左侧区域 -->
23-
<div class="section-input">
24-
<!-- 自定义左侧标题 -->
30+
<!-- 第一列 -->
31+
<div v-if="hasColumn(0)" class="section-first">
2532
<div class="title">
26-
{{ labels?.left || 'Input' }}
33+
{{ labels?.first || 'First' }}
2734
</div>
28-
<div v-for="(item, index) in renderList" :key="index" class="input" :style="{ backgroundColor: props.colors?.inputBg }" v-html="item[0]" />
35+
<div v-for="(item, index) in renderList" :key="index" class="first" :style="{ backgroundColor: props.colors?.firstBg }" v-html="item[0]" />
2936
</div>
3037

31-
<!-- 右侧区域 -->
32-
<div class="section-output">
33-
<!-- 自定义右侧标题 -->
38+
<!-- 第二列 -->
39+
<div v-if="hasColumn(1)" class="section-second">
3440
<div class="title">
35-
{{ labels?.right || 'Output' }}
41+
{{ labels?.second || 'Second' }}
3642
</div>
37-
<div v-for="(item, index) in renderList" :key="index" class="output" :style="{ backgroundColor: props.colors?.outputBg }">
43+
<div v-for="(item, index) in renderList" :key="index" class="second" :style="{ backgroundColor: props.colors?.secondBg }">
3844
{{ item[1] }}
3945
</div>
4046
</div>
47+
48+
<!-- 第三列 -->
49+
<div v-if="hasColumn(2)" class="section-third">
50+
<div class="title">
51+
{{ labels?.third || 'Third' }}
52+
</div>
53+
<div v-for="(item, index) in renderList" :key="index" class="third" :style="{ backgroundColor: props.colors?.thirdBg }">
54+
{{ item[2] }}
55+
</div>
56+
</div>
57+
58+
<!-- 第四列 -->
59+
<div v-if="hasColumn(3)" class="section-fourth">
60+
<div class="title">
61+
{{ labels?.fourth || 'Fourth' }}
62+
</div>
63+
<div v-for="(item, index) in renderList" :key="index" class="fourth" :style="{ backgroundColor: props.colors?.fourthBg }">
64+
{{ item[3] }}
65+
</div>
66+
</div>
67+
68+
<!-- 第五列 -->
69+
<div v-if="hasColumn(4)" class="section-fifth">
70+
<div class="title">
71+
{{ labels?.fifth || 'Fifth' }}
72+
</div>
73+
<div v-for="(item, index) in renderList" :key="index" class="fifth" :style="{ backgroundColor: props.colors?.fifthBg }">
74+
{{ item[4] }}
75+
</div>
76+
</div>
4177
</div>
4278
</div>
4379
</template>
@@ -65,27 +101,46 @@ const renderList = computed(() => props.list)
65101
height: auto;
66102
}
67103
68-
.section-input .input:not(:last-child),
69-
.section-output .output:not(:last-child){
104+
.wrapper > div {
105+
flex: 1;
106+
}
107+
108+
.section-first .first:not(:last-child),
109+
.section-second .second:not(:last-child),
110+
.section-third .third:not(:last-child),
111+
.section-fourth .fourth:not(:last-child),
112+
.section-fifth .fifth:not(:last-child)
113+
{
70114
/* margin-bottom: 0.5rem; */
71115
padding-bottom: 0.5rem;
72116
border-bottom: 3px solid var(--glb-customtable-c-border);
73117
}
74118
75-
.section-input .title {background-color: var(--glb-customtable-title-c-bg-lv1)}
76-
.section-output .title {background-color: var(--glb-customtable-title-c-bg-lv2)}
119+
.section-first .title {background-color: var(--glb-customtable-title-c-bg-lv0)}
120+
.section-second .title {background-color: var(--glb-customtable-title-c-bg-lv1)}
121+
.section-third .title {background-color: var(--glb-customtable-title-c-bg-lv2)}
122+
.section-fourth .title {background-color: var(--glb-customtable-title-c-bg-lv3)}
123+
.section-fifth .title {background-color: var(--glb-customtable-title-c-bg-lv4)}
77124
78-
.section-input,
79-
.section-output{
125+
.section-first,
126+
.section-second,
127+
.section-third,
128+
.section-fourth,
129+
.section-fifth
130+
{
80131
display: flex;
81132
flex-direction: column;
82133
flex: 1; /* 平均分配宽度 */
83134
min-height: 0; /* 允许内容溢出时正常拉伸 */
84135
overflow: auto;
85136
}
86137
87-
.input,
88-
.output{
138+
.first,
139+
.second,
140+
.third,
141+
.fourth,
142+
.fifth
143+
{
89144
flex: 1; /* 让内容撑开容器并保持高度一致 */
90145
display: flex;
91146
flex-direction: column;
@@ -94,21 +149,48 @@ const renderList = computed(() => props.list)
94149
overflow: auto; /* 内容溢出时显示滚动条 */
95150
}
96151
97-
.wrapper .input {
152+
.wrapper .first {
153+
flex: 2;
154+
background-color: var(--glb-customtable-content-bg-lv0);
155+
color: var(--vp-c-text-1);
156+
padding: 1rem;
157+
font-family: sans-serif;
158+
white-space: pre-wrap;
159+
}
160+
161+
.wrapper .second {
162+
flex: 2;
163+
background-color: var(--glb-customtable-content-bg-lv1);
164+
color: var(--vp-c-text-1);
165+
padding: 1rem;
166+
font-family: sans-serif;
167+
white-space: pre-wrap;
168+
}
169+
170+
.wrapper .third {
171+
flex: 2;
172+
background-color: var(--glb-customtable-content-bg-lv2);
173+
color: var(--vp-c-text-1);
174+
padding: 1rem;
175+
font-family: sans-serif;
176+
white-space: pre-wrap;
177+
}
178+
179+
.wrapper .fourth {
98180
flex: 2;
99-
background-color: var(--usagetable-c-bg-correct);
181+
background-color: var(--glb-customtable-content-bg-lv3);
100182
color: var(--vp-c-text-1);
101183
padding: 1rem;
102-
font-family: monospace;
184+
font-family: sans-serif;
103185
white-space: pre-wrap;
104186
}
105187
106-
.wrapper .output {
188+
.wrapper .fifth {
107189
flex: 2;
108-
background-color: var(--usagetable-c-bg-wrong);
190+
background-color: var(--glb-customtable-content-bg-lv4);
109191
color: var(--vp-c-text-1);
110192
padding: 1rem;
111-
font-family: monospace;
193+
font-family: sans-serif;
112194
white-space: pre-wrap;
113195
}
114196
</style>

0 commit comments

Comments
 (0)