STab.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 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";
  2. import { n as ye } from "./index2.js";
  3. 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" }, {
  4. __name: "STab",
  5. props: {
  6. // 用法一的数据源:[{ label, value }];字段名可用 tabKey 改
  7. tabs: {
  8. type: Array,
  9. default: () => []
  10. },
  11. // 当前选中值(v-model:activeKey)
  12. activeKey: {
  13. type: [String, Number],
  14. default: ""
  15. },
  16. // 默认选中值:activeKey 未传(或传空)时用它;它也没传就自动选中第一项
  17. defaultActiveKey: {
  18. type: [String, Number],
  19. default: ""
  20. },
  21. // 标签字段映射,默认 { label: 'label', value: 'value' }
  22. tabKey: {
  23. type: Object,
  24. default: () => ({ label: "label", value: "value" })
  25. },
  26. // 已到首个/末个标签但仍可滚动时,一次滚多少屏
  27. stepRatio: {
  28. type: Number,
  29. default: 0.6
  30. }
  31. },
  32. emits: ["update:activeKey", "change"],
  33. setup(Y, { expose: q, emit: U }) {
  34. 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);
  35. function s(e, t) {
  36. const n = e && e[Q.value];
  37. return n == null ? t : n;
  38. }
  39. function ee(e) {
  40. var n, a;
  41. if (!e) return "";
  42. const t = e[Z.value];
  43. return t == null ? (a = (n = e.label) != null ? n : e.tab) != null ? a : "" : t;
  44. }
  45. const C = (e) => e === "" || e === void 0 || e === null;
  46. function te() {
  47. if (!D.value && d.value.length) return s(d.value[0], 0);
  48. const e = f();
  49. return e.length ? e[0].dataset.value : "";
  50. }
  51. function ne() {
  52. return C(u.defaultActiveKey) ? te() : u.defaultActiveKey;
  53. }
  54. function A() {
  55. if (!C(l.value)) return !1;
  56. const e = ne();
  57. return C(e) ? !1 : (l.value = e, !0);
  58. }
  59. function f() {
  60. const e = N.value;
  61. return e ? Array.from(e.querySelectorAll(".s-tab-item")) : [];
  62. }
  63. function m() {
  64. return f().findIndex((e) => e.dataset.value === String(l.value));
  65. }
  66. function w() {
  67. const e = r.value;
  68. e && (g.value = e.scrollWidth - e.clientWidth > 1, O.value = e.scrollLeft > 1, P.value = e.scrollLeft + e.clientWidth < e.scrollWidth - 1);
  69. }
  70. function le(e) {
  71. const t = r.value;
  72. if (!t || !e) return;
  73. const n = t.getBoundingClientRect(), a = e.getBoundingClientRect();
  74. let o = null;
  75. 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" });
  76. }
  77. function v() {
  78. const e = m();
  79. e >= 0 && le(f()[e]);
  80. }
  81. function W(e) {
  82. const t = r.value;
  83. t && t.scrollBy({ left: e * t.clientWidth * u.stepRatio, behavior: "smooth" });
  84. }
  85. function E(e) {
  86. if (e === l.value) return;
  87. l.value = e, y("update:activeKey", e);
  88. const t = d.value.findIndex((n, a) => s(n, a) === e);
  89. y("change", e, t >= 0 ? d.value[t] : void 0), K(v);
  90. }
  91. function T() {
  92. const e = f(), t = m();
  93. if (t > 0) {
  94. E(e[t - 1].dataset.value);
  95. return;
  96. }
  97. W(-1);
  98. }
  99. function X() {
  100. const e = f(), t = m();
  101. if (t >= 0 && t < e.length - 1) {
  102. E(e[t + 1].dataset.value);
  103. return;
  104. }
  105. W(1);
  106. }
  107. function ae(e) {
  108. const t = r.value;
  109. if (!t || !g.value) return;
  110. const n = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY;
  111. t.scrollLeft += n;
  112. }
  113. let i = null, S = !1;
  114. function re(e) {
  115. 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));
  116. }
  117. function B(e) {
  118. var n;
  119. if (!i || e.pointerId !== i.id) return;
  120. const t = r.value;
  121. if (t) {
  122. if (!i.moved) {
  123. if (Math.abs(e.clientX - i.startX) < Ke) return;
  124. i.moved = !0, (n = t.setPointerCapture) == null || n.call(t, e.pointerId);
  125. }
  126. t.scrollLeft -= e.clientX - i.lastX, i.lastX = e.clientX;
  127. }
  128. }
  129. function x() {
  130. var a;
  131. if (I(), !i) return;
  132. const e = r.value, { moved: t, id: n } = i;
  133. i = null, R.value = !1, (a = e == null ? void 0 : e.hasPointerCapture) != null && a.call(e, n) && e.releasePointerCapture(n), t && (S = !0);
  134. }
  135. function I() {
  136. window.removeEventListener("pointermove", B, !0), window.removeEventListener("pointerup", x, !0), window.removeEventListener("pointercancel", x, !0);
  137. }
  138. function oe(e) {
  139. S && (S = !1, e.preventDefault(), e.stopPropagation());
  140. }
  141. const ie = h(() => m() > 0 || O.value), ue = h(() => {
  142. const e = m();
  143. return e >= 0 && e < f().length - 1 || P.value;
  144. });
  145. ce("sTabContext", {
  146. activeValue: l,
  147. select: E,
  148. prev: T,
  149. next: X
  150. }), M(
  151. () => u.activeKey,
  152. (e) => {
  153. if (e !== l.value) {
  154. if (C(e)) {
  155. A() && y("update:activeKey", l.value), K(v);
  156. return;
  157. }
  158. l.value = e, K(v);
  159. }
  160. }
  161. ), M(
  162. () => d.value,
  163. () => {
  164. K(() => {
  165. w(), A() && y("update:activeKey", l.value), v();
  166. });
  167. },
  168. { deep: !0 }
  169. );
  170. let p = null;
  171. return de(() => {
  172. K(() => {
  173. w(), A() && y("update:activeKey", l.value), v();
  174. }), window.ResizeObserver && r.value && (p = new ResizeObserver(() => w()), p.observe(r.value), r.value.firstElementChild && p.observe(r.value.firstElementChild));
  175. }), fe(() => {
  176. I(), p && (p.disconnect(), p = null);
  177. }), q({ syncArrow: w, scrollToActive: v, scrollRef: r }), (e, t) => {
  178. const n = V("left-outlined"), a = V("right-outlined");
  179. return k(), _("div", {
  180. class: $(["s-tab", { dragging: R.value }])
  181. }, [
  182. g.value ? (k(), _("button", {
  183. key: 0,
  184. type: "link",
  185. class: "s-tab-arrow prev",
  186. disabled: !ie.value,
  187. title: e.$l("上一个"),
  188. onClick: T
  189. }, [
  190. z(n)
  191. ], 8, ge)) : F("", !0),
  192. j("div", {
  193. ref_key: "scrollRef",
  194. ref: r,
  195. class: "s-tab-scroll",
  196. role: "tablist",
  197. onScroll: w,
  198. onWheel: L(ae, ["prevent"]),
  199. onPointerdown: re,
  200. onDragstart: t[0] || (t[0] = L(() => {
  201. }, ["prevent"])),
  202. onClickCapture: oe
  203. }, [
  204. j("div", {
  205. ref_key: "trackRef",
  206. ref: N,
  207. class: "s-tab-track"
  208. }, [
  209. D.value ? he(e.$slots, "default", {}, void 0, !0, 1) : (k(!0), _(ve, { key: 0 }, pe(d.value, (o, b) => (k(), _("a", {
  210. key: s(o, b),
  211. class: $(["s-tab-item", { active: s(o, b) === l.value }]),
  212. "data-value": s(o, b),
  213. role: "tab",
  214. "aria-selected": s(o, b) === l.value,
  215. tabindex: s(o, b) === l.value ? 0 : -1,
  216. onClick: (_e) => E(s(o, b)),
  217. onKeydown: [
  218. G(L(T, ["prevent"]), ["left"]),
  219. G(L(X, ["prevent"]), ["right"])
  220. ]
  221. }, be(ee(o)), 43, me))), 128))
  222. ], 512)
  223. ], 544),
  224. g.value ? (k(), _("button", {
  225. key: 1,
  226. type: "link",
  227. class: "s-tab-arrow next",
  228. disabled: !ue.value,
  229. title: e.$l("下一个"),
  230. onClick: X
  231. }, [
  232. z(a)
  233. ], 8, we)) : F("", !0)
  234. ], 2);
  235. };
  236. }
  237. }), Se = /* @__PURE__ */ ye(ke, [["__scopeId", "data-v-ac556582"]]);
  238. export {
  239. Se as default
  240. };