This commit is contained in:
薇薇安 2026-01-17 19:48:27 +08:00
parent 695bdfd319
commit 35b3777f3e

View File

@ -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()