WEB/src/views/order/orderManagement/component/OrderToolbarButtonGroup.vue
2026-06-22 18:15:09 +08:00

253 lines
7.3 KiB
Vue

<template>
<div ref="containerRef" class="order-toolbar-button-group">
<div ref="mainRef" class="order-toolbar-button-group__main">
<template v-for="button in visibleButtons" :key="button.key">
<el-dropdown
v-if="button.buttonType === 'dropdown' && button.dropdownOptions?.length"
trigger="click"
split-button
:size="button.size || 'small'"
:type="button.type"
:plain="button.plain"
:disabled="resolveDisabled(button.disabledKey) || resolveLoading(button.loadingKey)"
@click="emitAction(button.action)"
@command="emitAction"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="option in button.dropdownOptions"
:key="option.action"
:command="option.action"
:divided="option.divided"
>
{{ option.label }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<el-button
v-else
:size="button.size || 'small'"
:type="button.type"
:plain="button.plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
@click="emitAction(button.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
</el-button>
</template>
<el-popover
v-if="overflowButtons.length"
v-model:visible="expandVisible"
placement="bottom-start"
:width="172"
trigger="click"
popper-class="order-toolbar-button-group__popover"
>
<template #reference>
<el-button size="small" :type="expandVisible ? 'primary' : 'default'" plain>
展开{{ overflowButtons.length ? `(${overflowButtons.length})` : '' }}
</el-button>
</template>
<div class="order-toolbar-button-group__overflow-list">
<template v-for="button in overflowButtons" :key="`overflow-${button.key}`">
<template v-if="button.buttonType === 'dropdown' && button.dropdownOptions?.length">
<el-button
v-for="option in button.dropdownOptions"
:key="`overflow-${button.key}-${option.action}`"
:size="button.size || 'small'"
:type="button.type"
:plain="button.plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
class="order-toolbar-button-group__overflow-item"
@click="handleOverflowClick(option.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }} · {{ option.label }}
</el-button>
</template>
<el-button
v-else
:size="button.size || 'small'"
:type="button.type"
:plain="button.plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
class="order-toolbar-button-group__overflow-item"
@click="handleOverflowClick(button.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
</el-button>
</template>
</div>
</el-popover>
</div>
</div>
</template>
<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { ToolbarButtonConfig } from '../config/order-common-config'
const props = defineProps<{
buttons: ToolbarButtonConfig[]
resolveDisabled: (disabledKey?: string) => boolean
resolveLoading: (loadingKey?: string) => boolean
resolveLabel: (defaultLabel: string, dynamicLabelKey?: string) => string
layoutKey?: string | number
}>()
const emit = defineEmits<{
(e: 'action', action: ToolbarButtonConfig['action']): void
}>()
const containerRef = ref<HTMLElement | null>(null)
const mainRef = ref<HTMLElement | null>(null)
const visibleButtons = ref<ToolbarButtonConfig[]>([])
const overflowButtons = ref<ToolbarButtonConfig[]>([])
const expandVisible = ref(false)
let resizeObserver: ResizeObserver | null = null
let layoutFrame = 0
function emitAction(action: ToolbarButtonConfig['action']) {
emit('action', action)
}
function handleOverflowClick(action: ToolbarButtonConfig['action']) {
expandVisible.value = false
emitAction(action)
}
function isMainOverflowing() {
const mainEl = mainRef.value
if (!mainEl) return false
return mainEl.scrollWidth > mainEl.clientWidth + 1
}
async function trimVisibleUntilFits() {
const buttons = props.buttons
if (!buttons.length) {
visibleButtons.value = []
overflowButtons.value = []
return
}
visibleButtons.value = [...buttons]
overflowButtons.value = []
await nextTick()
let guard = 0
while (isMainOverflowing() && visibleButtons.value.length > 0 && guard < buttons.length + 1) {
guard += 1
const removed = visibleButtons.value.pop()
if (!removed) break
overflowButtons.value.unshift(removed)
await nextTick()
}
if (!visibleButtons.value.length && overflowButtons.value.length) {
const first = overflowButtons.value[0]
if (!first) return
visibleButtons.value = [first]
overflowButtons.value = overflowButtons.value.slice(1)
await nextTick()
while (isMainOverflowing() && visibleButtons.value.length > 0 && overflowButtons.value.length > 0 && guard < buttons.length + 2) {
guard += 1
const removed = visibleButtons.value.pop()
if (!removed) break
overflowButtons.value.unshift(removed)
await nextTick()
}
}
}
async function scheduleLayout() {
const frame = ++layoutFrame
await nextTick()
if (frame !== layoutFrame) return
await trimVisibleUntilFits()
}
function setupResizeObserver() {
resizeObserver?.disconnect()
resizeObserver = new ResizeObserver(() => {
void scheduleLayout()
})
const parent = containerRef.value?.parentElement
if (parent) resizeObserver.observe(parent)
if (containerRef.value) resizeObserver.observe(containerRef.value)
if (mainRef.value) resizeObserver.observe(mainRef.value)
}
onMounted(() => {
visibleButtons.value = [...props.buttons]
setupResizeObserver()
void scheduleLayout()
})
onBeforeUnmount(() => {
resizeObserver?.disconnect()
resizeObserver = null
})
watch(
() => [props.buttons, props.layoutKey],
() => {
void scheduleLayout()
},
{ deep: true }
)
</script>
<style scoped lang="scss">
.order-toolbar-button-group {
position: relative;
flex: 1;
width: 100%;
max-width: 100%;
min-width: 0;
overflow: hidden;
}
.order-toolbar-button-group__main {
display: flex;
align-items: center;
gap: 6px;
width: 100%;
max-width: 100%;
min-width: 0;
overflow: hidden;
flex-wrap: nowrap;
}
.order-toolbar-button-group__main :deep(.el-dropdown) {
flex-shrink: 0;
}
.order-toolbar-button-group__overflow-list {
display: flex;
flex-direction: column;
gap: 8px;
}
.order-toolbar-button-group__overflow-item {
width: 100%;
margin: 0;
justify-content: flex-start;
}
</style>
<style lang="scss">
.order-toolbar-button-group__popover {
padding: 10px !important;
}
</style>