index.vue 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <uni-shadow-root class="vant-dist-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/dist/dropdown-menu/index'
  16. import { VantComponent } from '../common/component';
  17. import { useChildren } from '../common/relation';
  18. import { addUnit, getRect, getSystemInfoSync } from '../common/utils';
  19. let ARRAY = [];
  20. VantComponent({
  21. field: true,
  22. relation: useChildren('dropdown-item', function () {
  23. this.updateItemListData();
  24. }),
  25. props: {
  26. activeColor: {
  27. type: String,
  28. observer: 'updateChildrenData',
  29. },
  30. overlay: {
  31. type: Boolean,
  32. value: true,
  33. observer: 'updateChildrenData',
  34. },
  35. zIndex: {
  36. type: Number,
  37. value: 10,
  38. },
  39. duration: {
  40. type: Number,
  41. value: 200,
  42. observer: 'updateChildrenData',
  43. },
  44. direction: {
  45. type: String,
  46. value: 'down',
  47. observer: 'updateChildrenData',
  48. },
  49. closeOnClickOverlay: {
  50. type: Boolean,
  51. value: true,
  52. observer: 'updateChildrenData',
  53. },
  54. closeOnClickOutside: {
  55. type: Boolean,
  56. value: true,
  57. },
  58. },
  59. data: {
  60. itemListData: [],
  61. },
  62. beforeCreate() {
  63. const { windowHeight } = getSystemInfoSync();
  64. this.windowHeight = windowHeight;
  65. ARRAY.push(this);
  66. },
  67. destroyed() {
  68. ARRAY = ARRAY.filter((item) => item !== this);
  69. },
  70. methods: {
  71. updateItemListData() {
  72. this.setData({
  73. itemListData: this.children.map((child) => child.data),
  74. });
  75. },
  76. updateChildrenData() {
  77. this.children.forEach((child) => {
  78. child.updateDataFromParent();
  79. });
  80. },
  81. toggleItem(active) {
  82. this.children.forEach((item, index) => {
  83. const { showPopup } = item.data;
  84. if (index === active) {
  85. item.toggle();
  86. }
  87. else if (showPopup) {
  88. item.toggle(false, { immediate: true });
  89. }
  90. });
  91. },
  92. close() {
  93. this.children.forEach((child) => {
  94. child.toggle(false, { immediate: true });
  95. });
  96. },
  97. getChildWrapperStyle() {
  98. const { zIndex, direction } = this.data;
  99. return getRect(this, '.van-dropdown-menu').then((rect) => {
  100. const { top = 0, bottom = 0 } = rect;
  101. const offset = direction === 'down' ? bottom : this.windowHeight - top;
  102. let wrapperStyle = `z-index: ${zIndex};`;
  103. if (direction === 'down') {
  104. wrapperStyle += `top: ${addUnit(offset)};`;
  105. }
  106. else {
  107. wrapperStyle += `bottom: ${addUnit(offset)};`;
  108. }
  109. return wrapperStyle;
  110. });
  111. },
  112. onTitleTap(event) {
  113. const { index } = event.currentTarget.dataset;
  114. const child = this.children[index];
  115. if (!child.data.disabled) {
  116. ARRAY.forEach((menuItem) => {
  117. if (menuItem &&
  118. menuItem.data.closeOnClickOutside &&
  119. menuItem !== this) {
  120. menuItem.close();
  121. }
  122. });
  123. this.toggleItem(index);
  124. }
  125. },
  126. },
  127. });
  128. export default global['__wxComponents']['vant/dist/dropdown-menu/index']
  129. </script>
  130. <style platform="mp-weixin">
  131. @import '../common/index.css';.van-dropdown-menu{display:flex;box-shadow:0 2px 12px rgba(100,101,102,.12);-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:flex;flex:1;align-items: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;transform:rotate(-45deg);opacity:.8;content:""}.van-dropdown-menu__title--active{color:#ee0a24;color:var(--dropdown-menu-title-active-text-color,#ee0a24)}.van-dropdown-menu__title--down:after{margin-top:-1px;transform:rotate(135deg)}
  132. </style>