PageTabs.uvue
4.69 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<template>
<view class="tabs-component flex">
<!-- 背景层:替代 ::after 伪元素,App 端兼容 -->
<!-- <view class="tabs-bg"></view> -->
<view
v-for="(item, index) in list"
:class="['tab flex', size, { active: activeIndex == index }]"
:key="index"
@click="changeTab(index)"
class="tab-item"
>
<text class="tab-text">{{ getTabText(item) }}</text>
<view v-if="showDot(item)" class="dot"></view>
</view>
<view
class="active-btn"
:style="{
width: blockWidth,
left: activeBlockLeft
}"
></view>
</view>
</template>
<script setup lang="uts">
import { ref, computed, watch } from "vue";
/** Tab 项:对象或字符串 */
type TabItem = UTSJSONObject | string;
const props = withDefaults(
defineProps<{
/** Tab 数据列表 */
list?: TabItem[]
/** 默认选中索引 */
defaultIndex?: number
/** 显示名称的字段 key,默认 text */
tabKey?: string
/** 尺寸:normal | small */
size?: string
}>(),
{
list: () => [],
defaultIndex: 0,
tabKey: "text",
size: "normal"
}
);
const emit = defineEmits<{
changeTab: [number]
}>();
const activeIndex = ref(props.defaultIndex != null ? props.defaultIndex : 0);
/** 用 computed 包装 defaultIndex,避免 watch getter 在 UTS 下的类型推断问题 */
const defaultIndexSource = computed((): number => {
const v = props.defaultIndex;
return v != null ? v : 0;
});
watch(
defaultIndexSource,
(newVal: number) => {
activeIndex.value = newVal;
},
{ immediate: true }
);
/** 每个 tab 的宽度(百分比,自适应) */
const blockWidth = computed((): string => {
const len = props.list != null ? props.list.length : 0;
if (len <= 0) {
return "100%";
}
const pct = 100 / len;
return `${pct}%`;
});
/** 激活滑块 left 位置:按 tab 等分,统一用 % 保证 transition 在所有切换时生效(px 与 % 混用会导致过渡失效) */
const activeBlockLeft = computed((): string => {
const len = props.list != null ? props.list.length : 0;
if (len <= 0) {
return "0%";
}
const idx = activeIndex.value;
const pct = (idx * 100) / len;
return `${pct}%`;
});
/** 获取 tab 显示文本 */
function getTabText(item: TabItem): string {
if (item == null) {
return "";
}
if (typeof item == "string") {
return item;
}
const obj = item as UTSJSONObject;
const key = props.tabKey != null ? props.tabKey : "text";
const val = obj[key];
if (val == null) {
return "";
}
return `${val}`;
}
/** 是否显示红点:支持 dot 为 boolean 或可调用函数 */
function showDot(item: TabItem): boolean {
if (item == null) {
return false;
}
if (typeof item == "string") {
return false;
}
const obj = item as UTSJSONObject;
const dot = obj["dot"];
if (dot == null) {
return false;
}
if (typeof dot == "boolean") {
return dot;
}
return false;
}
function changeTab(index: number): void {
activeIndex.value = index;
emit("changeTab", index);
}
</script>
<style lang="scss">
/* 遵循 UniAppX 规范:px 单位、无 calc/伪元素、font-weight 仅 400|700、文字样式在 text 上 */
.tabs-component {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
width: 100%;
height: 100%;
min-height: 28px;
background-color: #f5f6f7;
border-radius: 8px;
position: relative;
box-sizing: border-box;
padding: 2px;
}
// .tabs-bg {
// position: absolute;
// z-index: 0;
// left: 0;
// top: 0;
// width: 100%;
// height: 100%;
// background-color: #f5f6f7;
// border-radius: 8px;
// }
.tab-item {
flex: 1;
height: 100%;
min-height: 28px;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
color: #666666;
z-index: 10;
position: relative;
}
.tab-item .tab-text {
font-size: 14px;
font-weight: 400;
color: #666666;
text-align: center;
}
.tab-item.normal .tab-text {
font-size: 14px;
}
.tab-item.small .tab-text {
font-size: 12px;
}
/* 仅选中态加粗 */
.tab-item.active .tab-text {
color: #1a1a1a;
}
.tab-item .dot {
position: absolute;
top: 2px;
right: 14px;
width: 6px;
height: 6px;
border-radius: 3px;
background-color: #f24724;
}
/* 选中态滑块:宽度与 tab 等分,高度自适应,白色背景,滑动过渡 */
.active-btn {
position: absolute;
top: 2px;
bottom: 2px;
opacity: 1;
background-color: #ffffff;
/* 优化滑动:Material Design 缓动 + 略长时长 + GPU 加速 */
transition: left 0.28s cubic-bezier(0.4, 0, 0.2, 1);
transform: translateZ(0);
border-radius: 8px;
z-index: 1;
box-sizing: border-box;
font-weight: 700;
margin-left: 2px;
}
</style>