index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <uni-shadow-root class="vant-dropdown-item-index"><view v-if="showWrapper" :class="(utils.bem('dropdown-item', direction))+' custom-class'" :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" :safe-area-tab-bar="safeAreaTabBar" :close-on-click-overlay="closeOnClickOverlay" :rootPortal="rootPortal" @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 item-title-class" :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 { useParent } from '../common/relation';
  22. import { VantComponent } from '../common/component';
  23. VantComponent({
  24. classes: ['item-title-class'],
  25. field: true,
  26. relation: useParent('dropdown-menu', function () {
  27. this.updateDataFromParent();
  28. }),
  29. props: {
  30. value: {
  31. type: null,
  32. observer: 'rerender',
  33. },
  34. title: {
  35. type: String,
  36. observer: 'rerender',
  37. },
  38. disabled: Boolean,
  39. titleClass: {
  40. type: String,
  41. observer: 'rerender',
  42. },
  43. options: {
  44. type: Array,
  45. value: [],
  46. observer: 'rerender',
  47. },
  48. popupStyle: String,
  49. useBeforeToggle: {
  50. type: Boolean,
  51. value: false,
  52. },
  53. rootPortal: {
  54. type: Boolean,
  55. value: false,
  56. },
  57. },
  58. data: {
  59. transition: true,
  60. showPopup: false,
  61. showWrapper: false,
  62. displayTitle: '',
  63. safeAreaTabBar: false,
  64. },
  65. methods: {
  66. rerender() {
  67. wx.nextTick(() => {
  68. var _a;
  69. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.updateItemListData();
  70. });
  71. },
  72. updateDataFromParent() {
  73. if (this.parent) {
  74. const { overlay, duration, activeColor, closeOnClickOverlay, direction, safeAreaTabBar, } = this.parent.data;
  75. this.setData({
  76. overlay,
  77. duration,
  78. activeColor,
  79. closeOnClickOverlay,
  80. direction,
  81. safeAreaTabBar,
  82. });
  83. }
  84. },
  85. onOpen() {
  86. this.$emit('open');
  87. },
  88. onOpened() {
  89. this.$emit('opened');
  90. },
  91. onClose() {
  92. this.$emit('close');
  93. },
  94. onClosed() {
  95. this.$emit('closed');
  96. this.setData({ showWrapper: false });
  97. },
  98. onOptionTap(event) {
  99. const { option } = event.currentTarget.dataset;
  100. const { value } = option;
  101. const shouldEmitChange = this.data.value !== value;
  102. this.setData({ showPopup: false, value });
  103. this.$emit('close');
  104. this.rerender();
  105. if (shouldEmitChange) {
  106. this.$emit('change', value);
  107. }
  108. },
  109. toggle(show, options = {}) {
  110. const { showPopup } = this.data;
  111. if (typeof show !== 'boolean') {
  112. show = !showPopup;
  113. }
  114. if (show === showPopup) {
  115. return;
  116. }
  117. this.onBeforeToggle(show).then((status) => {
  118. var _a;
  119. if (!status) {
  120. return;
  121. }
  122. this.setData({
  123. transition: !options.immediate,
  124. showPopup: show,
  125. });
  126. if (show) {
  127. (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getChildWrapperStyle().then((wrapperStyle) => {
  128. this.setData({ wrapperStyle, showWrapper: true });
  129. this.rerender();
  130. });
  131. }
  132. else {
  133. this.rerender();
  134. }
  135. });
  136. },
  137. onBeforeToggle(status) {
  138. const { useBeforeToggle } = this.data;
  139. if (!useBeforeToggle) {
  140. return Promise.resolve(true);
  141. }
  142. return new Promise((resolve) => {
  143. this.$emit('before-toggle', {
  144. status,
  145. callback: (value) => resolve(value),
  146. });
  147. });
  148. },
  149. },
  150. });
  151. export default global['__wxComponents']['vant/dropdown-item/index']
  152. </script>
  153. <style platform="mp-weixin">
  154. @import '../common/index.css';.van-dropdown-item{left:0;overflow:hidden;position:fixed;right:0}.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:var(--dropdown-menu-option-active-color,#ee0a24)}.van-dropdown-item--up{top:0}.van-dropdown-item--down{bottom:0}.van-dropdown-item__icon{display:block;line-height:inherit}
  155. </style>