fix: prevent numeric input reset to 0 on text selection blur#7581
fix: prevent numeric input reset to 0 on text selection blur#7581uuutt2023 wants to merge 7 commits intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
@blurhandler conflatesnullandundefinedvia!= null; ifnumericTempcan ever beundefinedit might be clearer and safer to explicitly decide whether that should usemodelValueor be treated as an edited-but-cleared state (and use strict=== null/=== undefinedaccordingly). - Consider whether an empty-string input (user clears the field) should emit
0viatoNumber('')or be treated specially (e.g., emitnullor revert tomodelValue), and encode that case explicitly so the blur logic is unambiguous.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `@blur` handler conflates `null` and `undefined` via `!= null`; if `numericTemp` can ever be `undefined` it might be clearer and safer to explicitly decide whether that should use `modelValue` or be treated as an edited-but-cleared state (and use strict `=== null`/`=== undefined` accordingly).
- Consider whether an empty-string input (user clears the field) should emit `0` via `toNumber('')` or be treated specially (e.g., emit `null` or revert to `modelValue`), and encode that case explicitly so the blur logic is unambiguous.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request modifies the @blur event handler in ConfigItemRenderer.vue to prevent resetting numeric values to zero when the input is null. The reviewer suggested a further refinement to avoid redundant updates by only emitting the change if the user has actually interacted with the input field.
| :model-value="numericTemp ?? modelValue" | ||
| @update:model-value="val => (numericTemp = val)" | ||
| @blur="() => { emitUpdate(toNumber(numericTemp)); numericTemp = null }" | ||
| @blur="() => { emitUpdate(numericTemp != null ? toNumber(numericTemp) : modelValue); numericTemp = null }" |
There was a problem hiding this comment.
While this fix correctly prevents the value from being reset to 0 when numericTemp is null, emitting the modelValue back to the parent is redundant when no changes have been made (e.g., when the user just selects text and blurs). It's cleaner to only emit an update and reset the temporary state if the user has actually interacted with the input (i.e., numericTemp is not null). This avoids unnecessary event cycles and potential side-effects in the parent component.
@blur="() => { if (numericTemp != null) { emitUpdate(toNumber(numericTemp)); numericTemp = null } }"
Only emit update when numericTemp is not null to avoid unnecessary event cycles and potential side-effects.
- Add toValidNumber() function with comprehensive boundary handling - Add handleNumericBlur() to prevent unintended resets on text selection - Handle null, undefined, empty string, and NaN cases properly - Only emit updates when user actually modifies the value
…ation - Replace parseFloat with Number() for more consistent type coercion - Use Number.isFinite() pattern consistent with restartAstrBot.ts - Better handling of edge cases (Infinity, -Infinity)
|
#7560 already fixed |
fix: prevent numeric input reset to 0 on text selection blur
Modifications / 改动点
dashboard/src/components/shared/ConfigItemRenderer.vue: Line 164Screenshots or Test Results / 运行截图或测试结果
Bug 复现步骤:
修复后验证:
根因分析:
@blur="() => { emitUpdate(toNumber(numericTemp)); numericTemp = null }"toNumber()函数使用parseFloat(val),当 val 为 null 时返回 NaN,然后 isNaN(NaN) 为 true,所以返回 0numericTemp != null时才调用toNumber(),否则保留modelValueChecklist / 检查清单
This is NOT a breaking change. / 这不是一个破坏性变更。
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: