menu-item.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <li class="el-menu-item"
  3. role="menuitem"
  4. tabindex="-1"
  5. :style="[paddingStyle, itemStyle, { backgroundColor }]"
  6. :class="{
  7. 'is-active': active,
  8. 'is-disabled': disabled
  9. }"
  10. @click="handleClick"
  11. @mouseenter="onMouseEnter"
  12. @focus="onMouseEnter"
  13. @blur="onMouseLeave"
  14. @mouseleave="onMouseLeave"
  15. >
  16. <el-tooltip
  17. v-if="parentMenu.$options.componentName === 'ElMenu' && rootMenu.collapse && $slots.title"
  18. effect="dark"
  19. placement="right">
  20. <div slot="content"><slot name="title"></slot></div>
  21. <div style="position: absolute;left: 0;top: 0;height: 100%;width: 100%;display: inline-block;box-sizing: border-box;padding: 0 20px;">
  22. <slot></slot>
  23. </div>
  24. </el-tooltip>
  25. <template v-else>
  26. <slot></slot>
  27. <slot name="title"></slot>
  28. </template>
  29. </li>
  30. </template>
  31. <script>
  32. import Menu from './menu-mixin';
  33. import ElTooltip from 'element-ui/packages/tooltip';
  34. import Emitter from 'element-ui/src/mixins/emitter';
  35. export default {
  36. name: 'ElMenuItem',
  37. componentName: 'ElMenuItem',
  38. mixins: [Menu, Emitter],
  39. components: { ElTooltip },
  40. props: {
  41. index: {
  42. default: null,
  43. validator: val => typeof val === 'string' || val === null
  44. },
  45. route: [String, Object],
  46. disabled: Boolean
  47. },
  48. computed: {
  49. active() {
  50. return this.index === this.rootMenu.activeIndex;
  51. },
  52. hoverBackground() {
  53. return this.rootMenu.hoverBackground;
  54. },
  55. backgroundColor() {
  56. return this.rootMenu.backgroundColor || '';
  57. },
  58. activeTextColor() {
  59. return this.rootMenu.activeTextColor || '';
  60. },
  61. textColor() {
  62. return this.rootMenu.textColor || '';
  63. },
  64. mode() {
  65. return this.rootMenu.mode;
  66. },
  67. itemStyle() {
  68. const style = {
  69. color: this.active ? this.activeTextColor : this.textColor
  70. };
  71. if (this.mode === 'horizontal' && !this.isNested) {
  72. style.borderBottomColor = this.active
  73. ? (this.rootMenu.activeTextColor ? this.activeTextColor : '')
  74. : 'transparent';
  75. }
  76. return style;
  77. },
  78. isNested() {
  79. return this.parentMenu !== this.rootMenu;
  80. }
  81. },
  82. methods: {
  83. onMouseEnter() {
  84. if (this.mode === 'horizontal' && !this.rootMenu.backgroundColor) return;
  85. this.$el.style.backgroundColor = this.hoverBackground;
  86. },
  87. onMouseLeave() {
  88. if (this.mode === 'horizontal' && !this.rootMenu.backgroundColor) return;
  89. this.$el.style.backgroundColor = this.backgroundColor;
  90. },
  91. handleClick() {
  92. if (!this.disabled) {
  93. this.dispatch('ElMenu', 'item-click', this);
  94. this.$emit('click', this);
  95. }
  96. }
  97. },
  98. mounted() {
  99. this.parentMenu.addItem(this);
  100. this.rootMenu.addItem(this);
  101. },
  102. beforeDestroy() {
  103. this.parentMenu.removeItem(this);
  104. this.rootMenu.removeItem(this);
  105. }
  106. };
  107. </script>