index.js 2.8 KB

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