index.vue
2.33 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
<template>
<div class="tab-section"
:class="{ isSticky }"
:style="{ background: customBackground }">
<div class="tab-slider" :style="tabSliderStyle"></div>
<div v-for="item in normalizedList"
:key="item.value"
class="tab"
:class="{ active: item.value === currentValue }"
@click="handleSelect(item)">
{{ item.label }}
</div>
</div>
</template>
<script>
export default {
name: "DateTypeTabs",
props: {
value: {
type: [String, Number],
default: 0,
},
list: {
type: Array,
default() {
return [
{ label: "日", value: 0 },
{ label: "周", value: 1 },
{ label: "月", value: 2 },
];
},
},
isSticky: Boolean,
customBackground: {
type: String,
default: "",
},
},
computed: {
normalizedList() {
return (this.list || []).map((item, index) => {
const value = item?.value;
const label = item?.label;
return {
...item,
label,
value,
};
});
},
currentValue() {
return this.value;
},
tabSliderStyle() {
const tabCount = this.normalizedList.length || 1;
const activeIndex = this.normalizedList.findIndex(item => item.value === this.currentValue);
const currentIndex = activeIndex > -1 ? activeIndex : 0;
return {
width: `calc((100% - 4px) / ${tabCount})`,
transform: `translateX(${currentIndex * 100}%)`,
};
},
},
methods: {
handleSelect(item) {
if (item.value === this.currentValue)
return;
this.$emit("input", item.value);
this.$emit("change", item);
},
},
};
</script>
<style lang="scss" scoped>
.tab-section {
position: relative;
overflow: hidden;
box-sizing: border-box;
height: 28px;
display: flex;
align-items: center;
background: #ffffff;
border-radius: 28px;
font-size: 12px;
color: #999999;
padding: 2px;
flex-shrink: 0;
.tab-slider {
position: absolute;
top: 2px;
left: 2px;
height: 24px;
border-radius: 24px;
background: #2c66f71a;
transition: transform 0.25s ease;
}
&.isSticky {
background: #f7f8fa;
}
.tab {
position: relative;
z-index: 1;
flex: 1;
text-align: center;
font-weight: 500;
font-size: 12px;
height: 24px;
border-radius: 24px;
transition: color 0.25s ease;
flex-shrink: 0;
box-sizing: border-box;
padding: 4px 0;
&.active {
color: #2c66f7;
font-weight: 500;
}
}
}
</style>