SidebarItem.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { defineComponent } from "vue";
  3. import { extend, numericProp, createNamespace } from "../utils/index.mjs";
  4. import { SIDEBAR_KEY } from "../sidebar/Sidebar.mjs";
  5. import { useParent } from "@vant/use";
  6. import { useRoute, routeProps } from "../composables/use-route.mjs";
  7. import { Badge } from "../badge/index.mjs";
  8. const [name, bem] = createNamespace("sidebar-item");
  9. const sidebarItemProps = extend({}, routeProps, {
  10. dot: Boolean,
  11. title: String,
  12. badge: numericProp,
  13. disabled: Boolean,
  14. badgeProps: Object
  15. });
  16. var stdin_default = defineComponent({
  17. name,
  18. props: sidebarItemProps,
  19. emits: ["click"],
  20. setup(props, {
  21. emit,
  22. slots
  23. }) {
  24. const route = useRoute();
  25. const {
  26. parent,
  27. index
  28. } = useParent(SIDEBAR_KEY);
  29. if (!parent) {
  30. if (process.env.NODE_ENV !== "production") {
  31. console.error("[Vant] <SidebarItem> must be a child component of <Sidebar>.");
  32. }
  33. return;
  34. }
  35. const onClick = () => {
  36. if (props.disabled) {
  37. return;
  38. }
  39. emit("click", index.value);
  40. parent.setActive(index.value);
  41. route();
  42. };
  43. return () => {
  44. const {
  45. dot,
  46. badge,
  47. title,
  48. disabled
  49. } = props;
  50. const selected = index.value === parent.getActive();
  51. return _createVNode("div", {
  52. "role": "tab",
  53. "class": bem({
  54. select: selected,
  55. disabled
  56. }),
  57. "tabindex": disabled ? void 0 : 0,
  58. "aria-selected": selected,
  59. "onClick": onClick
  60. }, [_createVNode(Badge, _mergeProps({
  61. "dot": dot,
  62. "class": bem("text"),
  63. "content": badge
  64. }, props.badgeProps), {
  65. default: () => [slots.title ? slots.title() : title]
  66. })]);
  67. };
  68. }
  69. });
  70. export {
  71. stdin_default as default
  72. };