a
This commit is contained in:
parent
695bdfd319
commit
35b3777f3e
|
|
@ -476,7 +476,27 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
|
||||||
return
|
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)) {
|
if (isNaN(numValue)) {
|
||||||
// 如果输入无效,恢复原值
|
// 如果输入无效,恢复原值
|
||||||
const restoreValue = label.includes('PERCENT')
|
const restoreValue = label.includes('PERCENT')
|
||||||
|
|
@ -688,13 +708,16 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
|
||||||
// 如果无效,不更新(保持原值)
|
// 如果无效,不更新(保持原值)
|
||||||
}
|
}
|
||||||
} else if (config.type === 'number') {
|
} else if (config.type === 'number') {
|
||||||
// 其他数字配置:保持数字形式,允许空值
|
// 其他数字配置:允许字符串形式的中间状态(如 "0." 或 "."),支持小数输入
|
||||||
if (newValue === '') {
|
if (newValue === '') {
|
||||||
handleChange('')
|
handleChange('')
|
||||||
} else {
|
} else {
|
||||||
const numValue = parseFloat(newValue)
|
// 允许部分输入状态(如 "0.", ".5", "-" 等)
|
||||||
if (!isNaN(numValue)) {
|
// 使用正则验证是否为有效的数字格式(包括部分输入)
|
||||||
handleChange(numValue)
|
const validPattern = /^-?\d*\.?\d*$/
|
||||||
|
if (validPattern.test(newValue)) {
|
||||||
|
// 如果是有效的数字格式(包括部分输入),直接保存字符串
|
||||||
|
handleChange(newValue)
|
||||||
}
|
}
|
||||||
// 如果无效,不更新(保持原值)
|
// 如果无效,不更新(保持原值)
|
||||||
}
|
}
|
||||||
|
|
@ -711,6 +734,17 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
|
||||||
}
|
}
|
||||||
// 对于数字输入,只允许数字、小数点和负号
|
// 对于数字输入,只允许数字、小数点和负号
|
||||||
if (config.type === 'number') {
|
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.-]$/
|
const validKeys = /^[0-9.-]$/
|
||||||
if (!validKeys.test(e.key) && !['Enter', 'Escape'].includes(e.key)) {
|
if (!validKeys.test(e.key) && !['Enter', 'Escape'].includes(e.key)) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user