STab.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 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";
  2. import { n as ye } from "./index2.js";
  3. 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" }, {
  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(H, { expose: Y, emit: q }) {
  34. 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);
  35. function s(e, t) {
  36. const n = e && e[J.value];
  37. return n == null ? t : n;
  38. }
  39. function Z(e) {
  40. var n, a;
  41. if (!e) return "";
  42. const t = e[Q.value];
  43. return t == null ? (a = (n = e.label) != null ? n : e.tab) != null ? a : "" : t;
  44. }
  45. const S = (e) => e === "" || e === void 0 || e === null;
  46. function ee() {
  47. if (!D.value && d.value.length) return s(d.value[0], 0);
  48. const e = v();
  49. return e.length ? e[0].dataset.value : "";
  50. }
  51. function te() {
  52. return S(u.defaultActiveKey) ? ee() : u.defaultActiveKey;
  53. }
  54. function L() {
  55. if (!S(l.value)) return !1;
  56. const e = te();
  57. return S(e) ? !1 : (l.value = e, !0);
  58. }
  59. function v() {
  60. const e = X.value;
  61. return e ? Array.from(e.querySelectorAll(".s-tab-item")) : [];
  62. }
  63. function w() {
  64. return v().findIndex((e) => e.dataset.value === String(l.value));
  65. }
  66. function K() {
  67. const e = r.value;
  68. e && (f.value = e.scrollWidth - e.clientWidth > 1, N.value = e.scrollLeft > 1, O.value = e.scrollLeft + e.clientWidth < e.scrollWidth - 1);
  69. }
  70. function ne(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) - G : a.right > n.right && (o = t.scrollLeft + (a.right - n.right) + G), o !== null && t.scrollTo({ left: Math.max(0, o), behavior: "smooth" });
  76. }
  77. function p() {
  78. const e = w();
  79. e >= 0 && ne(v()[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 x(e) {
  86. if (e === l.value) return;
  87. l.value = e, m("update:activeKey", e);
  88. const t = d.value.findIndex((n, a) => s(n, a) === e);
  89. m("change", e, t >= 0 ? d.value[t] : void 0), k(p);
  90. }
  91. function P() {
  92. const e = v(), t = w();
  93. if (t > 0) {
  94. x(e[t - 1].dataset.value);
  95. return;
  96. }
  97. W(-1);
  98. }
  99. function T() {
  100. const e = v(), t = w();
  101. if (t >= 0 && t < e.length - 1) {
  102. x(e[t + 1].dataset.value);
  103. return;
  104. }
  105. W(1);
  106. }
  107. function le(e) {
  108. const t = r.value;
  109. if (!t || !f.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, E = !1;
  114. function ae(e) {
  115. 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));
  116. }
  117. function re(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, A.value = !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 b() {
  130. var a;
  131. if (B(), !i) return;
  132. const e = r.value, { moved: t, id: n } = i;
  133. i = null, A.value = !1, (a = e == null ? void 0 : e.hasPointerCapture) != null && a.call(e, n) && e.releasePointerCapture(n), t && (E = !0);
  134. }
  135. function B() {
  136. window.removeEventListener("pointerup", b, !0), window.removeEventListener("pointercancel", b, !0);
  137. }
  138. function oe(e) {
  139. E && (E = !1, e.preventDefault(), e.stopPropagation());
  140. }
  141. const ie = g(() => w() > 0 || N.value), ue = g(() => {
  142. const e = w();
  143. return e >= 0 && e < v().length - 1 || O.value;
  144. });
  145. ce("sTabContext", {
  146. activeValue: l,
  147. select: x,
  148. prev: P,
  149. next: T
  150. }), I(
  151. () => u.activeKey,
  152. (e) => {
  153. if (e !== l.value) {
  154. if (S(e)) {
  155. L() && m("update:activeKey", l.value), k(p);
  156. return;
  157. }
  158. l.value = e, k(p);
  159. }
  160. }
  161. ), I(
  162. () => d.value,
  163. () => {
  164. k(() => {
  165. K(), L() && m("update:activeKey", l.value), p();
  166. });
  167. },
  168. { deep: !0 }
  169. );
  170. let h = null;
  171. return fe(() => {
  172. k(() => {
  173. K(), L() && m("update:activeKey", l.value), p();
  174. }), window.ResizeObserver && r.value && (h = new ResizeObserver(() => K()), h.observe(r.value), r.value.firstElementChild && h.observe(r.value.firstElementChild));
  175. }), de(() => {
  176. B(), h && (h.disconnect(), h = null);
  177. }), Y({ syncArrow: K, scrollToActive: p, scrollRef: r }), (e, t) => {
  178. const n = M("left-outlined"), a = M("right-outlined");
  179. return _(), C("div", {
  180. class: V(["s-tab", { "is-overflow": f.value, dragging: A.value }])
  181. }, [
  182. f.value ? (_(), C("button", {
  183. key: 0,
  184. type: "link",
  185. class: "s-tab-arrow prev",
  186. disabled: !ie.value,
  187. title: e.$l("上一个"),
  188. onClick: P
  189. }, [
  190. $(n)
  191. ], 8, ge)) : z("", !0),
  192. F("div", {
  193. ref_key: "scrollRef",
  194. ref: r,
  195. class: "s-tab-scroll",
  196. role: "tablist",
  197. onScroll: K,
  198. onWheel: R(le, ["prevent"]),
  199. onPointerdown: ae,
  200. onPointermove: re,
  201. onPointerup: b,
  202. onPointercancel: b,
  203. onDragstart: t[0] || (t[0] = R(() => {
  204. }, ["prevent"])),
  205. onClickCapture: oe
  206. }, [
  207. F("div", {
  208. ref_key: "trackRef",
  209. ref: X,
  210. class: "s-tab-track"
  211. }, [
  212. D.value ? he(e.$slots, "default", {}, void 0, !0, 1) : (_(!0), C(ve, { key: 0 }, pe(d.value, (o, y) => (_(), C("a", {
  213. key: s(o, y),
  214. class: V(["s-tab-item", { active: s(o, y) === l.value }]),
  215. "data-value": s(o, y),
  216. role: "tab",
  217. "aria-selected": s(o, y) === l.value,
  218. tabindex: s(o, y) === l.value ? 0 : -1,
  219. onClick: (_e) => x(s(o, y)),
  220. onKeydown: [
  221. j(R(P, ["prevent"]), ["left"]),
  222. j(R(T, ["prevent"]), ["right"])
  223. ]
  224. }, be(Z(o)), 43, me))), 128))
  225. ], 512)
  226. ], 544),
  227. f.value ? (_(), C("button", {
  228. key: 1,
  229. type: "link",
  230. class: "s-tab-arrow next",
  231. disabled: !ue.value,
  232. title: e.$l("下一个"),
  233. onClick: T
  234. }, [
  235. $(a)
  236. ], 8, we)) : z("", !0)
  237. ], 2);
  238. };
  239. }
  240. }), xe = /* @__PURE__ */ ye(ke, [["__scopeId", "data-v-328b8dcc"]]);
  241. export {
  242. xe as default
  243. };