Skip to content

fix: prevent numeric input reset to 0 on text selection blur#7581

Closed
uuutt2023 wants to merge 7 commits intoAstrBotDevs:masterfrom
uuutt2023:fix/issue-7423-numeric-input-reset
Closed

fix: prevent numeric input reset to 0 on text selection blur#7581
uuutt2023 wants to merge 7 commits intoAstrBotDevs:masterfrom
uuutt2023:fix/issue-7423-numeric-input-reset

Conversation

@uuutt2023
Copy link
Copy Markdown

@uuutt2023 uuutt2023 commented Apr 15, 2026

fix: prevent numeric input reset to 0 on text selection blur

Modifications / 改动点

  • dashboard/src/components/shared/ConfigItemRenderer.vue: Line 164
    • 修改了数字输入框的 @blur 事件处理逻辑
    • 当用户只是选中文本而没有实际修改值时,保留原始的 modelValue 而非被重置为 0

Screenshots or Test Results / 运行截图或测试结果

Bug 复现步骤

  1. 打开 AstrBot WebUI
  2. 选中 "AstrBot 插件" 选项卡
  3. 点击 ⚙ 按钮打开任意插件的配置页面
  4. 在数字输入框中鼠标左键拖拽选中文本
  5. 只点击其他地方取消选中(不做任何修改操作)
  6. Bug 结果:数值已被重置为 0
PixPin_2026-04-15_21-35-54

修复后验证

  1. 按照上述步骤操作
  2. 预期结果:数值保持不变(不会被重置为 0)
PixPin_2026-04-15_21-36-44

根因分析

  • 原代码:@blur="() => { emitUpdate(toNumber(numericTemp)); numericTemp = null }"
  • toNumber() 函数使用 parseFloat(val),当 val 为 null 时返回 NaN,然后 isNaN(NaN) 为 true,所以返回 0
  • 修复后:当 numericTemp != null 时才调用 toNumber(),否则保留 modelValue

Checklist / 检查清单

  • 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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Prevent numeric input fields from resetting to 0 when focus is lost after only selecting text without modifying the value.

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:webui The bug / feature is about webui(dashboard) of astrbot. labels Apr 15, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Apr 15, 2026
…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)
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 16, 2026
@Soulter
Copy link
Copy Markdown
Member

Soulter commented Apr 16, 2026

#7560 already fixed

@Soulter Soulter closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:webui The bug / feature is about webui(dashboard) of astrbot. lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants