Tab.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { ref, watch, provide, computed, nextTick, watchEffect, defineComponent, getCurrentInstance, mergeProps as _mergeProps, createVNode as _createVNode, vShow as _vShow, withDirectives as _withDirectives } from "vue";
  2. import { normalizeClass, normalizeStyle, stringifyStyle } from "@vue/shared";
  3. import { pick, 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 { TabTitle } from "./TabTitle.mjs";
  11. import { SwipeItem } from "../swipe-item/index.mjs";
  12. const [name, bem] = createNamespace("tab");
  13. const tabProps = extend({}, routeProps, {
  14. dot: Boolean,
  15. name: numericProp,
  16. badge: numericProp,
  17. title: String,
  18. disabled: Boolean,
  19. titleClass: unknownProp,
  20. titleStyle: [String, Object],
  21. showZeroBadge: truthProp
  22. });
  23. var stdin_default = defineComponent({
  24. name,
  25. props: tabProps,
  26. setup(props, {
  27. slots
  28. }) {
  29. const id = useId();
  30. const inited = ref(false);
  31. const instance = getCurrentInstance();
  32. const {
  33. parent,
  34. index
  35. } = useParent(TABS_KEY);
  36. if (!parent) {
  37. if (process.env.NODE_ENV !== "production") {
  38. console.error("[Vant] <Tab> must be a child component of <Tabs>.");
  39. }
  40. return;
  41. }
  42. const getName = () => {
  43. var _a;
  44. return (_a = props.name) != null ? _a : index.value;
  45. };
  46. const init = () => {
  47. inited.value = true;
  48. if (parent.props.lazyRender) {
  49. nextTick(() => {
  50. parent.onRendered(getName(), props.title);
  51. });
  52. }
  53. };
  54. const active = computed(() => {
  55. const isActive = getName() === parent.currentName.value;
  56. if (isActive && !inited.value) {
  57. init();
  58. }
  59. return isActive;
  60. });
  61. const parsedClass = ref("");
  62. const parsedStyle = ref("");
  63. watchEffect(() => {
  64. const {
  65. titleClass,
  66. titleStyle
  67. } = props;
  68. parsedClass.value = titleClass ? normalizeClass(titleClass) : "";
  69. parsedStyle.value = titleStyle && typeof titleStyle !== "string" ? stringifyStyle(normalizeStyle(titleStyle)) : titleStyle;
  70. });
  71. const renderTitle = (onClickTab) => _createVNode(TabTitle, _mergeProps({
  72. "key": id,
  73. "id": `${parent.id}-${index.value}`,
  74. "ref": parent.setTitleRefs(index.value),
  75. "style": parsedStyle.value,
  76. "class": parsedClass.value,
  77. "isActive": active.value,
  78. "controls": id,
  79. "scrollable": parent.scrollable.value,
  80. "activeColor": parent.props.titleActiveColor,
  81. "inactiveColor": parent.props.titleInactiveColor,
  82. "onClick": (event) => onClickTab(instance.proxy, index.value, event)
  83. }, pick(parent.props, ["type", "color", "shrink"]), pick(props, ["dot", "badge", "title", "disabled", "showZeroBadge"])), {
  84. title: slots.title
  85. });
  86. const hasInactiveClass = ref(!active.value);
  87. watch(active, (val) => {
  88. if (val) {
  89. hasInactiveClass.value = false;
  90. } else {
  91. doubleRaf(() => {
  92. hasInactiveClass.value = true;
  93. });
  94. }
  95. });
  96. watch(() => props.title, () => {
  97. parent.setLine();
  98. parent.scrollIntoView();
  99. });
  100. provide(TAB_STATUS_KEY, active);
  101. useExpose({
  102. id,
  103. renderTitle
  104. });
  105. return () => {
  106. var _a;
  107. const label = `${parent.id}-${index.value}`;
  108. const {
  109. animated,
  110. swipeable,
  111. scrollspy,
  112. lazyRender
  113. } = parent.props;
  114. if (!slots.default && !animated) {
  115. return;
  116. }
  117. const show = scrollspy || active.value;
  118. if (animated || swipeable) {
  119. return _createVNode(SwipeItem, {
  120. "id": id,
  121. "role": "tabpanel",
  122. "class": bem("panel-wrapper", {
  123. inactive: hasInactiveClass.value
  124. }),
  125. "tabindex": active.value ? 0 : -1,
  126. "aria-hidden": !active.value,
  127. "aria-labelledby": label,
  128. "data-allow-mismatch": "attribute"
  129. }, {
  130. default: () => {
  131. var _a2;
  132. return [_createVNode("div", {
  133. "class": bem("panel")
  134. }, [(_a2 = slots.default) == null ? void 0 : _a2.call(slots)])];
  135. }
  136. });
  137. }
  138. const shouldRender = inited.value || scrollspy || !lazyRender;
  139. const Content = shouldRender ? (_a = slots.default) == null ? void 0 : _a.call(slots) : null;
  140. return _withDirectives(_createVNode("div", {
  141. "id": id,
  142. "role": "tabpanel",
  143. "class": bem("panel"),
  144. "tabindex": show ? 0 : -1,
  145. "aria-labelledby": label,
  146. "data-allow-mismatch": "attribute"
  147. }, [Content]), [[_vShow, show]]);
  148. };
  149. }
  150. });
  151. export {
  152. stdin_default as default,
  153. tabProps
  154. };