index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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" :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="checked" 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: [Number, String],
  47. value: 300,
  48. },
  49. max: {
  50. type: Number,
  51. value: Infinity,
  52. },
  53. },
  54. data: {
  55. subItems: [],
  56. },
  57. methods: {
  58. // 当一个子项被选择时
  59. onSelectItem(event) {
  60. const { item } = event.currentTarget.dataset;
  61. const isArray = Array.isArray(this.data.activeId);
  62. // 判断有没有超出右侧选择的最大数
  63. const isOverMax = isArray && this.data.activeId.length >= this.data.max;
  64. // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件
  65. const isSelected = isArray
  66. ? this.data.activeId.indexOf(item.id) > -1
  67. : this.data.activeId === item.id;
  68. if (!item.disabled && (!isOverMax || isSelected)) {
  69. this.$emit('click-item', item);
  70. }
  71. },
  72. // 当一个导航被点击时
  73. onClickNav(event) {
  74. const index = event.detail;
  75. const item = this.data.items[index];
  76. if (!item.disabled) {
  77. this.$emit('click-nav', { index });
  78. }
  79. },
  80. // 更新子项列表
  81. updateSubItems() {
  82. const { items, mainActiveIndex } = this.data;
  83. const { children = [] } = items[mainActiveIndex] || {};
  84. return this.set({ subItems: children });
  85. },
  86. },
  87. });
  88. export default global['__wxComponents']['vant/tree-select/index']
  89. </script>
  90. <style platform="mp-weixin">
  91. @import '../common/index.css';.van-tree-select{position:relative;display:-webkit-flex;display:flex;-webkit-user-select:none;user-select:none;font-size:14px;font-size:var(--tree-select-font-size,14px)}.van-tree-select__nav{-webkit-flex:1;flex:1;background-color:#f7f8fa;background-color:var(--tree-select-nav-background-color,#f7f8fa);--sidebar-padding:12px 8px 12px 12px}.van-tree-select__nav__inner{width:100%!important;height:100%}.van-tree-select__content{-webkit-flex:2;flex:2;background-color:#fff;background-color:var(--tree-select-content-background-color,#fff)}.van-tree-select__item{position:relative;font-weight:700;padding:0 32px 0 16px;padding:0 32px 0 var(--padding-md,16px);line-height:44px;line-height:var(--tree-select-item-height,44px)}.van-tree-select__item--active{color:#ee0a24;color:var(--tree-select-item-active-color,#ee0a24)}.van-tree-select__item--disabled{color:#c8c9cc;color:var(--tree-select-item-disabled-color,#c8c9cc)}.van-tree-select__selected{position:absolute;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%);right:16px;right:var(--padding-md,16px)}
  92. </style>