TabbarItem.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. import { computed, defineComponent, getCurrentInstance } from "vue";
  3. import { createNamespace, extend, isObject, numericProp } from "../utils/index.mjs";
  4. import { TABBAR_KEY } from "../tabbar/Tabbar.mjs";
  5. import { useParent } from "@vant/use";
  6. import { routeProps, useRoute } from "../composables/use-route.mjs";
  7. import { Icon } from "../icon/index.mjs";
  8. import { Badge } from "../badge/index.mjs";
  9. const [name, bem] = createNamespace("tabbar-item");
  10. const tabbarItemProps = extend({}, routeProps, {
  11. dot: Boolean,
  12. icon: String,
  13. name: numericProp,
  14. badge: numericProp,
  15. badgeProps: Object,
  16. iconPrefix: String
  17. });
  18. var stdin_default = defineComponent({
  19. name,
  20. props: tabbarItemProps,
  21. emits: ["click"],
  22. setup(props, {
  23. emit,
  24. slots
  25. }) {
  26. const route = useRoute();
  27. const vm = getCurrentInstance().proxy;
  28. const {
  29. parent,
  30. index
  31. } = useParent(TABBAR_KEY);
  32. if (!parent) {
  33. if (process.env.NODE_ENV !== "production") {
  34. console.error("[Vant] <TabbarItem> must be a child component of <Tabbar>.");
  35. }
  36. return;
  37. }
  38. const active = computed(() => {
  39. var _a;
  40. const {
  41. route: route2,
  42. modelValue
  43. } = parent.props;
  44. if (route2 && "$route" in vm) {
  45. const {
  46. $route
  47. } = vm;
  48. const {
  49. to
  50. } = props;
  51. const config = isObject(to) ? to : {
  52. path: to
  53. };
  54. return !!$route.matched.find((val) => {
  55. const pathMatched = "path" in config && config.path === val.path;
  56. const nameMatched = "name" in config && config.name === val.name;
  57. return pathMatched || nameMatched;
  58. });
  59. }
  60. return ((_a = props.name) != null ? _a : index.value) === modelValue;
  61. });
  62. const onClick = (event) => {
  63. var _a;
  64. if (!active.value) {
  65. parent.setActive((_a = props.name) != null ? _a : index.value, route);
  66. }
  67. emit("click", event);
  68. };
  69. const renderIcon = () => {
  70. if (slots.icon) {
  71. return slots.icon({
  72. active: active.value
  73. });
  74. }
  75. if (props.icon) {
  76. return _createVNode(Icon, {
  77. "name": props.icon,
  78. "classPrefix": props.iconPrefix
  79. }, null);
  80. }
  81. };
  82. return () => {
  83. var _a;
  84. const {
  85. dot,
  86. badge
  87. } = props;
  88. const {
  89. activeColor,
  90. inactiveColor
  91. } = parent.props;
  92. const color = active.value ? activeColor : inactiveColor;
  93. return _createVNode("div", {
  94. "role": "tab",
  95. "class": bem({
  96. active: active.value
  97. }),
  98. "style": {
  99. color
  100. },
  101. "tabindex": 0,
  102. "aria-selected": active.value,
  103. "onClick": onClick
  104. }, [_createVNode(Badge, _mergeProps({
  105. "dot": dot,
  106. "class": bem("icon"),
  107. "content": badge
  108. }, props.badgeProps), {
  109. default: renderIcon
  110. }), _createVNode("div", {
  111. "class": bem("text")
  112. }, [(_a = slots.default) == null ? void 0 : _a.call(slots, {
  113. active: active.value
  114. })])]);
  115. };
  116. }
  117. });
  118. export {
  119. stdin_default as default
  120. };