| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import { useSlots as se, ref as c, computed as g, provide as ce, watch as I, nextTick as k, onMounted as fe, onBeforeUnmount as de, resolveComponent as M, openBlock as _, createElementBlock as C, normalizeClass as V, createVNode as $, createCommentVNode as z, createElementVNode as F, withModifiers as R, Fragment as ve, renderList as pe, withKeys as j, 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"], G = 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(H, { expose: Y, emit: q }) {
- const u = H, m = q, U = se(), r = c(null), X = c(null), N = c(!1), O = c(!1), f = c(!1), l = c(u.activeKey), A = c(!1), d = g(() => u.tabs || []), J = g(() => u.tabKey && u.tabKey.value || "value"), Q = g(() => u.tabKey && u.tabKey.label || "label"), D = g(() => !!U.default);
- function s(e, t) {
- const n = e && e[J.value];
- return n == null ? t : n;
- }
- function Z(e) {
- var n, a;
- if (!e) return "";
- const t = e[Q.value];
- return t == null ? (a = (n = e.label) != null ? n : e.tab) != null ? a : "" : t;
- }
- const S = (e) => e === "" || e === void 0 || e === null;
- function ee() {
- if (!D.value && d.value.length) return s(d.value[0], 0);
- const e = v();
- return e.length ? e[0].dataset.value : "";
- }
- function te() {
- return S(u.defaultActiveKey) ? ee() : u.defaultActiveKey;
- }
- function L() {
- if (!S(l.value)) return !1;
- const e = te();
- return S(e) ? !1 : (l.value = e, !0);
- }
- function v() {
- const e = X.value;
- return e ? Array.from(e.querySelectorAll(".s-tab-item")) : [];
- }
- function w() {
- return v().findIndex((e) => e.dataset.value === String(l.value));
- }
- function K() {
- const e = r.value;
- e && (f.value = e.scrollWidth - e.clientWidth > 1, N.value = e.scrollLeft > 1, O.value = e.scrollLeft + e.clientWidth < e.scrollWidth - 1);
- }
- function ne(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) - G : a.right > n.right && (o = t.scrollLeft + (a.right - n.right) + G), o !== null && t.scrollTo({ left: Math.max(0, o), behavior: "smooth" });
- }
- function p() {
- const e = w();
- e >= 0 && ne(v()[e]);
- }
- function W(e) {
- const t = r.value;
- t && t.scrollBy({ left: e * t.clientWidth * u.stepRatio, behavior: "smooth" });
- }
- function x(e) {
- if (e === l.value) return;
- l.value = e, m("update:activeKey", e);
- const t = d.value.findIndex((n, a) => s(n, a) === e);
- m("change", e, t >= 0 ? d.value[t] : void 0), k(p);
- }
- function P() {
- const e = v(), t = w();
- if (t > 0) {
- x(e[t - 1].dataset.value);
- return;
- }
- W(-1);
- }
- function T() {
- const e = v(), t = w();
- if (t >= 0 && t < e.length - 1) {
- x(e[t + 1].dataset.value);
- return;
- }
- W(1);
- }
- function le(e) {
- const t = r.value;
- if (!t || !f.value) return;
- const n = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY;
- t.scrollLeft += n;
- }
- let i = null, E = !1;
- function ae(e) {
- E = !1, !(!r.value || !f.value) && e.pointerType !== "touch" && e.button === 0 && (i = { id: e.pointerId, startX: e.clientX, lastX: e.clientX, moved: !1 }, window.addEventListener("pointerup", b, !0), window.addEventListener("pointercancel", b, !0));
- }
- function re(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, A.value = !0, (n = t.setPointerCapture) == null || n.call(t, e.pointerId);
- }
- t.scrollLeft -= e.clientX - i.lastX, i.lastX = e.clientX;
- }
- }
- function b() {
- var a;
- if (B(), !i) return;
- const e = r.value, { moved: t, id: n } = i;
- i = null, A.value = !1, (a = e == null ? void 0 : e.hasPointerCapture) != null && a.call(e, n) && e.releasePointerCapture(n), t && (E = !0);
- }
- function B() {
- window.removeEventListener("pointerup", b, !0), window.removeEventListener("pointercancel", b, !0);
- }
- function oe(e) {
- E && (E = !1, e.preventDefault(), e.stopPropagation());
- }
- const ie = g(() => w() > 0 || N.value), ue = g(() => {
- const e = w();
- return e >= 0 && e < v().length - 1 || O.value;
- });
- ce("sTabContext", {
- activeValue: l,
- select: x,
- prev: P,
- next: T
- }), I(
- () => u.activeKey,
- (e) => {
- if (e !== l.value) {
- if (S(e)) {
- L() && m("update:activeKey", l.value), k(p);
- return;
- }
- l.value = e, k(p);
- }
- }
- ), I(
- () => d.value,
- () => {
- k(() => {
- K(), L() && m("update:activeKey", l.value), p();
- });
- },
- { deep: !0 }
- );
- let h = null;
- return fe(() => {
- k(() => {
- K(), L() && m("update:activeKey", l.value), p();
- }), window.ResizeObserver && r.value && (h = new ResizeObserver(() => K()), h.observe(r.value), r.value.firstElementChild && h.observe(r.value.firstElementChild));
- }), de(() => {
- B(), h && (h.disconnect(), h = null);
- }), Y({ syncArrow: K, scrollToActive: p, scrollRef: r }), (e, t) => {
- const n = M("left-outlined"), a = M("right-outlined");
- return _(), C("div", {
- class: V(["s-tab", { "is-overflow": f.value, dragging: A.value }])
- }, [
- f.value ? (_(), C("button", {
- key: 0,
- type: "link",
- class: "s-tab-arrow prev",
- disabled: !ie.value,
- title: e.$l("上一个"),
- onClick: P
- }, [
- $(n)
- ], 8, ge)) : z("", !0),
- F("div", {
- ref_key: "scrollRef",
- ref: r,
- class: "s-tab-scroll",
- role: "tablist",
- onScroll: K,
- onWheel: R(le, ["prevent"]),
- onPointerdown: ae,
- onPointermove: re,
- onPointerup: b,
- onPointercancel: b,
- onDragstart: t[0] || (t[0] = R(() => {
- }, ["prevent"])),
- onClickCapture: oe
- }, [
- F("div", {
- ref_key: "trackRef",
- ref: X,
- class: "s-tab-track"
- }, [
- D.value ? he(e.$slots, "default", {}, void 0, !0, 1) : (_(!0), C(ve, { key: 0 }, pe(d.value, (o, y) => (_(), C("a", {
- key: s(o, y),
- class: V(["s-tab-item", { active: s(o, y) === l.value }]),
- "data-value": s(o, y),
- role: "tab",
- "aria-selected": s(o, y) === l.value,
- tabindex: s(o, y) === l.value ? 0 : -1,
- onClick: (_e) => x(s(o, y)),
- onKeydown: [
- j(R(P, ["prevent"]), ["left"]),
- j(R(T, ["prevent"]), ["right"])
- ]
- }, be(Z(o)), 43, me))), 128))
- ], 512)
- ], 544),
- f.value ? (_(), C("button", {
- key: 1,
- type: "link",
- class: "s-tab-arrow next",
- disabled: !ue.value,
- title: e.$l("下一个"),
- onClick: T
- }, [
- $(a)
- ], 8, we)) : z("", !0)
- ], 2);
- };
- }
- }), xe = /* @__PURE__ */ ye(ke, [["__scopeId", "data-v-328b8dcc"]]);
- export {
- xe as default
- };
|