/** * 验证步进器配置 */ export function validateStepperConfig(config: any) { const errors: string[] = [] if (config.min !== undefined && config.max !== undefined && config.min > config.max) { errors.push('最小值不能大于最大值') } if (config.step !== undefined && config.step <= 0) { errors.push('步进值必须大于0') } if (config.buttonSize !== undefined && config.buttonSize < 20) { errors.push('按钮大小不能小于20px') } if (config.inputWidth !== undefined && config.inputWidth < 20) { errors.push('输入框宽度不能小于20px') } return errors } /** * 计算安全值范围 */ export function getSafeValue(value: number, min: number, max: number) { let safeValue = value if (value < min) { safeValue = min } else if (value > max) { safeValue = max } return safeValue } /** * 格式化显示值 */ export function formatDisplayValue( value: number, options: { integer?: boolean; step?: number; min?: number; max?: number; } ): string { let formattedValue = value // 整数模式 if (options.integer) { formattedValue = Math.round(formattedValue) } // 处理小数精度 if (options.step) { const decimal = options.step.toString().split('.')[1]?.length || 0 if (decimal > 0) { formattedValue = Number(formattedValue.toFixed(decimal)) } } // 范围限制 if (options.min !== undefined && formattedValue < options.min) { formattedValue = options.min } if (options.max !== undefined && formattedValue > options.max) { formattedValue = options.max } return formattedValue.toString() } /** * 解析输入值 */ export function parseInputValue( input: string, options: { integer?: boolean; defaultValue?: number; min?: number; max?: number; } ): number { // 空值处理 if (input.trim() === '') { return options.defaultValue || 0 } let value = parseFloat(input) // 无效值处理 if (isNaN(value)) { return options.defaultValue || 0 } // 整数模式 if (options.integer) { value = Math.round(value) } // 范围限制 if (options.min !== undefined && value < options.min) { value = options.min } if (options.max !== undefined && value > options.max) { value = options.max } return value } /** * 生成步进器样式 */ export function generateStepperStyles(options: { theme?: string; shape?: string; buttonSize?: number | string; inputWidth?: number | string; buttonColor?: string; inputBgColor?: string; disabledColor?: string; }): Record { const styles: Record = {} // 按钮样式 if (options.buttonSize) { const size = typeof options.buttonSize === 'number' ? `${options.buttonSize}px` : options.buttonSize styles.button = { width: size, height: size } } // 输入框样式 if (options.inputWidth) { const width = typeof options.inputWidth === 'number' ? `${options.inputWidth}px` : options.inputWidth styles.input = { width: width } } // 主题相关样式 if (options.theme === 'round') { styles.container = { borderRadius: '20px', overflow: 'hidden' } } return styles }