index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <uni-shadow-root class="vant-dropdown-item-index"><view v-if="showWrapper" :class="utils.bem('dropdown-item', direction)" :style="wrapperStyle">
  3. <van-popup :show="showPopup" :custom-style="'position: absolute;'+(popupStyle)" overlay-style="position: absolute;" :overlay="overlay" :position="direction === 'down' ? 'top' : 'bottom'" :duration="transition ? duration : 0" :close-on-click-overlay="closeOnClickOverlay" @enter="onOpen" @leave="onClose" @close="toggle" @after-enter="onOpened" @after-leave="onClosed">
  4. <van-cell v-for="(item,index) in (options)" :key="item.value" :data-option="item" :class="utils.bem('dropdown-item__option', { active: item.value === value } )" clickable :icon="item.icon" @click.native="onOptionTap">
  5. <view slot="title" class="van-dropdown-item__title" :style="item.value === value ? 'color:' + activeColor : ''">
  6. {{ item.text }}
  7. </view>
  8. <van-icon v-if="item.value === value" name="success" class="van-dropdown-item__icon" :color="activeColor"></van-icon>
  9. </van-cell>
  10. <slot></slot>
  11. </van-popup>
  12. </view></uni-shadow-root>
  13. </template>
  14. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  15. <script>
  16. import VanPopup from '../popup/index.vue'
  17. import VanCell from '../cell/index.vue'
  18. import VanIcon from '../icon/index.vue'
  19. global['__wxVueOptions'] = {components:{'van-popup': VanPopup,'van-cell': VanCell,'van-icon': VanIcon}}
  20. global['__wxRoute'] = 'vant/dropdown-item/index'
  21. import { VantComponent } from '../common/component';
  22. VantComponent({
  23. field: true,
  24. relation: {
  25. name: 'dropdown-menu',
  26. type: 'ancestor',
  27. current: 'dropdown-item',
  28. linked() {
  29. this.updateDataFromParent();
  30. },
  31. },
  32. props: {
  33. value: {
  34. type: null,
  35. observer: 'rerender',
  36. },
  37. title: {
  38. type: String,
  39. observer: 'rerender',
  40. },
  41. disabled: Boolean,
  42. titleClass: {
  43. type: String,
  44. observer: 'rerender',
  45. },
  46. options: {
  47. type: Array,
  48. value: [],
  49. observer: 'rerender',
  50. },
  51. popupStyle: String,
  52. },
  53. data: {
  54. transition: true,
  55. showPopup: false,
  56. showWrapper: false,
  57. displayTitle: '',
  58. },
  59. methods: {
  60. rerender() {
  61. wx.nextTick(() => {
  62. this.parent && this.parent.updateItemListData();
  63. });
  64. },
  65. updateDataFromParent() {
  66. if (this.parent) {
  67. const {
  68. overlay,
  69. duration,
  70. activeColor,
  71. closeOnClickOverlay,
  72. direction,
  73. } = this.parent.data;
  74. this.setData({
  75. overlay,
  76. duration,
  77. activeColor,
  78. closeOnClickOverlay,
  79. direction,
  80. });
  81. }
  82. },
  83. onOpen() {
  84. this.$emit('open');
  85. },
  86. onOpened() {
  87. this.$emit('opened');
  88. },
  89. onClose() {
  90. this.$emit('close');
  91. },
  92. onClosed() {
  93. this.$emit('closed');
  94. this.setData({ showWrapper: false });
  95. },
  96. onOptionTap(event) {
  97. const { option } = event.currentTarget.dataset;
  98. const { value } = option;
  99. const shouldEmitChange = this.data.value !== value;
  100. this.setData({ showPopup: false, value });
  101. this.$emit('close');
  102. this.rerender();
  103. if (shouldEmitChange) {
  104. this.$emit('change', value);
  105. }
  106. },
  107. toggle(show, options = {}) {
  108. const { showPopup } = this.data;
  109. if (typeof show !== 'boolean') {
  110. show = !showPopup;
  111. }
  112. if (show === showPopup) {
  113. return;
  114. }
  115. this.setData({
  116. transition: !options.immediate,
  117. showPopup: show,
  118. });
  119. if (show) {
  120. this.parent.getChildWrapperStyle().then((wrapperStyle) => {
  121. this.setData({ wrapperStyle, showWrapper: true });
  122. this.rerender();
  123. });
  124. } else {
  125. this.rerender();
  126. }
  127. },
  128. },
  129. });
  130. export default global['__wxComponents']['vant/dropdown-item/index']
  131. </script>
  132. <style platform="mp-weixin">
  133. @import '../common/index.css';.van-dropdown-item{position:fixed;right:0;left:0;overflow:hidden}.van-dropdown-item__option{text-align:left}.van-dropdown-item__option--active .van-dropdown-item__icon,.van-dropdown-item__option--active .van-dropdown-item__title{color:#1989fa;color:var(--dropdown-menu-option-active-color,#1989fa)}.van-dropdown-item--up{top:0}.van-dropdown-item--down{bottom:0}.van-dropdown-item__icon{display:block;line-height:inherit}
  134. </style>