import { useSlots as se, ref as c, computed as h, provide as ce, watch as M, nextTick as K, onMounted as de, onBeforeUnmount as fe, resolveComponent as V, openBlock as k, createElementBlock as _, normalizeClass as $, createVNode as z, createCommentVNode as F, createElementVNode as j, withModifiers as L, Fragment as ve, renderList as pe, withKeys as G, toDisplayString as be, renderSlot as he } from "vue"; import { n as ye } from "./index2.js"; const ge = ["disabled", "title"], me = ["data-value", "aria-selected", "tabindex", "onClick", "onKeydown"], we = ["disabled", "title"], H = 8, Ke = 5, ke = /* @__PURE__ */ Object.assign({ name: "STab" }, { __name: "STab", props: { // 用法一的数据源:[{ label, value }];字段名可用 tabKey 改 tabs: { type: Array, default: () => [] }, // 当前选中值(v-model:activeKey) activeKey: { type: [String, Number], default: "" }, // 默认选中值:activeKey 未传(或传空)时用它;它也没传就自动选中第一项 defaultActiveKey: { type: [String, Number], default: "" }, // 标签字段映射,默认 { label: 'label', value: 'value' } tabKey: { type: Object, default: () => ({ label: "label", value: "value" }) }, // 已到首个/末个标签但仍可滚动时,一次滚多少屏 stepRatio: { type: Number, default: 0.6 } }, emits: ["update:activeKey", "change"], setup(Y, { expose: q, emit: U }) { const u = Y, y = U, J = se(), r = c(null), N = c(null), O = c(!1), P = c(!1), g = c(!1), l = c(u.activeKey), R = c(!1), d = h(() => u.tabs || []), Q = h(() => u.tabKey && u.tabKey.value || "value"), Z = h(() => u.tabKey && u.tabKey.label || "label"), D = h(() => !!J.default); function s(e, t) { const n = e && e[Q.value]; return n == null ? t : n; } function ee(e) { var n, a; if (!e) return ""; const t = e[Z.value]; return t == null ? (a = (n = e.label) != null ? n : e.tab) != null ? a : "" : t; } const C = (e) => e === "" || e === void 0 || e === null; function te() { if (!D.value && d.value.length) return s(d.value[0], 0); const e = f(); return e.length ? e[0].dataset.value : ""; } function ne() { return C(u.defaultActiveKey) ? te() : u.defaultActiveKey; } function A() { if (!C(l.value)) return !1; const e = ne(); return C(e) ? !1 : (l.value = e, !0); } function f() { const e = N.value; return e ? Array.from(e.querySelectorAll(".s-tab-item")) : []; } function m() { return f().findIndex((e) => e.dataset.value === String(l.value)); } function w() { const e = r.value; e && (g.value = e.scrollWidth - e.clientWidth > 1, O.value = e.scrollLeft > 1, P.value = e.scrollLeft + e.clientWidth < e.scrollWidth - 1); } function le(e) { const t = r.value; if (!t || !e) return; const n = t.getBoundingClientRect(), a = e.getBoundingClientRect(); let o = null; a.left < n.left ? o = t.scrollLeft + (a.left - n.left) - H : a.right > n.right && (o = t.scrollLeft + (a.right - n.right) + H), o !== null && t.scrollTo({ left: Math.max(0, o), behavior: "smooth" }); } function v() { const e = m(); e >= 0 && le(f()[e]); } function W(e) { const t = r.value; t && t.scrollBy({ left: e * t.clientWidth * u.stepRatio, behavior: "smooth" }); } function E(e) { if (e === l.value) return; l.value = e, y("update:activeKey", e); const t = d.value.findIndex((n, a) => s(n, a) === e); y("change", e, t >= 0 ? d.value[t] : void 0), K(v); } function T() { const e = f(), t = m(); if (t > 0) { E(e[t - 1].dataset.value); return; } W(-1); } function X() { const e = f(), t = m(); if (t >= 0 && t < e.length - 1) { E(e[t + 1].dataset.value); return; } W(1); } function ae(e) { const t = r.value; if (!t || !g.value) return; const n = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY; t.scrollLeft += n; } let i = null, S = !1; function re(e) { S = !1, !(!r.value || !g.value) && e.pointerType !== "touch" && e.button === 0 && (i = { id: e.pointerId, startX: e.clientX, lastX: e.clientX, moved: !1 }, R.value = !0, window.addEventListener("pointermove", B, !0), window.addEventListener("pointerup", x, !0), window.addEventListener("pointercancel", x, !0)); } function B(e) { var n; if (!i || e.pointerId !== i.id) return; const t = r.value; if (t) { if (!i.moved) { if (Math.abs(e.clientX - i.startX) < Ke) return; i.moved = !0, (n = t.setPointerCapture) == null || n.call(t, e.pointerId); } t.scrollLeft -= e.clientX - i.lastX, i.lastX = e.clientX; } } function x() { var a; if (I(), !i) return; const e = r.value, { moved: t, id: n } = i; i = null, R.value = !1, (a = e == null ? void 0 : e.hasPointerCapture) != null && a.call(e, n) && e.releasePointerCapture(n), t && (S = !0); } function I() { window.removeEventListener("pointermove", B, !0), window.removeEventListener("pointerup", x, !0), window.removeEventListener("pointercancel", x, !0); } function oe(e) { S && (S = !1, e.preventDefault(), e.stopPropagation()); } const ie = h(() => m() > 0 || O.value), ue = h(() => { const e = m(); return e >= 0 && e < f().length - 1 || P.value; }); ce("sTabContext", { activeValue: l, select: E, prev: T, next: X }), M( () => u.activeKey, (e) => { if (e !== l.value) { if (C(e)) { A() && y("update:activeKey", l.value), K(v); return; } l.value = e, K(v); } } ), M( () => d.value, () => { K(() => { w(), A() && y("update:activeKey", l.value), v(); }); }, { deep: !0 } ); let p = null; return de(() => { K(() => { w(), A() && y("update:activeKey", l.value), v(); }), window.ResizeObserver && r.value && (p = new ResizeObserver(() => w()), p.observe(r.value), r.value.firstElementChild && p.observe(r.value.firstElementChild)); }), fe(() => { I(), p && (p.disconnect(), p = null); }), q({ syncArrow: w, scrollToActive: v, scrollRef: r }), (e, t) => { const n = V("left-outlined"), a = V("right-outlined"); return k(), _("div", { class: $(["s-tab", { dragging: R.value }]) }, [ g.value ? (k(), _("button", { key: 0, type: "link", class: "s-tab-arrow prev", disabled: !ie.value, title: e.$l("上一个"), onClick: T }, [ z(n) ], 8, ge)) : F("", !0), j("div", { ref_key: "scrollRef", ref: r, class: "s-tab-scroll", role: "tablist", onScroll: w, onWheel: L(ae, ["prevent"]), onPointerdown: re, onDragstart: t[0] || (t[0] = L(() => { }, ["prevent"])), onClickCapture: oe }, [ j("div", { ref_key: "trackRef", ref: N, class: "s-tab-track" }, [ D.value ? he(e.$slots, "default", {}, void 0, !0, 1) : (k(!0), _(ve, { key: 0 }, pe(d.value, (o, b) => (k(), _("a", { key: s(o, b), class: $(["s-tab-item", { active: s(o, b) === l.value }]), "data-value": s(o, b), role: "tab", "aria-selected": s(o, b) === l.value, tabindex: s(o, b) === l.value ? 0 : -1, onClick: (_e) => E(s(o, b)), onKeydown: [ G(L(T, ["prevent"]), ["left"]), G(L(X, ["prevent"]), ["right"]) ] }, be(ee(o)), 43, me))), 128)) ], 512) ], 544), g.value ? (k(), _("button", { key: 1, type: "link", class: "s-tab-arrow next", disabled: !ue.value, title: e.$l("下一个"), onClick: X }, [ z(a) ], 8, we)) : F("", !0) ], 2); }; } }), Se = /* @__PURE__ */ ye(ke, [["__scopeId", "data-v-ac556582"]]); export { Se as default };