wd-popover.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view :class="`wd-popover ${customClass}`" :style="customStyle" id="popover" @click.stop="popover.noop">
  3. <!-- 使用插槽时无法获取正确宽高 -->
  4. <view class="wd-popover__pos wd-popover__hidden" id="pos">
  5. <view :class="`wd-popover__container ${customPop}`">
  6. <view v-if="!useContentSlot && mode === 'normal'" class="wd-popover__inner">
  7. {{ content }}
  8. </view>
  9. <view v-if="!useContentSlot && mode === 'menu' && typeof content === 'object'" class="wd-popover__menu">
  10. <view v-for="(item, index) in content" :key="index" class="wd-popover__menu-inner" @click="menuClick(index)">
  11. <wd-icon v-if="item.iconClass" :name="item.iconClass" custom-class="wd-popover__icon" />
  12. <text>{{ item.content }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. <wd-transition custom-class="wd-popover__pos" :custom-style="popover.popStyle.value" :show="showPopover" name="fade" :duration="200">
  18. <view :class="`wd-popover__container ${customPop}`">
  19. <view
  20. v-if="props.visibleArrow"
  21. :class="`wd-popover__arrow ${popover.arrowClass.value} ${customArrow}`"
  22. :style="popover.arrowStyle.value"
  23. ></view>
  24. <!-- 普通模式 -->
  25. <view v-if="!useContentSlot && mode === 'normal'" class="wd-popover__inner">
  26. {{ content }}
  27. </view>
  28. <!-- 列表模式 -->
  29. <view v-if="!useContentSlot && mode === 'menu'" class="wd-popover__menu">
  30. <view
  31. v-for="(item, index) in content"
  32. :key="index"
  33. class="wd-popover__menu-inner"
  34. @click="menuClick(index)"
  35. :style="index === 0 ? 'border-top: none' : ''"
  36. >
  37. <wd-icon v-if="typeof item === 'object' && item.iconClass" :name="item.iconClass" custom-class="wd-popover__icon" />
  38. <view style="display: inline-block">{{ typeof item === 'object' && item.content ? item.content : '' }}</view>
  39. </view>
  40. </view>
  41. <!-- 用户自定义样式 -->
  42. <slot name="content" v-else />
  43. </view>
  44. <wd-icon v-if="showClose" name="close" custom-class="wd-popover__close-icon" @click="toggle"></wd-icon>
  45. </wd-transition>
  46. <view @click="toggle" class="wd-popover__target" id="target">
  47. <slot />
  48. </view>
  49. </view>
  50. </template>
  51. <script lang="ts">
  52. export default {
  53. name: 'wd-popover',
  54. options: {
  55. virtualHost: true,
  56. addGlobalClass: true,
  57. styleIsolation: 'shared'
  58. }
  59. }
  60. </script>
  61. <script lang="ts" setup>
  62. import wdIcon from '../wd-icon/wd-icon.vue'
  63. import wdTransition from '../wd-transition/wd-transition.vue'
  64. import { getCurrentInstance, inject, onBeforeMount, onBeforeUnmount, onMounted, ref, watch } from 'vue'
  65. import { usePopover } from '../composables/usePopover'
  66. import { closeOther, pushToQueue, removeFromQueue } from '../common/clickoutside'
  67. import { type Queue, queueKey } from '../composables/useQueue'
  68. import { popoverProps, type PopoverExpose } from './types'
  69. import { isArray } from '../common/util'
  70. const props = defineProps(popoverProps)
  71. const emit = defineEmits(['update:modelValue', 'menuclick', 'change', 'open', 'close'])
  72. const queue = inject<Queue | null>(queueKey, null)
  73. const selector: string = 'popover'
  74. const { proxy } = getCurrentInstance() as any
  75. const popover = usePopover(props.visibleArrow)
  76. const showPopover = ref<boolean>(false) // 控制popover显隐
  77. watch(
  78. () => props.content,
  79. (newVal) => {
  80. const { mode } = props
  81. if (mode === 'normal' && typeof newVal !== 'string') {
  82. console.error('The value type must be a string type in normal mode')
  83. } else if (mode === 'menu' && !isArray(newVal)) {
  84. console.error('The value type must be a Array type in menu mode')
  85. }
  86. }
  87. )
  88. watch(
  89. () => props.placement,
  90. () => {
  91. popover.init(props.placement, props.visibleArrow, selector)
  92. }
  93. )
  94. watch(
  95. () => props.modelValue,
  96. (newValue) => {
  97. showPopover.value = newValue
  98. }
  99. )
  100. watch(
  101. () => showPopover.value,
  102. (newValue) => {
  103. if (newValue) {
  104. popover.control(props.placement, props.offset)
  105. if (queue && queue.closeOther) {
  106. queue.closeOther(proxy)
  107. } else {
  108. closeOther(proxy)
  109. }
  110. }
  111. popover.showStyle.value = newValue ? 'display: inline-block;' : 'display: none;'
  112. emit('change', { show: newValue })
  113. emit(`${newValue ? 'open' : 'close'}`)
  114. }
  115. )
  116. onMounted(() => {
  117. popover.init(props.placement, props.visibleArrow, selector)
  118. })
  119. onBeforeMount(() => {
  120. if (queue && queue.pushToQueue) {
  121. queue.pushToQueue(proxy)
  122. } else {
  123. pushToQueue(proxy)
  124. }
  125. popover.showStyle.value = showPopover.value ? 'opacity: 1;' : 'opacity: 0;'
  126. })
  127. onBeforeUnmount(() => {
  128. if (queue && queue.removeFromQueue) {
  129. queue.removeFromQueue(proxy)
  130. } else {
  131. removeFromQueue(proxy)
  132. }
  133. })
  134. function menuClick(index: number) {
  135. updateModelValue(false)
  136. emit('menuclick', {
  137. item: (props.content as Array<Record<string, any>>)[index],
  138. index
  139. })
  140. }
  141. function toggle() {
  142. if (props.disabled) return
  143. updateModelValue(!showPopover.value)
  144. }
  145. function open() {
  146. updateModelValue(true)
  147. }
  148. function close() {
  149. updateModelValue(false)
  150. }
  151. function updateModelValue(value: boolean) {
  152. showPopover.value = value
  153. emit('update:modelValue', value)
  154. }
  155. defineExpose<PopoverExpose>({
  156. open,
  157. close
  158. })
  159. </script>
  160. <style lang="scss" scoped>
  161. @import './index.scss';
  162. </style>