TabbarItem.mjs 3.2 KB

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