index.vue 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <uni-shadow-root class="vant-tree-select-index"><view class="van-tree-select" :style="'height: '+(utils.addUnit(height))">
  3. <scroll-view scroll-y class="van-tree-select__nav">
  4. <van-sidebar :active-key="mainActiveIndex" @change="onClickNav" custom-class="van-tree-select__nav__inner">
  5. <van-sidebar-item v-for="(item,index) in (items)" :key="item.index" custom-class="main-item-class" active-class="main-active-class" disabled-class="main-disabled-class" :badge="item.badge" :dot="item.dot" :title="item.text" :disabled="item.disabled"></van-sidebar-item>
  6. </van-sidebar>
  7. </scroll-view>
  8. <scroll-view scroll-y class="van-tree-select__content">
  9. <slot name="content"></slot>
  10. <view v-for="(item,index) in (subItems)" :key="item.id" :class="'van-ellipsis content-item-class '+(utils.bem('tree-select__item', { active: wxs.isActive(activeId, item.id), disabled: item.disabled }))+' '+(wxs.isActive(activeId, item.id) ? 'content-active-class' : '')+' '+(item.disabled ? 'content-disabled-class' : '')" :data-item="item" @click="onSelectItem">
  11. {{ item.text }}
  12. <van-icon v-if="wxs.isActive(activeId, item.id)" :name="selectedIcon" size="16px" class="van-tree-select__selected"></van-icon>
  13. </view>
  14. </scroll-view>
  15. </view></uni-shadow-root>
  16. </template>
  17. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="wxs"></wxs>
  18. <script>
  19. import VanIcon from '../icon/index.vue'
  20. import VanSidebar from '../sidebar/index.vue'
  21. import VanSidebarItem from '../sidebar-item/index.vue'
  22. global['__wxVueOptions'] = {components:{'van-icon': VanIcon,'van-sidebar': VanSidebar,'van-sidebar-item': VanSidebarItem}}
  23. global['__wxRoute'] = 'vant/tree-select/index'
  24. import { VantComponent } from '../common/component';
  25. VantComponent({
  26. classes: [
  27. 'main-item-class',
  28. 'content-item-class',
  29. 'main-active-class',
  30. 'content-active-class',
  31. 'main-disabled-class',
  32. 'content-disabled-class',
  33. ],
  34. props: {
  35. items: {
  36. type: Array,
  37. observer: 'updateSubItems',
  38. },
  39. activeId: null,
  40. mainActiveIndex: {
  41. type: Number,
  42. value: 0,
  43. observer: 'updateSubItems',
  44. },
  45. height: {
  46. type: null,
  47. value: 300,
  48. },
  49. max: {
  50. type: Number,
  51. value: Infinity,
  52. },
  53. selectedIcon: {
  54. type: String,
  55. value: 'success',
  56. },
  57. },
  58. data: {
  59. subItems: [],
  60. },
  61. methods: {
  62. // 当一个子项被选择时
  63. onSelectItem(event) {
  64. const { item } = event.currentTarget.dataset;
  65. const isArray = Array.isArray(this.data.activeId);
  66. // 判断有没有超出右侧选择的最大数
  67. const isOverMax = isArray && this.data.activeId.length >= this.data.max;
  68. // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件
  69. const isSelected = isArray
  70. ? this.data.activeId.indexOf(item.id) > -1
  71. : this.data.activeId === item.id;
  72. if (!item.disabled && (!isOverMax || isSelected)) {
  73. this.$emit('click-item', item);
  74. }
  75. },
  76. // 当一个导航被点击时
  77. onClickNav(event) {
  78. const index = event.detail;
  79. const item = this.data.items[index];
  80. if (!item.disabled) {
  81. this.$emit('click-nav', { index });
  82. }
  83. },
  84. // 更新子项列表
  85. updateSubItems() {
  86. const { items, mainActiveIndex } = this.data;
  87. const { children = [] } = items[mainActiveIndex] || {};
  88. this.setData({ subItems: children });
  89. },
  90. },
  91. });
  92. export default global['__wxComponents']['vant/tree-select/index']
  93. </script>
  94. <style platform="mp-weixin">
  95. @import '../common/index.css';.van-tree-select{display:flex;font-size:var(--tree-select-font-size,14px);position:relative;-webkit-user-select:none;user-select:none}.van-tree-select__nav{--sidebar-padding:12px 8px 12px 12px;background-color:var(--tree-select-nav-background-color,#f7f8fa);flex:1}.van-tree-select__nav__inner{height:100%;width:100%!important}.van-tree-select__content{background-color:var(--tree-select-content-background-color,#fff);flex:2}.van-tree-select__item{font-weight:700;line-height:var(--tree-select-item-height,44px);padding:0 32px 0 var(--padding-md,16px);position:relative}.van-tree-select__item--active{color:var(--tree-select-item-active-color,#ee0a24)}.van-tree-select__item--disabled{color:var(--tree-select-item-disabled-color,#c8c9cc)}.van-tree-select__selected{position:absolute;right:var(--padding-md,16px);top:50%;transform:translateY(-50%)}
  96. </style>