index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { VantComponent } from '../common/component';
  2. import { useChildren } from '../common/relation';
  3. import { addUnit, getRect, getSystemInfoSync } from '../common/utils';
  4. let ARRAY = [];
  5. VantComponent({
  6. field: true,
  7. classes: ['title-class'],
  8. relation: useChildren('dropdown-item', function () {
  9. this.updateItemListData();
  10. }),
  11. props: {
  12. activeColor: {
  13. type: String,
  14. observer: 'updateChildrenData',
  15. },
  16. overlay: {
  17. type: Boolean,
  18. value: true,
  19. observer: 'updateChildrenData',
  20. },
  21. zIndex: {
  22. type: Number,
  23. value: 10,
  24. },
  25. duration: {
  26. type: Number,
  27. value: 200,
  28. observer: 'updateChildrenData',
  29. },
  30. direction: {
  31. type: String,
  32. value: 'down',
  33. observer: 'updateChildrenData',
  34. },
  35. safeAreaTabBar: {
  36. type: Boolean,
  37. value: false,
  38. },
  39. closeOnClickOverlay: {
  40. type: Boolean,
  41. value: true,
  42. observer: 'updateChildrenData',
  43. },
  44. closeOnClickOutside: {
  45. type: Boolean,
  46. value: true,
  47. },
  48. },
  49. data: {
  50. itemListData: [],
  51. },
  52. beforeCreate() {
  53. const { windowHeight } = getSystemInfoSync();
  54. this.windowHeight = windowHeight;
  55. ARRAY.push(this);
  56. },
  57. destroyed() {
  58. ARRAY = ARRAY.filter((item) => item !== this);
  59. },
  60. methods: {
  61. updateItemListData() {
  62. this.setData({
  63. itemListData: this.children.map((child) => child.data),
  64. });
  65. },
  66. updateChildrenData() {
  67. this.children.forEach((child) => {
  68. child.updateDataFromParent();
  69. });
  70. },
  71. toggleItem(active) {
  72. this.children.forEach((item, index) => {
  73. const { showPopup } = item.data;
  74. if (index === active) {
  75. item.toggle();
  76. }
  77. else if (showPopup) {
  78. item.toggle(false, { immediate: true });
  79. }
  80. });
  81. },
  82. close() {
  83. this.children.forEach((child) => {
  84. child.toggle(false, { immediate: true });
  85. });
  86. },
  87. getChildWrapperStyle() {
  88. const { zIndex, direction } = this.data;
  89. return getRect(this, '.van-dropdown-menu').then((rect) => {
  90. const { top = 0, bottom = 0 } = rect;
  91. const offset = direction === 'down' ? bottom : this.windowHeight - top;
  92. let wrapperStyle = `z-index: ${zIndex};`;
  93. if (direction === 'down') {
  94. wrapperStyle += `top: ${addUnit(offset)};`;
  95. }
  96. else {
  97. wrapperStyle += `bottom: ${addUnit(offset)};`;
  98. }
  99. return wrapperStyle;
  100. });
  101. },
  102. onTitleTap(event) {
  103. const { index } = event.currentTarget.dataset;
  104. const child = this.children[index];
  105. if (!child.data.disabled) {
  106. ARRAY.forEach((menuItem) => {
  107. if (menuItem &&
  108. menuItem.data.closeOnClickOutside &&
  109. menuItem !== this) {
  110. menuItem.close();
  111. }
  112. });
  113. this.toggleItem(index);
  114. }
  115. },
  116. },
  117. });