Skip to content

fix: track keyboard press synchronously for native range keys - #308

Merged
yoyo837 merged 1 commit into
react-component:masterfrom
mokshupadhyay:fix/keyboard-keypressing-ref
Dec 2, 2026
Merged

yoyo837 merged 1 commit into
react-component:masterfrom
mokshupadhyay:fix/keyboard-keypressing-ref

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

The colour picker's handles are native range inputs. While a value key is held (arrow keys, Page Up/Down, Home/End), Handler is meant to keep the interaction open and call onChangeComplete only on keyup. It tracked that with the keyPressing state, set in onKeyDown. A native range input fires its input event in the same keydown turn, before React has committed that state update, so the change handler still saw keyPressing === false, treated the change as a finished interaction and called onChangeComplete while the key was still down (and again on keyup).

Handler now also keeps the same flag in a useRef (keyPressingRef), set and cleared next to every setKeyPressing call (keydown, keyup, blur), and the change handler reads the ref. The state is still there for rendering; only the check in the change handler is synchronous.

Test: Should defer onChangeComplete until keyup for native range keys in tests/index.test.tsx dispatches a keydown (PageDown), sets the range value and dispatches input inside one act(), the order a native range input uses, and expects onChange once and onChangeComplete not at all until keyup. On master it fails (expected "vi.fn()" to not be called at all, but actually been called 1 times) and it passes now. npm test passes (3 files, 72 tests), and tsc --noEmit and Prettier are clean on the changed files. The test simulates the browser's event order; I did not try this in a real browser.

Summary by CodeRabbit

  • Bug 修复
    • 修复键盘操作滑块时过早触发完成回调的问题;现在会在松开按键后,使用最终值触发回调。

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Handler 新增同步按键状态记录,并用它判断输入变更是否发生在按键交互期间。新增测试验证原生 range 输入事件触发后,onChangeComplete 在 keyup 时调用一次。

Changes

按键输入处理

Layer / File(s) Summary
同步按键状态与输入回调
src/components/Handler/index.tsx, tests/index.test.tsx
Handler 在值键按下、释放和焦点离开时同步更新按键状态,并根据该状态处理输入变更。测试验证按键期间的回调时机,以及完成回调中的最终 hue 值为 150。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: pareder, yoyo837

Merge Risk: Minimal · up to e104a

This fixes onChangeComplete firing before a held key is released on native range inputs. The change is small and local, with a regression test covering the reported event order. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: track keyboard press synchronously for native range keys”与变更内容一致。变更在 Handler 中新增同步的 keyPressingRef,并在按键按下、释放和焦点离开时更新它。原生 range 输入在同一个 keydown 轮次中触发 input 事件,因此 onChangeComplete 不再提前调用。标题准确描述了主…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

标题“fix: track keyboard press synchronously for native range keys”与变更内容一致。变更在 Handler 中新增同步的 keyPressingRef,并在按键按下、释放和焦点离开时更新它。原生 range 输入在同一个 keydown 轮次中触发 input 事件,因此 onChangeComplete 不再提前调用。标题准确描述了主要修复点,并使用了清楚的前缀格式。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子轻敲键盘,色相悄悄变
按下时先记录,输入稳稳传
松开键帽以后,完成回调响
Hue 到一百五,兔耳也摇晃
测试确认时序,月光落在旁

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5dccb618-2523-4b80-9e49-6d48124a4bfe
Commits

Reviewing files that changed from the base of the PR and between 3477bc8 and e104a16.

Files selected for processing: 2

  • src/components/Handler/index.tsx
  • tests/index.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3477bc8) to head (e104a16).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #308   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines          364       368    +4     
  Branches        94        94           
=========================================
+ Hits           364       368    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yoyo837
yoyo837 merged commit cd0412a into react-component:master Dec 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants