uv-drop-down-popup.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="uv-drop-down-popup">
  3. <uv-transition :show="show" mode="fade" :duration="300" :custom-style="overlayStyle" @click="clickOverlay">
  4. <view class="uv-dp__container" ref="uvDPContainer" :style="{height: `${height}px`}" @click.stop="blockClick">
  5. <view class="uv-dp__container__list" ref="uvDPList">
  6. <slot>
  7. <view class="uv-dp__container__list--item" v-for="(item,index) in list" :key="index" @click="clickHandler(item,index)" :style="[itemCustomStyle(index)]">
  8. <uv-text :text="item[keyName]" :size="getTextSize(index)" :color="getTextColor(index)"></uv-text>
  9. </view>
  10. </slot>
  11. </view>
  12. </view>
  13. </uv-transition>
  14. </view>
  15. </template>
  16. <script>
  17. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js';
  18. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js';
  19. // #ifdef APP-NVUE
  20. const animation = uni.requireNativePlugin('animation');
  21. const dom = uni.requireNativePlugin('dom');
  22. // #endif
  23. /**
  24. * DropDownPopup 下拉框
  25. * @description 下拉筛选框
  26. * @tutorial https://ext.dcloud.net.cn/plugin?name=uv-drop-down
  27. * @property {String | Number} name 字段标识
  28. * @property {String | Number} zIndex 弹出层的层级
  29. * @property {String | Number} opacity 遮罩层的透明度
  30. * @property {Boolean} clickOverlayOnClose 是否允许点击遮罩层关闭弹窗
  31. * @property {Object} currentDropItem 当前下拉筛选菜单对象
  32. * @property {String} keyName 指定从当前下拉筛选菜单对象元素中读取哪个属性作为文本展示
  33. */
  34. export default {
  35. name: 'uv-drop-down-popup',
  36. mixins: [mpMixin, mixin],
  37. props: {
  38. sign: {
  39. type: [String, Number],
  40. default: 'UVDROPDOWN'
  41. },
  42. zIndex: {
  43. type: [Number, String],
  44. default: 999
  45. },
  46. opacity: {
  47. type: [Number, String],
  48. default: 0.5
  49. },
  50. clickOverlayOnClose: {
  51. type: Boolean,
  52. default: true
  53. },
  54. // 当前下拉选项对象
  55. currentDropItem: {
  56. type: Object,
  57. default () {
  58. return {
  59. activeIndex: 0,
  60. child: []
  61. }
  62. }
  63. },
  64. keyName: {
  65. type: String,
  66. default: 'label'
  67. }
  68. },
  69. data() {
  70. return {
  71. show: false,
  72. rect: {},
  73. height: 0
  74. }
  75. },
  76. computed: {
  77. overlayStyle() {
  78. let { height = 0, top = 0 } = this.rect;
  79. // #ifdef H5
  80. top += this.$uv.sys().windowTop;
  81. // #endif
  82. const style = {
  83. position: 'fixed',
  84. top: `${top+height}px`,
  85. left: 0,
  86. right: 0,
  87. zIndex: this.zIndex,
  88. bottom: 0,
  89. 'background-color': `rgba(0, 0, 0, ${this.opacity})`
  90. }
  91. return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle))
  92. },
  93. list() {
  94. try {
  95. return Array.isArray(this.currentDropItem.child) ? this.currentDropItem.child : [];
  96. } catch (e) {
  97. return [];
  98. }
  99. },
  100. getTextColor(index) {
  101. return index => {
  102. const active = this.currentDropItem.activeIndex == index;
  103. const color = this.currentDropItem.color;
  104. const activeColor = this.currentDropItem.activeColor;
  105. if (active) {
  106. return activeColor ? activeColor : '#3c9cff';
  107. }
  108. return color ? color : '#333';
  109. }
  110. },
  111. getTextSize(index) {
  112. return index => {
  113. const active = this.currentDropItem.activeIndex == index;
  114. const size = this.currentDropItem.size;
  115. const activeSize = this.currentDropItem.activeSize;
  116. if (active) {
  117. return activeSize ? activeSize : '30rpx';
  118. }
  119. return size ? size : '30rpx';
  120. }
  121. },
  122. itemCustomStyle() {
  123. return index => {
  124. const active = this.currentDropItem.activeIndex == index;
  125. const style = {};
  126. if (active && this.currentDropItem.itemActiveCustomStyle) {
  127. return this.$uv.deepMerge(style, this.$uv.addStyle(this.currentDropItem.itemActiveCustomStyle));
  128. }
  129. if (this.currentDropItem.itemCustomStyle) {
  130. return this.$uv.deepMerge(style, this.$uv.addStyle(this.currentDropItem.itemCustomStyle))
  131. }
  132. return style;
  133. }
  134. }
  135. },
  136. created() {
  137. this.init();
  138. },
  139. methods: {
  140. blockClick() {},
  141. clickHandler(item, index) {
  142. this.currentDropItem.activeIndex = index;
  143. this.$emit('clickItem', item);
  144. this.close();
  145. },
  146. init() {
  147. uni.$off(`${this.sign}_GETRECT`);
  148. uni.$on(`${this.sign}_GETRECT`, rect => {
  149. this.rect = rect;
  150. })
  151. uni.$off(`${this.sign}_CLICKMENU`);
  152. uni.$on(`${this.sign}_CLICKMENU`, async res => {
  153. if (res.show) {
  154. this.open();
  155. } else {
  156. this.close();
  157. }
  158. })
  159. },
  160. open() {
  161. this.show = true;
  162. this.$nextTick(async () => {
  163. // #ifndef H5 || MP-WEIXIN
  164. await this.$uv.sleep(60);
  165. // #endif
  166. const res = await this.queryRect();
  167. // #ifndef APP-NVUE
  168. this.height = res.height;
  169. // #endif
  170. // #ifdef APP-NVUE
  171. this.animation(res.height);
  172. // #endif
  173. this.$emit('popupChange', { show: true });
  174. })
  175. },
  176. close() {
  177. if(!this.show) return;
  178. this.height = 0;
  179. // #ifndef APP-NVUE
  180. this.height = 0;
  181. // #endif
  182. // #ifdef APP-NVUE
  183. this.animation(0);
  184. // #endif
  185. this.show = false;
  186. uni.$emit(`${this.sign}_CLOSEPOPUP`);
  187. this.$emit('popupChange', { show: false });
  188. },
  189. clickOverlay() {
  190. if (this.clickOverlayOnClose) {
  191. this.close();
  192. }
  193. },
  194. // 查询内容高度
  195. queryRect() {
  196. // #ifndef APP-NVUE
  197. // 组件内部一般用this.$uvGetRect,对外的为getRect,二者功能一致,名称不同
  198. return new Promise(resolve => {
  199. this.$uvGetRect(`.uv-dp__container__list`).then(size => {
  200. resolve(size)
  201. })
  202. })
  203. // #endif
  204. // #ifdef APP-NVUE
  205. // nvue下,使用dom模块查询元素高度
  206. // 返回一个promise,让调用此方法的主体能使用then回调
  207. return new Promise(resolve => {
  208. dom.getComponentRect(this.$refs.uvDPList, res => {
  209. resolve(res.size)
  210. })
  211. })
  212. // #endif
  213. },
  214. // nvue下设置高度
  215. animation(height, duration = 200) {
  216. // #ifdef APP-NVUE
  217. const ref = this.$refs['uvDPContainer'];
  218. animation.transition(ref, {
  219. styles: {
  220. height: `${height}px`
  221. },
  222. duration
  223. })
  224. // #endif
  225. }
  226. }
  227. }
  228. </script>
  229. <style scoped lang="scss">
  230. .uv-dp__container {
  231. /* #ifndef APP-NVUE */
  232. overflow: hidden;
  233. transition: all .15s;
  234. /* #endif */
  235. background-color: #fff;
  236. }
  237. .uv-dp__container__list {
  238. &--item {
  239. padding: 20rpx 60rpx;
  240. }
  241. }
  242. </style>