index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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;" overlay-style="position: absolute;" :overlay="overlay" :position="direction === 'down' ? 'top' : 'bottom'" :duration="transition ? duration : 0" :close-on-click-overlay="closeOnClickOverlay" @close="onClickOverlay">
  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. linked(target) {
  28. this.parent = target;
  29. this.updateDataFromParent();
  30. },
  31. unlinked() {
  32. this.parent = null;
  33. }
  34. },
  35. props: {
  36. value: {
  37. type: null,
  38. observer: 'rerender'
  39. },
  40. title: {
  41. type: String,
  42. observer: 'rerender'
  43. },
  44. disabled: Boolean,
  45. titleClass: {
  46. type: String,
  47. observer: 'rerender'
  48. },
  49. options: {
  50. type: Array,
  51. value: [],
  52. observer: 'rerender'
  53. }
  54. },
  55. data: {
  56. transition: true,
  57. showPopup: false,
  58. showWrapper: false,
  59. displayTitle: ''
  60. },
  61. methods: {
  62. rerender() {
  63. wx.nextTick(() => {
  64. this.parent && this.parent.updateItemListData();
  65. });
  66. },
  67. updateDataFromParent() {
  68. if (this.parent) {
  69. const { overlay, duration, activeColor, closeOnClickOverlay, direction } = this.parent.data;
  70. this.setData({
  71. overlay,
  72. duration,
  73. activeColor,
  74. closeOnClickOverlay,
  75. direction
  76. });
  77. }
  78. },
  79. onClickOverlay() {
  80. this.toggle();
  81. this.$emit('close');
  82. },
  83. onOptionTap(event) {
  84. const { option } = event.currentTarget.dataset;
  85. const { value } = option;
  86. const shouldEmitChange = this.data.value !== value;
  87. this.setData({ showPopup: false, value });
  88. setTimeout(() => {
  89. this.setData({ showWrapper: false });
  90. }, this.data.duration || 0);
  91. this.rerender();
  92. if (shouldEmitChange) {
  93. this.$emit('change', value);
  94. }
  95. },
  96. toggle(show, options = {}) {
  97. const { showPopup, duration } = this.data;
  98. if (show == null) {
  99. show = !showPopup;
  100. }
  101. if (show === showPopup) {
  102. return;
  103. }
  104. if (!show) {
  105. const time = options.immediate ? 0 : duration;
  106. this.setData({ transition: !options.immediate, showPopup: show });
  107. setTimeout(() => {
  108. this.setData({ showWrapper: false });
  109. }, time);
  110. this.rerender();
  111. return;
  112. }
  113. this.parent.getChildWrapperStyle().then((wrapperStyle = '') => {
  114. this.setData({
  115. transition: !options.immediate,
  116. showPopup: show,
  117. wrapperStyle,
  118. showWrapper: true
  119. });
  120. this.rerender();
  121. });
  122. }
  123. }
  124. });
  125. export default global['__wxComponents']['vant/dropdown-item/index']
  126. </script>
  127. <style platform="mp-weixin">
  128. @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}
  129. </style>