| :visible="isOpen" |
| 9 | :placement="placement" |
| 10 | trigger="click" |
| 11 | :width="popoverWidth" |
| 12 | popper-class="popover-menu-popper" |
| 13 | :persistent="false" |
| 14 | :hide-after="0" |
| 15 | :offset="8" |
| 16 | :show-arrow="false" |
| 17 | > |
| 18 | <template #reference> |
| 19 | <div |
| 20 | v-if="filterable" |
| 21 | class="popover-trigger filterable-trigger" |
| 22 | :class="{ 'show-clear': showClearIcon }" |
| 23 | @click.stop |
| 24 | @mouseenter="isHovering = true" |
| 25 | @mouseleave="isHovering = false" |
| 26 | > |
| 27 | <el-input |
| 28 | ref="filterInputRef" |
| 29 | :model-value="inputValue" |
| 30 | :placeholder="inputPlaceholder" |
| 31 | :disabled="disabled" |
| 32 | :readonly="!isOpen" |
| 33 | @click="handleInputClick" |
| 34 | @update:model-value="handleFilterInput" |
| 35 | > |
| 36 | <template #suffix> |
| 37 | <el-icon |
| 38 | v-if="showClearIcon" |
| 39 | class="clear-icon" |
| 40 | @click.stop="handleClear" |
| 41 | > |
| 42 | <CircleClose /> |
| 43 | </el-icon> |
| 44 | <el-icon v-else class="arrow-icon"><ArrowDown /></el-icon> |
| 45 | </template> |
| 46 | </el-input> |
| 47 | </div> |
| 48 | <div v-else class="popover-trigger" @click.stop="toggle"> |
| 49 | <slot name="trigger" /> |
| 50 | </div> |
| 51 | </template> |
| 52 | <div class="popover-menu-content"> |
| 53 | <slot :close="close" :filter-text="filterText" /> |
| 54 | </div> |
| 55 | </el-popover> |
| 56 | </div> |
| 57 | </template> |
| 58 |
| 59 | <script lang="ts"> |
| 60 | // Module-level singleton for coordinating popover menus |
| 61 | const popoverEventTarget = new EventTarget() |
| 62 | const CLOSE_ALL_EVENT = 'close-all-popovers' |
| 63 | </script> |
| 64 |
| 65 | <script setup lang="ts"> |
| 66 | import { |
| 67 | ref, |
| 68 | computed, |
| 69 | provide, |
| 70 | inject, |
| 71 | watch, |
| 72 | onMounted, |
| 73 | onUnmounted, |
| 74 | } from 'vue' |
| 75 | import { formItemContextKey, ElInput } from 'element-plus' |
| 76 | import { ArrowDown, CircleClose } from '@element-plus/icons-vue' |
| 77 |
| 78 | interface Props { |
| 79 | width?: number |
| 80 | placement?: |
| 81 | | 'top' |
| 82 | | 'top-start' |
| 83 | | 'top-end' |
| 84 | | 'bottom' |
| 85 | | 'bottom-start' |
| 86 | | 'bottom-end' |
| 87 | modelValue?: string | number | null |
| 88 | selectable?: boolean |
| 89 | disabled?: boolean |
| 90 | fullWidth?: boolean |
| 91 | filterable?: boolean |
| 92 | filterPlaceholder?: string |
| 93 | displayValue?: string |
| 94 | clearable?: boolean |
| 95 | } |
| 96 |
| 97 | const props = withDefaults(defineProps<Props>(), { |
| 98 | width: 160, |
| 99 | placement: 'bottom-end', |
| 100 | modelValue: null, |
| 101 | selectable: false, |
| 102 | disabled: false, |
| 103 | fullWidth: false, |
| 104 | filterable: false, |
| 105 | filterPlaceholder: 'Search...', |
| 106 | displayValue: '', |
| 107 | clearable: false, |
| 108 | }) |
| 109 |
| 110 | const emit = defineEmits<{ |
| 111 | (e: 'update:modelValue', value: string | number | null): void |
| 112 | (e: 'filter-change', text: string): void |
| 113 | }>() |
| 114 |
| 115 | const elFormItem = inject(formItemContextKey, undefined) |
| 116 |
| 117 | const isOpen = ref(false) |
| 118 | const wrapperRef = ref<HTMLElement | null>(null) |
| 119 | const instanceId = Symbol() |
| 120 | const filterText = ref('') |
| 121 | const filterInputRef = ref<InstanceType<typeof ElInput> | null>(null) |
| 122 | const isHovering = ref(false) |
| 123 | const triggerWidth = ref(0) |
| 124 |
| 125 | const popoverWidth = computed(() => { |
| 126 | if (props.filterable && triggerWidth.value > 0) { |
| 127 | return Math.max(triggerWidth.value, props.width) |
| 128 | } |
| 129 | return props.width |
| 130 | }) |
| 131 |
| 132 | const updateTriggerWidth = () => { |
| 133 | if (wrapperRef.value) { |
| 134 | triggerWidth.value = wrapperRef.value.offsetWidth |
| 135 | } |
| 136 | } |
| 137 |
| 138 | const inputValue = computed(() => { |
| 139 | if (isOpen.value) return filterText.value |
| 140 | if (props.modelValue) return props.displayValue || '' |
| 141 | return '' |
| 142 | }) |
| 143 |
| 144 | const inputPlaceholder = computed(() => { |
| 145 | if (isOpen.value) return props.filterPlaceholder |
| 146 | if (!props.modelValue) return props.displayValue || props.filterPlaceholder |
| 147 | return props.filterPlaceholder |
| 148 | }) |
| 149 |
| 150 | const showClearIcon = computed(() => { |
| 151 | return ( |
| 152 | props.clearable && props.modelValue && isHovering.value && !props.disabled |
| 153 | ) |
| 154 | }) |
| 155 |
| 156 | watch(isOpen, (open) => { |
| 157 | if (!open && props.filterable) { |
| 158 | filterText.value = '' |
| 159 | emit('filter-change', '') |
| 160 | } |
| 161 | }) |
| 162 |
| 163 | const handleInputClick = () => { |
| 164 | if (props.disabled) return |
| 165 | if (!isOpen.value) { |
| 166 | updateTriggerWidth() |
| 167 | popoverEventTarget.dispatchEvent( |
| 168 | new CustomEvent(CLOSE_ALL_EVENT, { detail: instanceId }), |
| 169 | ) |
| 170 | isOpen.value = true |
| 171 | } |
| 172 | } |
| 173 |
| 174 | const handleFilterInput = (value: string) => { |
| 175 | filterText.value = value |
| 176 | emit('filter-change', value) |
| 177 | } |
| 178 |
| 179 | const handleClear = () => { |
| 180 | emit('update:modelValue', '') |
| 181 | filterText.value = '' |
| 182 | emit('filter-change', '') |
| 183 | elFormItem?.validate?.('change') |
| 184 | } |
| 185 |
| 186 | const toggle = () => { |
| 187 | if (props.disabled) return |
| 188 | if (!isOpen.value) { |
| 189 | popoverEventTarget.dispatchEvent( |
| 190 | new CustomEvent(CLOSE_ALL_EVENT, { detail: instanceId }), |
| 191 | ) |
| 192 | } |
| 193 | isOpen.value = !isOpen.value |
| 194 | } |
| 195 |
| 196 | const handleCloseAll = (e: Event) => { |
| 197 | const customEvent = e as CustomEvent |
| 198 | if (customEvent.detail !== instanceId) { |
| 199 | isOpen.value = false |
| 200 | } |
| 201 | } |
| 202 |
| 203 | const close = () => { |
| 204 | isOpen.value = false |
| 205 | } |
| 206 |
| 207 | const select = (value: string | number) => { |
| 208 | emit('update:modelValue', value) |
| 209 | if (props.filterable) { |
| 210 | filterText.value = '' |
| 211 | emit('filter-change', '') |
| 212 | filterInputRef.value?.blur() |
| 213 | } |
| 214 | close() |
| 215 | elFormItem?.validate?.('change') |
| 216 | } |
| 217 |
| 218 | const handleClickOutside = (e: MouseEvent) => { |
| 219 | const target = e.target as HTMLElement |
| 220 | if (wrapperRef.value && !wrapperRef.value.contains(target)) { |
| 221 | close() |
| 222 | } |
| 223 | } |
| 224 |
| 225 | onMounted(() => { |
| 226 | document.addEventListener('click', handleClickOutside) |
| 227 | popoverEventTarget.addEventListener(CLOSE_ALL_EVENT, handleCloseAll) |
| 228 | }) |
| 229 |
| 230 | onUnmounted(() => { |
| 231 | document.removeEventListener('click', handleClickOutside) |
| 232 | popoverEventTarget.removeEventListener(CLOSE_ALL_EVENT, handleCloseAll) |
| 233 | }) |
| 234 |
| 235 | provide('popoverMenu', { |
| 236 | close, |
| 237 | select, |
| 238 | selectable: props.selectable, |
| 239 | modelValue: () => props.modelValue, |
| 240 | }) |
| 241 | </script> |
| 242 |
| 243 | <style scoped lang="scss"> |
| 244 | .popover-menu-wrapper { |
| 245 | display: inline-block; |
| 246 |
| 247 | &.is-full-width { |
| 248 | display: block; |
| 249 | width: 100%; |
| 250 |
| 251 | .popover-trigger { |
| 252 | display: block; |
| 253 | width: 100%; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 |
| 258 | .popover-trigger { |
| 259 | display: inline-flex; |
| 260 |
| 261 | &.filterable-trigger { |
| 262 | display: block; |
| 263 | width: 100%; |
| 264 |
| 265 | :deep(.el-input__wrapper) { |
| 266 | cursor: pointer; |
| 267 | } |
| 268 |
| 269 | :deep(.el-input__suffix) { |
| 270 | cursor: pointer; |
| 271 | } |
| 272 |
| 273 | .arrow-icon { |
| 274 | color: var(--el-text-color-placeholder); |
| 275 | transition: transform 0.2s; |
| 276 | } |
| 277 |
| 278 | .clear-icon { |
| 279 | color: var(--el-text-color-placeholder); |
| 280 | transition: color 0.2s; |
| 281 |
| 282 | &:hover { |
| 283 | color: var(--el-text-color-regular); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 |
| 289 | .popover-menu-content { |
| 290 | padding: 4px; |
| 291 | } |
| 292 | </style> |
| 293 |
| 294 | <style lang="scss"> |
| 295 | .popover-menu-popper { |
| 296 | padding: 0 !important; |
| 297 | border-radius: 12px !important; |
| 298 | border: 1px solid $color-border-light !important; |
| 299 | box-shadow: |
| 300 | 0 10px 25px -5px rgba(0, 0, 0, 0.1), |
| 301 | 0 8px 10px -6px rgba(0, 0, 0, 0.1) !important; |
| 302 | } |
| 303 | </style> |