index.vue 5.7KB

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