index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <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
  6. custom-class="main-item-class"
  7. active-class="main-active-class"
  8. disabled-class="main-disabled-class"
  9. :title="item.text"
  10. :disabled="item.disabled"
  11. v-for="(item, index) in items"
  12. :key="index"
  13. ></van-sidebar-item>
  14. </van-sidebar>
  15. </scroll-view>
  16. <scroll-view scroll-y class="van-tree-select__content">
  17. <slot name="content" />
  18. <view
  19. :class="
  20. 'van-ellipsis content-item-class ' +
  21. utils.bem('tree-select__item', { active: wxs.isActive(activeId, item.id), disabled: item.disabled }) +
  22. ' ' +
  23. (wxs.isActive(activeId, item.id) ? 'content-active-class' : '') +
  24. ' ' +
  25. (item.disabled ? 'content-disabled-class' : '')
  26. "
  27. :data-item="item"
  28. @tap="onSelectItem"
  29. v-for="(item, index) in subItems"
  30. :key="index"
  31. >
  32. {{ item.text }}
  33. <van-icon v-if="wxs.isActive(activeId, item.id)" name="checked" size="16px" class="van-tree-select__selected" />
  34. </view>
  35. </scroll-view>
  36. </view>
  37. </template>
  38. <script module="utils" lang="wxs" src="@/miniprogram_npm/@vant/weapp/wxs/utils.wxs"></script>
  39. <script module="wxs" lang="wxs" src="@/miniprogram_npm/@vant/weapp/tree-select/index.wxs"></script>
  40. <script>
  41. 'use strict';
  42. Object.defineProperty(exports, '__esModule', {
  43. value: true
  44. });
  45. var component_1 = require('../common/component');
  46. component_1.VantComponent({
  47. classes: ['main-item-class', 'content-item-class', 'main-active-class', 'content-active-class', 'main-disabled-class', 'content-disabled-class'],
  48. props: {
  49. items: {
  50. type: Array,
  51. observer: 'updateSubItems'
  52. },
  53. activeId: null,
  54. mainActiveIndex: {
  55. type: Number,
  56. value: 0,
  57. observer: 'updateSubItems'
  58. },
  59. height: {
  60. type: [Number, String],
  61. value: 300
  62. },
  63. max: {
  64. type: Number,
  65. value: Infinity
  66. }
  67. },
  68. data: {
  69. subItems: []
  70. },
  71. methods: {
  72. // 当一个子项被选择时
  73. onSelectItem: function (event) {
  74. var item = event.currentTarget.dataset.item;
  75. var isArray = Array.isArray(this.activeId);
  76. // 判断有没有超出右侧选择的最大数
  77. var isOverMax = isArray && this.activeId.length >= this.max;
  78. // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件
  79. var isSelected = isArray ? this.activeId.indexOf(item.id) > -1 : this.activeId === item.id;
  80. if (!item.disabled && (!isOverMax || isSelected)) {
  81. this.$emit('click-item', item);
  82. }
  83. },
  84. // 当一个导航被点击时
  85. onClickNav: function (event) {
  86. var index = event.detail;
  87. var item = this.items[index];
  88. if (!item.disabled) {
  89. this.$emit('click-nav', {
  90. index: index
  91. });
  92. }
  93. },
  94. // 更新子项列表
  95. updateSubItems: function () {
  96. var _a = this;
  97. var items = _a.items;
  98. var mainActiveIndex = _a.mainActiveIndex;
  99. var _b = (items[mainActiveIndex] || {}).children;
  100. var children = _b === void 0 ? [] : _b;
  101. return this.set({
  102. subItems: children
  103. });
  104. }
  105. }
  106. });
  107. </script>
  108. <style>
  109. @import './index.css';
  110. </style>