index.js 3.5 KB

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