Tab.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
  2. import { ref, watch, provide, computed, nextTick, defineComponent } from "vue";
  3. import { extend, truthProp, unknownProp, numericProp, createNamespace } from "../utils/index.mjs";
  4. import { TABS_KEY } from "../tabs/Tabs.mjs";
  5. import { doubleRaf, useParent } from "@vant/use";
  6. import { useId } from "../composables/use-id.mjs";
  7. import { useExpose } from "../composables/use-expose.mjs";
  8. import { routeProps } from "../composables/use-route.mjs";
  9. import { TAB_STATUS_KEY } from "../composables/use-tab-status.mjs";
  10. import { SwipeItem } from "../swipe-item/index.mjs";
  11. const [name, bem] = createNamespace("tab");
  12. const tabProps = extend({}, routeProps, {
  13. dot: Boolean,
  14. name: numericProp,
  15. badge: numericProp,
  16. title: String,
  17. disabled: Boolean,
  18. titleClass: unknownProp,
  19. titleStyle: [String, Object],
  20. showZeroBadge: truthProp
  21. });
  22. var stdin_default = defineComponent({
  23. name,
  24. props: tabProps,
  25. setup(props, {
  26. slots
  27. }) {
  28. const id = useId();
  29. const inited = ref(false);
  30. const {
  31. parent,
  32. index
  33. } = useParent(TABS_KEY);
  34. if (!parent) {
  35. if (process.env.NODE_ENV !== "production") {
  36. console.error("[Vant] <Tab> must be a child component of <Tabs>.");
  37. }
  38. return;
  39. }
  40. const getName = () => {
  41. var _a;
  42. return (_a = props.name) != null ? _a : index.value;
  43. };
  44. const init = () => {
  45. inited.value = true;
  46. if (parent.props.lazyRender) {
  47. nextTick(() => {
  48. parent.onRendered(getName(), props.title);
  49. });
  50. }
  51. };
  52. const active = computed(() => {
  53. const isActive = getName() === parent.currentName.value;
  54. if (isActive && !inited.value) {
  55. init();
  56. }
  57. return isActive;
  58. });
  59. const hasInactiveClass = ref(!active.value);
  60. watch(active, (val) => {
  61. if (val) {
  62. hasInactiveClass.value = false;
  63. } else {
  64. doubleRaf(() => {
  65. hasInactiveClass.value = true;
  66. });
  67. }
  68. });
  69. watch(() => props.title, () => {
  70. parent.setLine();
  71. parent.scrollIntoView();
  72. });
  73. provide(TAB_STATUS_KEY, active);
  74. return () => {
  75. var _a;
  76. const label = `${parent.id}-${index.value}`;
  77. const {
  78. animated,
  79. swipeable,
  80. scrollspy,
  81. lazyRender
  82. } = parent.props;
  83. if (!slots.default && !animated) {
  84. return;
  85. }
  86. const show = scrollspy || active.value;
  87. if (animated || swipeable) {
  88. return _createVNode(SwipeItem, {
  89. "id": id,
  90. "role": "tabpanel",
  91. "class": bem("panel-wrapper", {
  92. inactive: hasInactiveClass.value
  93. }),
  94. "tabindex": active.value ? 0 : -1,
  95. "aria-hidden": !active.value,
  96. "aria-labelledby": label
  97. }, {
  98. default: () => {
  99. var _a2;
  100. return [_createVNode("div", {
  101. "class": bem("panel")
  102. }, [(_a2 = slots.default) == null ? void 0 : _a2.call(slots)])];
  103. }
  104. });
  105. }
  106. const shouldRender = inited.value || scrollspy || !lazyRender;
  107. const Content = shouldRender ? (_a = slots.default) == null ? void 0 : _a.call(slots) : null;
  108. useExpose({
  109. id
  110. });
  111. return _withDirectives(_createVNode("div", {
  112. "id": id,
  113. "role": "tabpanel",
  114. "class": bem("panel"),
  115. "tabindex": show ? 0 : -1,
  116. "aria-labelledby": label
  117. }, [Content]), [[_vShow, show]]);
  118. };
  119. }
  120. });
  121. export {
  122. stdin_default as default
  123. };