uni-menu-item.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="uni-menu-item"
  3. :class="{
  4. 'is-active':active,
  5. 'is-disabled':disabled
  6. }"
  7. :style="{
  8. paddingLeft:paddingLeft,
  9. 'background-color':active?activeBackgroundColor:''
  10. }"
  11. @click="onClickItem">
  12. <slot></slot>
  13. </view>
  14. </template>
  15. <script>
  16. import rootParent from '../uni-nav-menu/mixins/rootParent.js'
  17. export default {
  18. name: 'uniMenuItem',
  19. mixins: [rootParent],
  20. props: {
  21. // 唯一标识
  22. index: {
  23. type: [String,Object],
  24. default(){
  25. return ''
  26. }
  27. },
  28. // TODO 是否禁用
  29. disabled: {
  30. type: Boolean,
  31. default: false
  32. }
  33. },
  34. data() {
  35. return {
  36. active: false,
  37. activeTextColor: '#42B983',
  38. textColor: '#303133',
  39. activeBackgroundColor: ''
  40. };
  41. },
  42. computed: {
  43. paddingLeft() {
  44. return 20 + 20 * this.rootMenu.SubMenu.length + 'px'
  45. }
  46. },
  47. created() {
  48. this.init()
  49. },
  50. destroyed() {
  51. if (this.$menuParent) {
  52. const menuIndex = this.$menuParent.itemChildrens.findIndex(item => item === this)
  53. this.$menuParent.itemChildrens.splice(menuIndex, 1)
  54. }
  55. },
  56. methods: {
  57. init() {
  58. this.rootMenu = {
  59. NavMenu: [],
  60. SubMenu: []
  61. }
  62. this.indexPath = []
  63. // 获取直系的所有父元素实例
  64. this.getParentAll('SubMenu', this)
  65. // 获取最外层父元素实例
  66. this.$menuParent = this.getParent('uniNavMenu', this)
  67. this.$subMenu = this.rootMenu.SubMenu
  68. this.activeTextColor = this.$menuParent.activeTextColor
  69. this.textColor = this.$menuParent.textColor
  70. this.activeBackgroundColor = this.$menuParent.activeBackgroundColor
  71. // 将当前插入到menu数组中
  72. if (this.$menuParent) {
  73. this.$menuParent.itemChildrens.push(this)
  74. this.$menuParent.isActive(this)
  75. }
  76. },
  77. // 点击 menuItem
  78. onClickItem(e) {
  79. if (this.disabled) return
  80. // 关闭其他已经选中的 itemMenu
  81. this.$menuParent.closeOtherActive(this)
  82. this.active = true
  83. this.indexPath.unshift(this.index)
  84. this.indexPath.reverse()
  85. if(e !== 'init'){
  86. // this.$menuParent.activeIndex=this.index
  87. this.$menuParent.select(this.index, this.indexPath)
  88. }
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss">
  94. .uni-menu-item {
  95. display: flex;
  96. align-items: center;
  97. padding: 0 20px;
  98. height: 56px;
  99. line-height: 56px;
  100. color: #303133;
  101. transition: all 0.3s;
  102. cursor: pointer;
  103. // border-bottom: 1px #f5f5f5 solid;
  104. }
  105. .uni-menu-item:hover {
  106. outline: none;
  107. background-color: #EBEBEB;
  108. transition: all 0.3s;
  109. }
  110. .is-active {
  111. color: $uni-color-primary;
  112. // background-color: #ecf8f3;
  113. }
  114. .is-disabled {
  115. // background-color: #f5f5f5;
  116. color: #999;
  117. }
  118. .uni-menu-item.is-disabled:hover {
  119. background-color: inherit;
  120. color: #999;
  121. cursor: not-allowed;
  122. }
  123. </style>