import type { TabItem } from '../../../types/components/tabs' /** * 验证标签页配置 */ export function validateTabsConfig(config: any) { const errors: string[] = [] if (!Array.isArray(config.items)) { errors.push('items 必须是数组') } if (config.height && config.height < 30) { errors.push('高度不能小于30px') } if (config.duration && config.duration < 0) { errors.push('动画持续时间不能为负数') } return errors } /** * 计算标签宽度 */ export function calculateTabWidth( tabs: TabItem[], options: { containerWidth?: number minTabWidth?: number maxTabWidth?: number gutter?: number centered?: boolean } ): number { if (!tabs.length) return 0 const { containerWidth = 0, minTabWidth = 0, maxTabWidth = 0, gutter = 0, centered = false } = options // 如果有容器宽度,计算合适的宽度 if (containerWidth > 0) { const availableWidth = containerWidth - gutter * (tabs.length - 1) let calculatedWidth = availableWidth / tabs.length // 应用最小和最大宽度限制 if (minTabWidth > 0) { calculatedWidth = Math.max(calculatedWidth, minTabWidth) } if (maxTabWidth > 0) { calculatedWidth = Math.min(calculatedWidth, maxTabWidth) } return calculatedWidth } // 如果没有容器宽度,返回0(使用内容自适应) return 0 } /** * 获取激活的标签索引 */ export function getActiveIndex( items: TabItem[], modelValue?: number, activeKey?: string | number ): number { if (activeKey !== undefined) { const index = items.findIndex(item => (item.id !== undefined && item.id === activeKey) || item.title === activeKey ) if (index !== -1) return index } if (modelValue !== undefined) { return Math.max(0, Math.min(modelValue, items.length - 1)) } return 0 } /** * 更新标签徽标 */ export function updateTabBadge( items: TabItem[], index: number, badge: number | string, type: 'number' | 'text' = 'number' ): TabItem[] { return items.map((item, i) => { if (i === index) { return { ...item, badge: typeof badge === 'number' ? badge : undefined, badgeText: typeof badge === 'string' ? badge : undefined, badgeType: type } } return item }) } /** * 更新标签红点状态 */ export function updateTabRedDot( items: TabItem[], index: number, showRedDot: boolean ): TabItem[] { return items.map((item, i) => { if (i === index) { return { ...item, showRedDot } } return item }) } /** * 禁用/启用标签 */ export function toggleTabDisabled( items: TabItem[], index: number, disabled: boolean ): TabItem[] { return items.map((item, i) => { if (i === index) { return { ...item, disabled } } return item }) } /** * 生成标签样式 */ export function generateTabStyles(options: { type?: string position?: string height?: number background?: string border?: boolean }): Record { const styles: Record = {} // 基础样式 if (options.height) { styles.height = `${options.height}px` } if (options.background) { styles.backgroundColor = options.background } // 位置相关样式 if (options.position === 'left' || options.position === 'right') { styles.flexDirection = 'column' } // 边框样式 if (options.border) { if (options.position === 'top') { styles.borderBottom = '1px solid #ebedf0' } else if (options.position === 'bottom') { styles.borderTop = '1px solid #ebedf0' } } return styles }