uni-menu-item.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. let subMenu = this.rootMenu && this.rootMenu.SubMenu && this.rootMenu.SubMenu.length || 0;
  45. return 20 + 20 * subMenu + 'px'
  46. }
  47. },
  48. created() {
  49. this.init()
  50. },
  51. destroyed() {
  52. if (this.$menuParent) {
  53. const menuIndex = this.$menuParent.itemChildrens.findIndex(item => item === this)
  54. this.$menuParent.itemChildrens.splice(menuIndex, 1)
  55. }
  56. },
  57. methods: {
  58. init() {
  59. this.rootMenu = {
  60. NavMenu: [],
  61. SubMenu: []
  62. }
  63. this.indexPath = []
  64. // 获取直系的所有父元素实例
  65. this.getParentAll('SubMenu', this)
  66. // 获取最外层父元素实例
  67. this.$menuParent = this.getParent('uniNavMenu', this)
  68. this.$subMenu = this.rootMenu.SubMenu
  69. this.activeTextColor = this.$menuParent.activeTextColor
  70. this.textColor = this.$menuParent.textColor
  71. this.activeBackgroundColor = this.$menuParent.activeBackgroundColor
  72. // 将当前插入到menu数组中
  73. if (this.$menuParent) {
  74. this.$menuParent.itemChildrens.push(this)
  75. this.$menuParent.isActive(this)
  76. }
  77. },
  78. // 点击 menuItem
  79. onClickItem(e) {
  80. if (this.disabled) return
  81. // 关闭其他已经选中的 itemMenu
  82. this.$menuParent.closeOtherActive(this)
  83. this.active = true
  84. this.indexPath.unshift(this.index)
  85. this.indexPath.reverse()
  86. if(e !== 'init'){
  87. // this.$menuParent.activeIndex=this.index
  88. this.$menuParent.select(this.index, this.indexPath)
  89. }
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss">
  95. .uni-menu-item {
  96. display: flex;
  97. align-items: center;
  98. padding: 0 20px;
  99. height: 56px;
  100. line-height: 56px;
  101. color: #303133;
  102. transition: all 0.3s;
  103. cursor: pointer;
  104. // border-bottom: 1px #f5f5f5 solid;
  105. }
  106. .uni-menu-item:hover {
  107. outline: none;
  108. background-color: #EBEBEB;
  109. transition: all 0.3s;
  110. }
  111. .is-active {
  112. color: $uni-color-primary;
  113. // background-color: #ecf8f3;
  114. }
  115. .is-disabled {
  116. // background-color: #f5f5f5;
  117. color: #999;
  118. }
  119. .uni-menu-item.is-disabled:hover {
  120. background-color: inherit;
  121. color: #999;
  122. cursor: not-allowed;
  123. }
  124. </style>