uni-sub-menu.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="uni-sub-menu">
  3. <view class="uni-sub-menu__title" :class="{'is-disabled':disabled}" :style="{paddingLeft:paddingLeft}" @click="select">
  4. <view class="uni-sub-menu__title-sub" :style="{color:disabled?'#999':textColor}">
  5. <slot name="title"></slot>
  6. </view>
  7. <uni-icons class="uni-sub-menu__icon" :class="{transition:isOpen}" type="arrowdown" color="#bbb" size="14"></uni-icons>
  8. </view>
  9. <view class="uni-sub-menu__content" :class="{'uni-sub-menu--close':!isOpen}" :style="{'background-color':backgroundColor}">
  10. <view id="content--hook">
  11. <slot></slot>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import rootParent from '../uni-nav-menu/mixins/rootParent.js'
  18. export default {
  19. name: 'uniSubMenu',
  20. mixins: [rootParent],
  21. props: {
  22. // 唯一标识
  23. index: {
  24. type: [String,Object],
  25. default(){
  26. return ''
  27. }
  28. },
  29. // TODO 自定义类名
  30. popperClass: {
  31. type: String,
  32. default: ''
  33. },
  34. // TODO 是否禁用
  35. disabled: {
  36. type: Boolean,
  37. default: false
  38. },
  39. // 展开菜单的背景色
  40. backgroundColor: {
  41. type: String,
  42. default: '#f5f5f5'
  43. },
  44. },
  45. data() {
  46. return {
  47. height: 0,
  48. oldheight: 0,
  49. isOpen: false,
  50. textColor:'#303133'
  51. };
  52. },
  53. computed: {
  54. paddingLeft() {
  55. let subMenu = this.rootMenu && this.rootMenu.SubMenu && this.rootMenu.SubMenu.length || 0;
  56. return 20 + 20 * subMenu + 'px'
  57. }
  58. },
  59. created() {
  60. this.init()
  61. },
  62. destroyed() {
  63. // 销毁页面后,将当前页面实例从数据中删除
  64. if (this.$menuParent) {
  65. const menuIndex = this.$menuParent.subChildrens.findIndex(item => item === this)
  66. this.$menuParent.subChildrens.splice(menuIndex, 1)
  67. }
  68. },
  69. methods: {
  70. init() {
  71. // 所有父元素
  72. this.rootMenu = {
  73. NavMenu: [],
  74. SubMenu: []
  75. }
  76. this.childrens = []
  77. this.indexPath = []
  78. // 获取直系的所有父元素实例
  79. this.getParentAll('SubMenu', this)
  80. // 获取最外层父元素实例
  81. this.$menuParent = this.getParent('uniNavMenu', this)
  82. this.textColor = this.$menuParent.textColor
  83. // 直系父元素 SubMenu
  84. this.$subMenu = this.rootMenu.SubMenu
  85. // 将当前插入到menu数组中
  86. if(this.$menuParent){
  87. this.$menuParent.subChildrens.push(this)
  88. }
  89. },
  90. select() {
  91. if(this.disabled) return
  92. // 手动开关 sunMenu
  93. this.$menuParent.selectMenu(this)
  94. },
  95. open() {
  96. this.isOpen = true
  97. },
  98. close() {
  99. this.isOpen = false
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss">
  105. .uni-sub-menu {
  106. position: relative;
  107. /* background-color: #FFFFFF; */
  108. }
  109. .uni-sub-menu__title {
  110. display: flex;
  111. align-items: center;
  112. padding: 0 20px;
  113. padding-right: 10px;
  114. height: 56px;
  115. line-height: 56px;
  116. color: #303133;
  117. cursor: pointer;
  118. /* border-bottom: 1px #f5f5f5 solid; */
  119. }
  120. .uni-sub-menu__title:hover {
  121. color: #42B983;
  122. outline: none;
  123. background-color: #EBEBEB;
  124. }
  125. .uni-sub-menu__title-sub {
  126. display: flex;
  127. align-items: center;
  128. flex: 1;
  129. }
  130. .uni-sub-menu--close {
  131. height: 0;
  132. /* transition: all 0.3s; */
  133. }
  134. .uni-sub-menu__content {
  135. overflow: hidden;
  136. }
  137. .uni-sub-menu__icon {
  138. max-height: auto;
  139. transition: all 0.2s;
  140. }
  141. .transition {
  142. transform: rotate(-180deg);
  143. }
  144. .is-disabled {
  145. /* background-color: #f5f5f5; */
  146. color: red;
  147. }
  148. .uni-sub-menu__title.is-disabled:hover {
  149. background-color: inherit;
  150. color: #999;
  151. cursor: not-allowed;
  152. }
  153. </style>