index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <uni-shadow-root class="vant-dropdown-menu-index"><view class="van-dropdown-menu van-dropdown-menu--top-bottom">
  3. <view v-for="(item,index) in (itemListData)" :key="item.index" :data-index="index" :class="utils.bem('dropdown-menu__item', { disabled: item.disabled })" @click="onTitleTap">
  4. <view :class="(item.titleClass)+' '+(utils.bem('dropdown-menu__title', { active: item.showPopup, down: item.showPopup === (direction === 'down') }))" :style="item.showPopup ? 'color:' + activeColor : ''">
  5. <view class="van-ellipsis">
  6. {{ computed.displayTitle(item) }}
  7. </view>
  8. </view>
  9. </view>
  10. <slot></slot>
  11. </view></uni-shadow-root>
  12. </template>
  13. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  14. <script>
  15. global['__wxRoute'] = 'vant/dropdown-menu/index'
  16. import { VantComponent } from '../common/component';
  17. import { addUnit } from '../common/utils';
  18. let ARRAY = [];
  19. VantComponent({
  20. field: true,
  21. relation: {
  22. name: 'dropdown-item',
  23. type: 'descendant',
  24. current: 'dropdown-menu',
  25. linked() {
  26. this.updateItemListData();
  27. },
  28. unlinked() {
  29. this.updateItemListData();
  30. },
  31. },
  32. props: {
  33. activeColor: {
  34. type: String,
  35. observer: 'updateChildrenData',
  36. },
  37. overlay: {
  38. type: Boolean,
  39. value: true,
  40. observer: 'updateChildrenData',
  41. },
  42. zIndex: {
  43. type: Number,
  44. value: 10,
  45. },
  46. duration: {
  47. type: Number,
  48. value: 200,
  49. observer: 'updateChildrenData',
  50. },
  51. direction: {
  52. type: String,
  53. value: 'down',
  54. observer: 'updateChildrenData',
  55. },
  56. closeOnClickOverlay: {
  57. type: Boolean,
  58. value: true,
  59. observer: 'updateChildrenData',
  60. },
  61. closeOnClickOutside: {
  62. type: Boolean,
  63. value: true,
  64. },
  65. },
  66. data: {
  67. itemListData: [],
  68. },
  69. beforeCreate() {
  70. const { windowHeight } = wx.getSystemInfoSync();
  71. this.windowHeight = windowHeight;
  72. ARRAY.push(this);
  73. },
  74. destroyed() {
  75. ARRAY = ARRAY.filter((item) => item !== this);
  76. },
  77. methods: {
  78. updateItemListData() {
  79. this.setData({
  80. itemListData: this.children.map((child) => child.data),
  81. });
  82. },
  83. updateChildrenData() {
  84. this.children.forEach((child) => {
  85. child.updateDataFromParent();
  86. });
  87. },
  88. toggleItem(active) {
  89. this.children.forEach((item, index) => {
  90. const { showPopup } = item.data;
  91. if (index === active) {
  92. item.toggle();
  93. } else if (showPopup) {
  94. item.toggle(false, { immediate: true });
  95. }
  96. });
  97. },
  98. close() {
  99. this.children.forEach((child) => {
  100. child.toggle(false, { immediate: true });
  101. });
  102. },
  103. getChildWrapperStyle() {
  104. const { zIndex, direction } = this.data;
  105. return this.getRect('.van-dropdown-menu').then((rect) => {
  106. const { top = 0, bottom = 0 } = rect;
  107. const offset = direction === 'down' ? bottom : this.windowHeight - top;
  108. let wrapperStyle = `z-index: ${zIndex};`;
  109. if (direction === 'down') {
  110. wrapperStyle += `top: ${addUnit(offset)};`;
  111. } else {
  112. wrapperStyle += `bottom: ${addUnit(offset)};`;
  113. }
  114. return wrapperStyle;
  115. });
  116. },
  117. onTitleTap(event) {
  118. const { index } = event.currentTarget.dataset;
  119. const child = this.children[index];
  120. if (!child.data.disabled) {
  121. ARRAY.forEach((menuItem) => {
  122. if (
  123. menuItem &&
  124. menuItem.data.closeOnClickOutside &&
  125. menuItem !== this
  126. ) {
  127. menuItem.close();
  128. }
  129. });
  130. this.toggleItem(index);
  131. }
  132. },
  133. },
  134. });
  135. export default global['__wxComponents']['vant/dropdown-menu/index']
  136. </script>
  137. <style platform="mp-weixin">
  138. @import '../common/index.css';.van-dropdown-menu{display:-webkit-flex;display:flex;-webkit-user-select:none;user-select:none;height:50px;height:var(--dropdown-menu-height,50px);background-color:#fff;background-color:var(--dropdown-menu-background-color,#fff)}.van-dropdown-menu__item{display:-webkit-flex;display:flex;-webkit-flex:1;flex:1;-webkit-align-items:center;align-items:center;-webkit-justify-content:center;justify-content:center;min-width:0}.van-dropdown-menu__item:active{opacity:.7}.van-dropdown-menu__item--disabled:active{opacity:1}.van-dropdown-menu__item--disabled .van-dropdown-menu__title{color:#969799;color:var(--dropdown-menu-title-disabled-text-color,#969799)}.van-dropdown-menu__title{position:relative;box-sizing:border-box;max-width:100%;padding:0 8px;padding:var(--dropdown-menu-title-padding,0 8px);color:#323233;color:var(--dropdown-menu-title-text-color,#323233);font-size:15px;font-size:var(--dropdown-menu-title-font-size,15px);line-height:18px;line-height:var(--dropdown-menu-title-line-height,18px)}.van-dropdown-menu__title:after{position:absolute;top:50%;right:-4px;margin-top:-5px;border-color:transparent transparent currentcolor currentcolor;border-style:solid;border-width:3px;-webkit-transform:rotate(-45deg);transform:rotate(-45deg);opacity:.8;content:""}.van-dropdown-menu__title--active{color:#1989fa;color:var(--dropdown-menu-title-active-text-color,#1989fa)}.van-dropdown-menu__title--down:after{margin-top:-1px;-webkit-transform:rotate(135deg);transform:rotate(135deg)}
  139. </style>