refactor(frontend): 优化通用组件

- 改进ConfirmDialog对话框组件
- 增强DataTable表格组件功能和响应式布局
- 优化EmptyState空状态组件
- 完善SubscriptionProgressMini订阅进度组件
This commit is contained in:
IanShaw027 2025-12-27 16:04:16 +08:00
parent 227d506c53
commit c615a4264d
4 changed files with 58 additions and 10 deletions

View File

@ -31,8 +31,12 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import Modal from './Modal.vue' import Modal from './Modal.vue'
const { t } = useI18n()
interface Props { interface Props {
show: boolean show: boolean
title: string title: string
@ -47,12 +51,13 @@ interface Emits {
(e: 'cancel'): void (e: 'cancel'): void
} }
withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
confirmText: 'Confirm',
cancelText: 'Cancel',
danger: false danger: false
}) })
const confirmText = computed(() => props.confirmText || t('common.confirm'))
const cancelText = computed(() => props.cancelText || t('common.cancel'))
const emit = defineEmits<Emits>() const emit = defineEmits<Emits>()
const handleConfirm = () => { const handleConfirm = () => {

View File

@ -152,6 +152,7 @@ const { t } = useI18n()
// //
const tableWrapperRef = ref<HTMLElement | null>(null) const tableWrapperRef = ref<HTMLElement | null>(null)
const isScrollable = ref(false) const isScrollable = ref(false)
const actionsColumnNeedsExpanding = ref(false)
// //
const checkScrollable = () => { const checkScrollable = () => {
@ -160,17 +161,49 @@ const checkScrollable = () => {
} }
} }
//
const checkActionsColumnWidth = () => {
if (!tableWrapperRef.value) return
//
const actionsHeader = tableWrapperRef.value.querySelector('th:has(button[title*="Expand"], button[title*="展开"])')
if (!actionsHeader) return
//
const firstActionCell = tableWrapperRef.value.querySelector('tbody tr:first-child td:last-child')
if (!firstActionCell) return
//
const actionsContent = firstActionCell.querySelector('div')
if (!actionsContent) return
//
const contentWidth = actionsContent.scrollWidth
const cellWidth = (firstActionCell as HTMLElement).clientWidth
//
actionsColumnNeedsExpanding.value = contentWidth > cellWidth
}
// //
let resizeObserver: ResizeObserver | null = null let resizeObserver: ResizeObserver | null = null
onMounted(() => { onMounted(() => {
checkScrollable() checkScrollable()
checkActionsColumnWidth()
if (tableWrapperRef.value && typeof ResizeObserver !== 'undefined') { if (tableWrapperRef.value && typeof ResizeObserver !== 'undefined') {
resizeObserver = new ResizeObserver(checkScrollable) resizeObserver = new ResizeObserver(() => {
checkScrollable()
checkActionsColumnWidth()
})
resizeObserver.observe(tableWrapperRef.value) resizeObserver.observe(tableWrapperRef.value)
} else { } else {
// ResizeObserver 使 window resize // ResizeObserver 使 window resize
window.addEventListener('resize', checkScrollable) const handleResize = () => {
checkScrollable()
checkActionsColumnWidth()
}
window.addEventListener('resize', handleResize)
} }
}) })
@ -205,6 +238,7 @@ watch(
async () => { async () => {
await nextTick() await nextTick()
checkScrollable() checkScrollable()
checkActionsColumnWidth()
}, },
{ flush: 'post' } { flush: 'post' }
) )
@ -234,7 +268,11 @@ const sortedData = computed(() => {
// //
const hasExpandableActions = computed(() => { const hasExpandableActions = computed(() => {
return props.expandableActions && props.columns.some((col) => col.key === 'actions') return (
props.expandableActions &&
props.columns.some((col) => col.key === 'actions') &&
actionsColumnNeedsExpanding.value
)
}) })
// / // /

View File

@ -25,7 +25,7 @@
<!-- Title --> <!-- Title -->
<h3 class="empty-state-title"> <h3 class="empty-state-title">
{{ title }} {{ displayTitle }}
</h3> </h3>
<!-- Description --> <!-- Description -->
@ -61,8 +61,12 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import type { Component } from 'vue' import type { Component } from 'vue'
const { t } = useI18n()
interface Props { interface Props {
icon?: Component | string icon?: Component | string
title?: string title?: string
@ -73,11 +77,12 @@ interface Props {
message?: string message?: string
} }
withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
title: 'No data found',
description: '', description: '',
actionIcon: true actionIcon: true
}) })
const displayTitle = computed(() => props.title || t('common.noData'))
defineEmits(['action']) defineEmits(['action'])
</script> </script>

View File

@ -246,7 +246,7 @@ function formatDaysRemaining(expiresAt: string): string {
const diff = expires.getTime() - now.getTime() const diff = expires.getTime() - now.getTime()
if (diff < 0) return t('subscriptionProgress.expired') if (diff < 0) return t('subscriptionProgress.expired')
const days = Math.ceil(diff / (1000 * 60 * 60 * 24)) const days = Math.ceil(diff / (1000 * 60 * 60 * 24))
if (days === 0) return t('subscriptionProgress.expirestoday') if (days === 0) return t('subscriptionProgress.expiresToday')
if (days === 1) return t('subscriptionProgress.expiresTomorrow') if (days === 1) return t('subscriptionProgress.expiresTomorrow')
return t('subscriptionProgress.daysRemaining', { days }) return t('subscriptionProgress.daysRemaining', { days })
} }