海鲜小程序

index.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. classes: [
  4. 'main-item-class',
  5. 'content-item-class',
  6. 'main-active-class',
  7. 'content-active-class',
  8. 'main-disabled-class',
  9. 'content-disabled-class',
  10. ],
  11. props: {
  12. items: {
  13. type: Array,
  14. observer: 'updateSubItems',
  15. },
  16. activeId: null,
  17. mainActiveIndex: {
  18. type: Number,
  19. value: 0,
  20. observer: 'updateSubItems',
  21. },
  22. height: {
  23. type: [Number, String],
  24. value: 300,
  25. },
  26. max: {
  27. type: Number,
  28. value: Infinity,
  29. },
  30. },
  31. data: {
  32. subItems: [],
  33. },
  34. methods: {
  35. // 当一个子项被选择时
  36. onSelectItem(event) {
  37. const { item } = event.currentTarget.dataset;
  38. const isArray = Array.isArray(this.data.activeId);
  39. // 判断有没有超出右侧选择的最大数
  40. const isOverMax = isArray && this.data.activeId.length >= this.data.max;
  41. // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件
  42. const isSelected = isArray
  43. ? this.data.activeId.indexOf(item.id) > -1
  44. : this.data.activeId === item.id;
  45. if (!item.disabled && (!isOverMax || isSelected)) {
  46. this.$emit('click-item', item);
  47. }
  48. },
  49. // 当一个导航被点击时
  50. onClickNav(event) {
  51. const index = event.detail;
  52. const item = this.data.items[index];
  53. if (!item.disabled) {
  54. this.$emit('click-nav', { index });
  55. }
  56. },
  57. // 更新子项列表
  58. updateSubItems() {
  59. const { items, mainActiveIndex } = this.data;
  60. const { children = [] } = items[mainActiveIndex] || {};
  61. return this.set({ subItems: children });
  62. },
  63. },
  64. });