index.vue 5.7 KB

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