index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { VantComponent } from '../common/component';
  2. var ITEM_HEIGHT = 44;
  3. VantComponent({
  4. classes: ['main-item-class', 'content-item-class', 'main-active-class', 'content-active-class', 'main-disabled-class', 'content-disabled-class'],
  5. props: {
  6. items: Array,
  7. mainActiveIndex: {
  8. type: Number,
  9. value: 0
  10. },
  11. activeId: {
  12. type: [Number, String]
  13. },
  14. maxHeight: {
  15. type: Number,
  16. value: 300
  17. }
  18. },
  19. data: {
  20. subItems: [],
  21. mainHeight: 0,
  22. itemHeight: 0
  23. },
  24. watch: {
  25. items: function items() {
  26. this.updateSubItems();
  27. this.updateMainHeight();
  28. },
  29. maxHeight: function maxHeight() {
  30. this.updateItemHeight();
  31. this.updateMainHeight();
  32. },
  33. mainActiveIndex: 'updateSubItems'
  34. },
  35. methods: {
  36. // 当一个子项被选择时
  37. onSelectItem: function onSelectItem(event) {
  38. var item = event.currentTarget.dataset.item;
  39. if (!item.disabled) {
  40. this.$emit('click-item', item);
  41. }
  42. },
  43. // 当一个导航被点击时
  44. onClickNav: function onClickNav(event) {
  45. var index = event.currentTarget.dataset.index;
  46. var item = this.data.items[index];
  47. if (!item.disabled) {
  48. this.$emit('click-nav', {
  49. index: index
  50. });
  51. }
  52. },
  53. // 更新子项列表
  54. updateSubItems: function updateSubItems() {
  55. var selectedItem = this.data.items[this.data.mainActiveIndex] || {};
  56. this.set({
  57. subItems: selectedItem.children || []
  58. });
  59. this.updateItemHeight();
  60. },
  61. // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定
  62. updateMainHeight: function updateMainHeight() {
  63. var maxHeight = Math.max(this.data.items.length * ITEM_HEIGHT, this.data.subItems.length * ITEM_HEIGHT);
  64. this.set({
  65. mainHeight: Math.min(maxHeight, this.data.maxHeight)
  66. });
  67. },
  68. // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定
  69. updateItemHeight: function updateItemHeight() {
  70. this.set({
  71. itemHeight: Math.min(this.data.subItems.length * ITEM_HEIGHT, this.data.maxHeight)
  72. });
  73. }
  74. }
  75. });