From 35b3777f3e8b10d996fe7193f1976c7ab5c16171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Sat, 17 Jan 2026 19:48:27 +0800 Subject: [PATCH] a --- frontend/src/components/ConfigPanel.jsx | 44 ++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/ConfigPanel.jsx b/frontend/src/components/ConfigPanel.jsx index 75213a7..bb69ead 100644 --- a/frontend/src/components/ConfigPanel.jsx +++ b/frontend/src/components/ConfigPanel.jsx @@ -476,7 +476,27 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => { return } - const numValue = typeof localValue === 'string' ? parseFloat(localValue) : localValue + // 处理字符串形式的数字输入(包括部分输入如 "0." 或 ".") + let numValue + if (typeof localValue === 'string') { + // 如果字符串以 "." 结尾(如 "0." 或 "."),尝试补全为有效数字 + if (localValue.endsWith('.') && localValue.length > 1) { + // "0." -> 0, "1." -> 1 + numValue = parseFloat(localValue) + } else if (localValue === '.' || localValue === '-.') { + // "." 或 "-." 视为无效,恢复原值 + const restoreValue = label.includes('PERCENT') + ? (typeof value === 'number' && value < 1 ? Math.round(value * 100) : value) + : value + setLocalValue(restoreValue) + return + } else { + numValue = parseFloat(localValue) + } + } else { + numValue = localValue + } + if (isNaN(numValue)) { // 如果输入无效,恢复原值 const restoreValue = label.includes('PERCENT') @@ -688,13 +708,16 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => { // 如果无效,不更新(保持原值) } } else if (config.type === 'number') { - // 其他数字配置:保持数字形式,允许空值 + // 其他数字配置:允许字符串形式的中间状态(如 "0." 或 "."),支持小数输入 if (newValue === '') { handleChange('') } else { - const numValue = parseFloat(newValue) - if (!isNaN(numValue)) { - handleChange(numValue) + // 允许部分输入状态(如 "0.", ".5", "-" 等) + // 使用正则验证是否为有效的数字格式(包括部分输入) + const validPattern = /^-?\d*\.?\d*$/ + if (validPattern.test(newValue)) { + // 如果是有效的数字格式(包括部分输入),直接保存字符串 + handleChange(newValue) } // 如果无效,不更新(保持原值) } @@ -711,6 +734,17 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => { } // 对于数字输入,只允许数字、小数点和负号 if (config.type === 'number') { + const currentValue = String(localValue || '') + // 如果已经包含小数点,不允许再次输入小数点 + if (e.key === '.' && currentValue.includes('.')) { + e.preventDefault() + return + } + // 如果已经包含负号,不允许再次输入负号(除非是删除操作) + if (e.key === '-' && currentValue.includes('-')) { + e.preventDefault() + return + } const validKeys = /^[0-9.-]$/ if (!validKeys.test(e.key) && !['Enter', 'Escape'].includes(e.key)) { e.preventDefault()