You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
8.6 KiB

2 months ago
  1. <template>
  2. <view class="u-dropdown">
  3. <view class="u-dropdown__menu" :style="{
  4. height: addUnit(height)
  5. }" :class="{
  6. 'u-border-bottom': borderBottom
  7. }">
  8. <view class="u-dropdown__menu__item" v-for="(item, index) in menuList" :key="index" @tap.stop="menuClick(index)">
  9. <view class="u-flex u-flex-row">
  10. <text class="u-dropdown__menu__item__text" :style="{
  11. color: item.disabled ? '#c0c4cc' : (index === current || highlightIndex == index) ? activeColor : inactiveColor,
  12. fontSize: addUnit(titleSize)
  13. }">{{item.title}}</text>
  14. <view class="u-dropdown__menu__item__arrow" :class="{
  15. 'u-dropdown__menu__item__arrow--rotate': index === current
  16. }">
  17. <u-icon :custom-style="{display: 'flex'}" :name="menuIcon" :size="addUnit(menuIconSize)" :color="index === current || highlightIndex == index ? activeColor : '#c0c4cc'"></u-icon>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="u-dropdown__content" :style="[contentStyle, {
  23. transition: `opacity ${duration / 1000}s linear`,
  24. top: addUnit(height),
  25. height: contentHeight + 'px'
  26. }]"
  27. @tap="maskClick" @touchmove.stop.prevent>
  28. <view @tap.stop.prevent class="u-dropdown__content__popup" :style="[popupStyle]">
  29. <slot></slot>
  30. </view>
  31. <view class="u-dropdown__content__mask"></view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { props } from './props';
  37. import { mpMixin } from '../../libs/mixin/mpMixin';
  38. import { mixin } from '../../libs/mixin/mixin';
  39. import { addUnit, sys} from '../../libs/function/index';
  40. /**
  41. * dropdown 下拉菜单
  42. * @description 该组件一般用于向下展开菜单同时可切换多个选项卡的场景
  43. * @tutorial https://ijry.github.io/uview-plus/components/dropdown.html
  44. * @property {String} active-color 标题和选项卡选中的颜色默认#2979ff
  45. * @property {String} inactive-color 标题和选项卡未选中的颜色默认#606266
  46. * @property {Boolean} close-on-click-mask 点击遮罩是否关闭菜单默认true
  47. * @property {Boolean} close-on-click-self 点击当前激活项标题是否关闭菜单默认true
  48. * @property {String | Number} duration 选项卡展开和收起的过渡时间单位ms默认300
  49. * @property {String | Number} height 标题菜单的高度单位任意默认80
  50. * @property {String | Number} border-radius 菜单展开内容下方的圆角值单位任意默认0
  51. * @property {Boolean} border-bottom 标题菜单是否显示下边框默认false
  52. * @property {String | Number} title-size 标题的字体大小单位任意数值默认为rpx单位默认28
  53. * @event {Function} open 下拉菜单被打开时触发
  54. * @event {Function} close 下拉菜单被关闭时触发
  55. * @example <u-dropdown></u-dropdown>
  56. */
  57. export default {
  58. name: 'u-dropdown',
  59. mixins: [mpMixin, mixin, props],
  60. data() {
  61. return {
  62. showDropdown: true, // 是否打开下来菜单,
  63. menuList: [], // 显示的菜单
  64. active: false, // 下拉菜单的状态
  65. // 当前是第几个菜单处于激活状态,小程序中此处不能写成false或者"",否则后续将current赋值为0,
  66. // 无能的TX没有使用===而是使用==判断,导致程序认为前后二者没有变化,从而不会触发视图更新
  67. current: 99999,
  68. // 外层内容的样式,初始时处于底层,且透明
  69. contentStyle: {
  70. zIndex: -1,
  71. opacity: 0
  72. },
  73. // 让某个菜单保持高亮的状态
  74. highlightIndex: 99999,
  75. contentHeight: 0
  76. }
  77. },
  78. computed: {
  79. // 下拉出来部分的样式
  80. popupStyle() {
  81. let style = {};
  82. // 进行Y轴位移,展开状态时,恢复原位。收齐状态时,往上位移100%,进行隐藏
  83. style.transform = `translateY(${this.active ? 0 : '-100%'})`
  84. style['transition-duration'] = this.duration / 1000 + 's';
  85. style.borderRadius = `0 0 ${addUnit(this.borderRadius)} ${addUnit(this.borderRadius)}`;
  86. return style;
  87. }
  88. },
  89. created() {
  90. // 引用所有子组件(u-dropdown-item)的this,不能在data中声明变量,否则在微信小程序会造成循环引用而报错
  91. this.children = [];
  92. },
  93. mounted() {
  94. this.getContentHeight();
  95. },
  96. emits: ['open', 'close'],
  97. methods: {
  98. addUnit,
  99. init() {
  100. // 当某个子组件内容变化时,触发父组件的init,父组件再让每一个子组件重新初始化一遍
  101. // 以保证数据的正确性
  102. this.menuList = [];
  103. this.children.map(child => {
  104. child.init();
  105. })
  106. },
  107. // 点击菜单
  108. menuClick(index) {
  109. // 判断是否被禁用
  110. if (this.menuList[index].disabled) return;
  111. // 如果点击时的索引和当前激活项索引相同,意味着点击了激活项,需要收起下拉菜单
  112. if (index === this.current && this.closeOnClickSelf) {
  113. this.close();
  114. // 等动画结束后,再移除下拉菜单中的内容,否则直接移除,也就没有下拉菜单收起的效果了
  115. setTimeout(() => {
  116. this.children[index].active = false;
  117. }, this.duration)
  118. return;
  119. }
  120. this.open(index);
  121. },
  122. // 打开下拉菜单
  123. open(index) {
  124. // 嵌套popup使用时可能获取不到正确的高度,重新计算
  125. if (this.contentHeight < 1) this.getContentHeight()
  126. // 重置高亮索引,否则会造成多个菜单同时高亮
  127. // this.highlightIndex = 9999;
  128. // 展开时,设置下拉内容的样式
  129. this.contentStyle = {
  130. zIndex: 11,
  131. }
  132. // 标记展开状态以及当前展开项的索引
  133. this.active = true;
  134. this.current = index;
  135. // 历遍所有的子元素,将索引匹配的项标记为激活状态,因为子元素是通过v-if控制切换的
  136. // 之所以不是因display: none,是因为nvue没有display这个属性
  137. this.children.map((val, idx) => {
  138. val.active = index == idx ? true : false;
  139. })
  140. this.$emit('open', this.current);
  141. },
  142. // 设置下拉菜单处于收起状态
  143. close() {
  144. this.$emit('close', this.current);
  145. // 设置为收起状态,同时current归位,设置为空字符串
  146. this.active = false;
  147. this.current = 99999;
  148. // 下拉内容的样式进行调整,不透明度设置为0
  149. this.contentStyle = {
  150. zIndex: -1,
  151. opacity: 0
  152. }
  153. },
  154. // 点击遮罩
  155. maskClick() {
  156. // 如果不允许点击遮罩,直接返回
  157. if (!this.closeOnClickMask) return;
  158. this.close();
  159. },
  160. // 外部手动设置某个菜单高亮
  161. highlight(index = undefined) {
  162. this.highlightIndex = index !== undefined ? index : 99999;
  163. },
  164. // 获取下拉菜单内容的高度
  165. getContentHeight() {
  166. // 这里的原理为,因为dropdown组件是相对定位的,它的下拉出来的内容,必须给定一个高度
  167. // 才能让遮罩占满菜单一下,直到屏幕底部的高度
  168. // sys()为uview-plus封装的获取设备信息的方法
  169. let windowHeight = sys().windowHeight;
  170. this.$uGetRect('.u-dropdown__menu').then(res => {
  171. // 这里获取的是dropdown的尺寸,在H5上,uniapp获取尺寸是有bug的(以前提出修复过,后来又出现了此bug,目前hx2.8.11版本)
  172. // H5端bug表现为元素尺寸的top值为导航栏底部到到元素的上边沿的距离,但是元素的bottom值确是导航栏顶部到元素底部的距离
  173. // 二者是互相矛盾的,本质原因是H5端导航栏非原生,uni的开发者大意造成
  174. // 这里取菜单栏的botton值合理的,不能用res.top,否则页面会造成滚动
  175. this.contentHeight = windowHeight - res.bottom;
  176. })
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped lang="scss">
  182. @import "../../libs/css/components.scss";
  183. .u-dropdown {
  184. flex: 1;
  185. width: 100%;
  186. position: relative;
  187. &__menu {
  188. @include flex;
  189. position: relative;
  190. z-index: 11;
  191. height: 80rpx;
  192. &__item {
  193. flex: 1;
  194. @include flex;
  195. justify-content: center;
  196. align-items: center;
  197. .u-flex-row {
  198. flex-direction: row;
  199. }
  200. &__text {
  201. font-size: 28rpx;
  202. color: $u-content-color;
  203. }
  204. &__arrow {
  205. margin-left: 6rpx;
  206. transition: transform .3s;
  207. align-items: center;
  208. @include flex;
  209. &--rotate {
  210. transform: rotate(180deg);
  211. }
  212. }
  213. }
  214. }
  215. &__content {
  216. position: absolute;
  217. z-index: 8;
  218. width: 100%;
  219. left: 0px;
  220. bottom: 0;
  221. overflow: hidden;
  222. &__mask {
  223. position: absolute;
  224. z-index: 9;
  225. background: rgba(0, 0, 0, .3);
  226. width: 100%;
  227. left: 0;
  228. top: 0;
  229. bottom: 0;
  230. }
  231. &__popup {
  232. position: relative;
  233. z-index: 10;
  234. transition: all 0.3s;
  235. transform: translate3D(0, -100%, 0);
  236. overflow: hidden;
  237. }
  238. }
  239. }
  240. </style>