cwg-dropdown.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view>
  3. <view class="cwg-dropdown" :style="customStyle" @click="click">
  4. <slot></slot>
  5. </view>
  6. <view class="cwg-dropdown-menu">
  7. <view class="cwg-dropdown-menu-container" :style="[layout]" @click.stop>
  8. <slot name="menu">
  9. <view class="menu">
  10. <slot name="btn"></slot>
  11. <view class="menu-item" :class="{ active: props.showActive && isActive(item) }"
  12. v-for="(item, idx) in menuList" :key="idx"
  13. @click="menuClick(item, idx)">
  14. <view>{{ item.label || item }}</view>
  15. <!-- <view v-if="props.showActive && isActive(item)" class="active-icon">✓</view>-->
  16. </view>
  17. </view>
  18. </slot>
  19. </view>
  20. </view>
  21. <view class="cwg-dropdown-mask" :class="{ 'cwg-dropdown-mask-show': maskShow }"
  22. @click.stop="close" />
  23. </view>
  24. </template>
  25. <script setup>
  26. import { ref, reactive, nextTick, onMounted } from 'vue'
  27. import { queryElementRect } from '@/uni_modules/x-tools/tools/sugar.js'
  28. import { str2px, commonProps } from '@/uni_modules/x-tools/tools/com.js'
  29. // 合并 props
  30. const props = defineProps({
  31. ...commonProps,
  32. menuList: {
  33. type: Object,
  34. default: () => ['菜单1', '菜单2', '菜单3']
  35. },
  36. menuStyle: {
  37. type: Object,
  38. default: () => ({})
  39. },
  40. interspace: {
  41. type: [String, Number],
  42. default: '10rpx'
  43. },
  44. // 是否显示选中样式
  45. showActive: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 当前选中项的 key(用于匹配 menuList 中的项)
  50. activeKey: {
  51. type: [String, Number],
  52. default: ''
  53. }
  54. })
  55. const emit = defineEmits(['open', 'close', 'change', 'menuClick'])
  56. // 判断菜单项是否为选中状态
  57. const isActive = (item) => {
  58. console.log(item)
  59. if (!props.showActive || !props.activeKey) return false
  60. // 支持多种 key 字段匹配
  61. const itemKey = item.key !== undefined ? item.key :
  62. item.value !== undefined ? item.value :
  63. item.sysCode !== undefined ? item.sysCode :
  64. item.type !== undefined ? item.type : ''
  65. console.log(itemKey,props.activeKey,'key')
  66. return itemKey === props.activeKey
  67. }
  68. // 响应式数据
  69. const maskShow = ref(false)
  70. const windowInfo = reactive({
  71. width: 0,
  72. height: 0
  73. })
  74. const layout = reactive({})
  75. const innerInterspace = ref(0)
  76. // 获取系统信息
  77. const getSystemInfo = () => {
  78. return new Promise((resolve) => {
  79. if (windowInfo.width > 0) {
  80. resolve(windowInfo)
  81. } else {
  82. uni.getSystemInfo({
  83. success: (res) => {
  84. windowInfo.width = res.windowWidth
  85. windowInfo.height = res.windowHeight
  86. resolve(windowInfo)
  87. },
  88. fail: () => {
  89. setTimeout(() => getSystemInfo().then(resolve), 100)
  90. }
  91. })
  92. }
  93. })
  94. }
  95. // 点击触发器
  96. const click = async (e) => {
  97. e.stopPropagation()
  98. await getSystemInfo()
  99. const triggerRect = await queryElementRect('.cwg-dropdown')
  100. if (!triggerRect) return
  101. const tempStyle = {
  102. transform: 'scaleY(1)',
  103. visibility: 'hidden',
  104. top: '-9999px',
  105. left: '-9999px',
  106. transition: 'none'
  107. }
  108. Object.assign(layout, tempStyle)
  109. await nextTick()
  110. const menuRect = await queryElementRect('.cwg-dropdown-menu-container')
  111. if (!menuRect) {
  112. Object.keys(layout).forEach(key => delete layout[key])
  113. layout.transform = 'scaleY(0)'
  114. return
  115. }
  116. const { width: winWidth } = windowInfo
  117. const { left, right, bottom } = triggerRect
  118. const interspaceVal = innerInterspace.value
  119. const finalLayout = {
  120. transition: 'transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275)',
  121. transform: 'scaleY(1)',
  122. top: `10px`
  123. }
  124. if (left + menuRect.width < winWidth) {
  125. finalLayout.left = `-110px`
  126. } else {
  127. const val = winWidth - right
  128. finalLayout.right = `${val > 0 ? val : 0}px`
  129. }
  130. Object.keys(layout).forEach(key => delete layout[key])
  131. Object.assign(layout, finalLayout)
  132. maskShow.value = true
  133. emit('open')
  134. emit('change', true)
  135. }
  136. const menuClick = (value, index) => {
  137. emit('menuClick', { value, index })
  138. close()
  139. }
  140. const close = () => {
  141. maskShow.value = false
  142. Object.keys(layout).forEach(key => delete layout[key])
  143. layout.transform = 'scaleY(0)'
  144. emit('close')
  145. emit('change', false)
  146. }
  147. onMounted(() => {
  148. getSystemInfo()
  149. innerInterspace.value = str2px(props.interspace)
  150. })
  151. // 暴露方法
  152. defineExpose({
  153. close
  154. })
  155. </script>
  156. <style lang="scss" scoped>
  157. @import '@/uni.scss';
  158. .cwg-dropdown {
  159. width: fit-content;
  160. position: relative;
  161. overflow: hidden;
  162. }
  163. .cwg-dropdown-menu {
  164. position: relative;
  165. z-index: 1000;
  166. }
  167. .cwg-dropdown-mask {
  168. position: fixed;
  169. top: 0;
  170. left: 0;
  171. width: 100vw;
  172. height: 100vh;
  173. z-index: 999;
  174. transition: all 0.3s;
  175. opacity: 0;
  176. pointer-events: none;
  177. background-color: rgba(0, 0, 0, 0);
  178. }
  179. .cwg-dropdown-mask-show {
  180. opacity: 1;
  181. pointer-events: auto;
  182. }
  183. .cwg-dropdown-menu-container {
  184. position: absolute;
  185. transform-origin: top;
  186. transform: scaleY(0);
  187. .menu {
  188. position: relative;
  189. background: #FFFFFF;
  190. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  191. border-radius: px2rpx(4);
  192. overflow: hidden;
  193. .menu-item {
  194. min-width: px2rpx(150);
  195. display: flex;
  196. align-items: center;
  197. padding: px2rpx(10) px2rpx(16);
  198. font-size: px2rpx(14);
  199. line-height: px2rpx(30);
  200. color: #333;
  201. transition: background 0.2s;
  202. box-sizing: border-box;
  203. cursor: pointer;
  204. &:last-child {
  205. border-bottom: none;
  206. }
  207. text {
  208. flex: 1;
  209. line-height: 1.4;
  210. }
  211. &:hover {
  212. background-color: rgba(0, 0, 0, 0.05);
  213. }
  214. &:active {
  215. background-color: rgba(0, 0, 0, 0.05);
  216. }
  217. &.active {
  218. background-color: rgba(234, 0, 42, 0.1);
  219. color: #ea002a;
  220. }
  221. }
  222. }
  223. }
  224. </style>