index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <uni-shadow-root class="vant-tree-select-index"><view class="van-tree-select" :style="'height: '+(innerHeight)">
  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. import { addUnit } from '../common/utils';
  26. VantComponent({
  27. classes: [
  28. 'main-item-class',
  29. 'content-item-class',
  30. 'main-active-class',
  31. 'content-active-class',
  32. 'main-disabled-class',
  33. 'content-disabled-class'
  34. ],
  35. props: {
  36. items: {
  37. type: Array,
  38. observer: 'updateSubItems'
  39. },
  40. activeId: null,
  41. mainActiveIndex: {
  42. type: Number,
  43. value: 0,
  44. observer: 'updateSubItems'
  45. },
  46. height: {
  47. type: [Number, String],
  48. value: 300,
  49. observer: 'updateHeight'
  50. },
  51. max: {
  52. type: Number,
  53. value: Infinity
  54. }
  55. },
  56. data: {
  57. subItems: []
  58. },
  59. created() {
  60. this.updateHeight();
  61. },
  62. methods: {
  63. // 当一个子项被选择时
  64. onSelectItem(event) {
  65. const { item } = event.currentTarget.dataset;
  66. const isArray = Array.isArray(this.data.activeId);
  67. // 判断有没有超出右侧选择的最大数
  68. const isOverMax = isArray && this.data.activeId.length >= this.data.max;
  69. // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件
  70. const isSelected = isArray
  71. ? this.data.activeId.indexOf(item.id) > -1
  72. : this.data.activeId === item.id;
  73. if (!item.disabled && (!isOverMax || isSelected)) {
  74. this.$emit('click-item', item);
  75. }
  76. },
  77. // 当一个导航被点击时
  78. onClickNav(event) {
  79. const index = event.detail;
  80. const item = this.data.items[index];
  81. if (!item.disabled) {
  82. this.$emit('click-nav', { index });
  83. }
  84. },
  85. // 更新子项列表
  86. updateSubItems() {
  87. const { items, mainActiveIndex } = this.data;
  88. const { children = [] } = items[mainActiveIndex] || {};
  89. return this.set({ subItems: children });
  90. },
  91. updateHeight() {
  92. this.setData({
  93. innerHeight: addUnit(this.data.height)
  94. });
  95. }
  96. }
  97. });
  98. export default global['__wxComponents']['vant/tree-select/index']
  99. </script>
  100. <style platform="mp-weixin">
  101. @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:#fafafa;background-color:var(--tree-select-nav-background-color,#fafafa);--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)}
  102. </style>