index.vue 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <uni-shadow-root class="vant-tree-select-index"><view class="van-tree-select" :style="'height: '+(mainHeight)+'px'">
  3. <scroll-view scroll-y class="van-tree-select__nav">
  4. <view v-for="(item,index) in (items)" :key="item.index" :class="'van-ellipsis main-item-class '+(utils.bem('tree-select__nitem', { active: mainActiveIndex === index, disabled: item.disabled }))+' '+(mainActiveIndex === index ? 'main-active-class' : '')+' '+(item.disabled ? 'main-disabled-class' : '')" :data-index="index" @click="onClickNav">
  5. {{ item.text }}
  6. </view>
  7. </scroll-view>
  8. <scroll-view scroll-y class="van-tree-select__content" :style="'height: '+(itemHeight)+'px'">
  9. <view v-for="(item,index) in (subItems)" :key="item.id" :class="'van-ellipsis van-hairline--bottom 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">
  10. {{ item.text }}
  11. <van-icon v-if="wxs.isActive(activeId, item.id)" name="checked" size="16px" class="van-tree-select__selected"></van-icon>
  12. </view>
  13. </scroll-view>
  14. </view></uni-shadow-root>
  15. </template>
  16. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="wxs"></wxs>
  17. <script>
  18. import VanIcon from '../icon/index.vue'
  19. global['__wxVueOptions'] = {components:{'van-icon': VanIcon}}
  20. global['__wxRoute'] = 'vant/tree-select/index'
  21. import { VantComponent } from '../common/component';
  22. const ITEM_HEIGHT = 44;
  23. VantComponent({
  24. classes: [
  25. 'main-item-class',
  26. 'content-item-class',
  27. 'main-active-class',
  28. 'content-active-class',
  29. 'main-disabled-class',
  30. 'content-disabled-class'
  31. ],
  32. props: {
  33. items: Array,
  34. mainActiveIndex: {
  35. type: Number,
  36. value: 0
  37. },
  38. activeId: {
  39. type: [Number, String, Array]
  40. },
  41. maxHeight: {
  42. type: Number,
  43. value: 300
  44. }
  45. },
  46. data: {
  47. subItems: [],
  48. mainHeight: 0,
  49. itemHeight: 0
  50. },
  51. watch: {
  52. items() {
  53. this.updateSubItems().then(() => {
  54. this.updateMainHeight();
  55. });
  56. },
  57. maxHeight() {
  58. this.updateItemHeight(this.data.subItems);
  59. this.updateMainHeight();
  60. },
  61. mainActiveIndex: 'updateSubItems'
  62. },
  63. methods: {
  64. // 当一个子项被选择时
  65. onSelectItem(event) {
  66. const { item } = event.currentTarget.dataset;
  67. if (!item.disabled) {
  68. this.$emit('click-item', item);
  69. }
  70. },
  71. // 当一个导航被点击时
  72. onClickNav(event) {
  73. const { index } = event.currentTarget.dataset;
  74. const item = this.data.items[index];
  75. if (!item.disabled) {
  76. this.$emit('click-nav', { index });
  77. }
  78. },
  79. // 更新子项列表
  80. updateSubItems() {
  81. const { items, mainActiveIndex } = this.data;
  82. const { children = [] } = items[mainActiveIndex] || {};
  83. this.updateItemHeight(children);
  84. return this.set({ subItems: children });
  85. },
  86. // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定
  87. updateMainHeight() {
  88. const { items = [], subItems = [] } = this.data;
  89. const maxHeight = Math.max(items.length * ITEM_HEIGHT, subItems.length * ITEM_HEIGHT);
  90. this.set({ mainHeight: Math.min(maxHeight, this.data.maxHeight) });
  91. },
  92. // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定
  93. updateItemHeight(subItems) {
  94. const itemHeight = Math.min(subItems.length * ITEM_HEIGHT, this.data.maxHeight);
  95. return this.set({ itemHeight });
  96. }
  97. }
  98. });
  99. export default global['__wxComponents']['vant/tree-select/index']
  100. </script>
  101. <style platform="mp-weixin">
  102. @import '../common/index.css';.van-tree-select{position:relative;font-size:14px;-webkit-user-select:none;user-select:none}.van-tree-select__nav{position:absolute;top:0;bottom:0;left:0;width:35%;min-width:120px;background-color:#fafafa}.van-tree-select__nitem{position:relative;padding:0 9px 0 15px;line-height:44px}.van-tree-select__nitem--active:after{position:absolute;top:0;bottom:0;left:0;width:3.6px;background-color:#f44;content:""}.van-tree-select__nitem--active{font-weight:700;background-color:#fff}.van-tree-select__nitem--disabled{color:#999}.van-tree-select__content{width:65%;padding-left:15px;margin-left:35%;background-color:#fff;box-sizing:border-box}.van-tree-select__item{position:relative;font-weight:700;line-height:44px}.van-tree-select__item--active{color:#f44}.van-tree-select__item--disabled{color:#999}.van-tree-select__selected{position:absolute;top:0;right:15px;bottom:0;height:24px;margin:auto 0;line-height:24px}
  103. </style>