276 lines
9.2 KiB
JavaScript
Raw Normal View History

2025-12-19 09:46:56 +08:00
"use strict";
const common_vendor = require("../../../../common/vendor.js");
const _sfc_main = {
name: "u-tabs",
2026-05-28 09:48:42 +08:00
mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props$6],
2025-12-19 09:46:56 +08:00
data() {
return {
2026-05-28 09:48:42 +08:00
tabList: [],
2025-12-19 09:46:56 +08:00
scrollLeft: 0,
scrollViewWidth: 0,
lineOffsetLeft: 0,
2026-05-28 09:48:42 +08:00
lineShow: false,
2025-12-19 09:46:56 +08:00
tabsRect: {
left: 0
},
innerCurrent: 0,
moving: false
};
},
watch: {
current: {
immediate: true,
handler(newValue, oldValue) {
if (newValue !== this.innerCurrent) {
if (typeof newValue == "string") {
this.innerCurrent = parseInt(newValue);
} else {
this.innerCurrent = newValue;
}
this.$nextTick(() => {
this.resize();
});
}
}
},
// list变化时重新渲染list各项信息
2026-05-28 09:48:42 +08:00
list: {
handler(newValue, oldValue) {
this.tabList = common_vendor.deepClone(newValue);
this.$nextTick(() => {
this.resize();
});
},
immediate: true,
deep: true
2025-12-19 09:46:56 +08:00
}
},
computed: {
textStyle() {
return (index) => {
const style = {};
const customeStyle = index == this.innerCurrent ? common_vendor.addStyle(this.activeStyle) : common_vendor.addStyle(this.inactiveStyle);
2026-05-28 09:48:42 +08:00
if (this.tabList[index].disabled) {
2025-12-19 09:46:56 +08:00
style.color = "#c8c9cc";
}
return common_vendor.deepMerge(customeStyle, style);
};
},
propsBadge() {
2026-05-28 09:48:42 +08:00
return common_vendor.props$7.badge;
2025-12-19 09:46:56 +08:00
}
},
async mounted() {
this.init();
2026-05-28 09:48:42 +08:00
this.windowResizeCallback = (res) => {
this.init();
};
common_vendor.index.onWindowResize(this.windowResizeCallback);
},
beforeUnmount() {
common_vendor.index.offWindowResize(this.windowResizeCallback);
2025-12-19 09:46:56 +08:00
},
emits: ["click", "longPress", "change", "update:current"],
methods: {
addStyle: common_vendor.addStyle,
addUnit: common_vendor.addUnit,
setLineLeft() {
2026-05-28 09:48:42 +08:00
const tabItem = this.tabList[this.innerCurrent];
2025-12-19 09:46:56 +08:00
if (!tabItem) {
return;
}
2026-05-28 09:48:42 +08:00
let lineOffsetLeft = this.tabList.slice(0, this.innerCurrent).reduce((total, curr) => total + curr.rect.width, 0);
2025-12-19 09:46:56 +08:00
const lineWidth = common_vendor.getPx(this.lineWidth);
this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - lineWidth) / 2;
2026-05-28 09:48:42 +08:00
if (!this.lineShow)
this.lineShow = true;
2025-12-19 09:46:56 +08:00
},
// nvue下设置滑块的位置
animation(x, duration = 0) {
},
// 点击某一个标签
clickHandler(item, index) {
this.$emit("click", {
...item,
index
}, index);
if (item.disabled)
return;
2026-05-28 09:48:42 +08:00
if (this.innerCurrent == index)
return;
2025-12-19 09:46:56 +08:00
this.innerCurrent = index;
2026-05-28 09:48:42 +08:00
this.$nextTick(() => {
this.resize();
});
2025-12-19 09:46:56 +08:00
this.$emit("update:current", index);
this.$emit("change", {
...item,
index
}, index);
},
// 长按事件
longPressHandler(item, index) {
this.$emit("longPress", {
...item,
index
});
},
init() {
common_vendor.sleep().then(() => {
this.resize();
});
},
setScrollLeft() {
if (this.innerCurrent < 0) {
this.innerCurrent = 0;
}
2026-05-28 09:48:42 +08:00
const tabRect = this.tabList[this.innerCurrent];
const offsetLeft = this.tabList.slice(0, this.innerCurrent).reduce((total, curr) => {
2025-12-19 09:46:56 +08:00
return total + curr.rect.width;
}, 0);
const windowWidth = common_vendor.getWindowInfo().windowWidth;
let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect.right) / 2 + this.tabsRect.left / 2;
scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width);
this.scrollLeft = Math.max(0, scrollLeft);
},
// 获取所有标签的尺寸
resize() {
2026-05-28 09:48:42 +08:00
if (this.tabList.length === 0) {
2025-12-19 09:46:56 +08:00
return;
}
Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(([tabsRect, itemRect = []]) => {
if (tabsRect.left > tabsRect.width) {
tabsRect.right = tabsRect.right - Math.floor(tabsRect.left / tabsRect.width) * tabsRect.width;
tabsRect.left = tabsRect.left % tabsRect.width;
}
this.tabsRect = tabsRect;
this.scrollViewWidth = 0;
itemRect.map((item, index) => {
this.scrollViewWidth += item.width;
2026-05-28 09:48:42 +08:00
this.tabList[index].rect = item;
2025-12-19 09:46:56 +08:00
});
this.setLineLeft();
this.setScrollLeft();
});
},
// 获取导航菜单的尺寸
getTabsRect() {
return new Promise((resolve) => {
this.queryRect("u-tabs__wrapper__scroll-view").then((size) => resolve(size));
});
},
// 获取所有标签的尺寸
getAllItemRect() {
return new Promise((resolve) => {
2026-05-28 09:48:42 +08:00
const promiseAllArr = this.tabList.map((item, index) => this.queryRect(
2025-12-19 09:46:56 +08:00
`u-tabs__wrapper__nav__item-${index}`,
true
));
Promise.all(promiseAllArr).then((sizes) => resolve(sizes));
});
},
// 获取各个标签的尺寸
queryRect(el, item) {
return new Promise((resolve) => {
this.$uGetRect(`.${el}`).then((size) => {
resolve(size);
});
});
}
}
};
if (!Array) {
2026-05-28 09:48:42 +08:00
const _easycom_up_icon2 = common_vendor.resolveComponent("up-icon");
2025-12-19 09:46:56 +08:00
const _easycom_u_badge2 = common_vendor.resolveComponent("u-badge");
2026-05-28 09:48:42 +08:00
(_easycom_up_icon2 + _easycom_u_badge2)();
2025-12-19 09:46:56 +08:00
}
2026-05-28 09:48:42 +08:00
const _easycom_up_icon = () => "../u-icon/u-icon.js";
2025-12-19 09:46:56 +08:00
const _easycom_u_badge = () => "../u-badge/u-badge.js";
if (!Math) {
2026-05-28 09:48:42 +08:00
(_easycom_up_icon + _easycom_u_badge)();
2025-12-19 09:46:56 +08:00
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {
2026-05-28 09:48:42 +08:00
a: common_vendor.f($data.tabList, (item, index, i0) => {
return common_vendor.e(_ctx.$slots.icon ? {
a: "icon-" + i0,
b: common_vendor.r("icon", {
item,
keyName: _ctx.keyName,
index
}, i0)
} : common_vendor.e({
c: item.icon
}, item.icon ? {
d: "0546c3e4-0-" + i0,
e: common_vendor.p({
name: item.icon,
customStyle: $options.addStyle(_ctx.iconStyle)
})
} : {}), _ctx.$slots.content ? {
f: "content-" + i0,
g: common_vendor.r("content", {
2025-12-19 09:46:56 +08:00
item,
keyName: _ctx.keyName,
index
}, i0)
} : !_ctx.$slots.content && (_ctx.$slots.default || _ctx.$slots.$default) ? {
2026-05-28 09:48:42 +08:00
h: "d-" + i0,
i: common_vendor.r("d", {
2025-12-19 09:46:56 +08:00
item,
keyName: _ctx.keyName,
index
}, i0)
} : {
2026-05-28 09:48:42 +08:00
j: common_vendor.t(item[_ctx.keyName]),
k: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
l: common_vendor.s($options.textStyle(index))
2025-12-19 09:46:56 +08:00
}, {
2026-05-28 09:48:42 +08:00
m: "0546c3e4-1-" + i0,
n: common_vendor.p({
2025-12-19 09:46:56 +08:00
show: !!(item.badge && (item.badge.show || item.badge.isDot || item.badge.value)),
isDot: item.badge && item.badge.isDot || $options.propsBadge.isDot,
value: item.badge && item.badge.value || $options.propsBadge.value,
max: item.badge && item.badge.max || $options.propsBadge.max,
type: item.badge && item.badge.type || $options.propsBadge.type,
showZero: item.badge && item.badge.showZero || $options.propsBadge.showZero,
bgColor: item.badge && item.badge.bgColor || $options.propsBadge.bgColor,
color: item.badge && item.badge.color || $options.propsBadge.color,
shape: item.badge && item.badge.shape || $options.propsBadge.shape,
numberType: item.badge && item.badge.numberType || $options.propsBadge.numberType,
inverted: item.badge && item.badge.inverted || $options.propsBadge.inverted,
customStyle: "margin-left: 4px;"
}),
2026-05-28 09:48:42 +08:00
o: index,
p: common_vendor.o(($event) => $options.clickHandler(item, index), index),
q: common_vendor.o(($event) => $options.longPressHandler(item, index), index),
r: `u-tabs__wrapper__nav__item-${index}`,
s: common_vendor.n(`u-tabs__wrapper__nav__item-${index}`),
t: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item--disabled"),
v: common_vendor.n($data.innerCurrent == index ? "u-tabs__wrapper__nav__item-active" : "")
2025-12-19 09:46:56 +08:00
});
}),
2026-05-28 09:48:42 +08:00
b: _ctx.$slots.icon,
c: _ctx.$slots.content,
d: !_ctx.$slots.content && (_ctx.$slots.default || _ctx.$slots.$default),
e: common_vendor.s($options.addStyle(_ctx.itemStyle)),
f: common_vendor.s({
2025-12-19 09:46:56 +08:00
flex: _ctx.scrollable ? "" : 1
}),
2026-05-28 09:48:42 +08:00
g: common_vendor.s({
2025-12-19 09:46:56 +08:00
width: $options.addUnit(_ctx.lineWidth),
transform: `translate(${$data.lineOffsetLeft}px)`,
2026-05-28 09:48:42 +08:00
transitionDuration: `${_ctx.duration}ms`,
2025-12-19 09:46:56 +08:00
height: $options.addUnit(_ctx.lineHeight),
background: _ctx.lineColor,
2026-05-28 09:48:42 +08:00
backgroundSize: _ctx.lineBgSize,
display: $data.lineShow ? "block" : "none"
2025-12-19 09:46:56 +08:00
}),
2026-05-28 09:48:42 +08:00
h: _ctx.scrollable,
i: $data.scrollLeft,
j: common_vendor.n(_ctx.customClass)
2025-12-19 09:46:56 +08:00
};
}
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-0546c3e4"], ["__file", "/Users/zsq/Sources/github/2025property-pay/pay-employee/node_modules/uview-plus/components/u-tabs/u-tabs.vue"]]);
wx.createComponent(Component);