Skip to content

Conversation

@zombieJ
Copy link
Member

@zombieJ zombieJ commented Nov 10, 2025

将函数从 rc-input 挪过来,rc-input-number 本身与 rc-input 解耦

Summary by CodeRabbit

新功能

  • 新增焦点管理API,支持以编程方式聚焦元素并设置光标位置(起始、末尾或全选)。

测试

  • 增加焦点行为的测试覆盖。

@vercel
Copy link

vercel bot commented Nov 10, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
util Ready Ready Preview Comment Nov 10, 2025 8:57am

@coderabbitai
Copy link

coderabbitai bot commented Nov 10, 2025

Walkthrough

src/Dom/focus.ts 中新增公共 API 函数 triggerFocus,支持对 HTML 输入元素进行编程式焦点管理和光标定位。该函数通过 InputFocusOptions 接口提供光标选项('start'、'end'、'all'),并相应设置选区范围。配套测试已添加。

Changes

内聚体 / 文件 变更摘要
焦点管理公共 API
src/Dom/focus.ts
新增 InputFocusOptions 接口(扩展 FocusOptions),包含 cursor: 'start' | 'end' | 'all' 选项。新增 triggerFocus(element, options) 函数,支持元素焦点设置及光标位置控制(start=0,0; end=end,end; all=0,length)。
焦点管理测试覆盖
tests/focus.test.ts
新增针对 triggerFocus 的测试用例,验证 textarea 元素的焦点设置及不同光标选项下的 setSelectionRange 调用行为。导出公共函数 triggerFocus

代码审查工作量估计

🎯 2 (简单) | ⏱️ ~10 分钟

  • 新增函数逻辑清晰,边界情况处理得当(元素缺失时的无操作行为)
  • 测试用例覆盖三种光标位置场景,验证完整性良好
  • 仅涉及两个文件,变更范围集中

诗句

🐰 焦点跟踪小魔法,
光标舞动显风采,
输入框内任你驾,
从头到尾随心裁!✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确描述了拉取请求的主要变更,即添加 triggerFocus 功能支持,与文件变更内容相符。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch triggerFocus

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @zombieJ, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求引入了一个新的 triggerFocus 工具函数,该函数此前内嵌于 rc-input 中。通过将此函数提取到共享的 Dom/focus.ts 模块中,它使得 rc-input-number 和其他组件能够使用相同的焦点和光标定位逻辑,而无需直接依赖 rc-input,从而提高了模块化和代码复用性。

Highlights

  • 新增 triggerFocus 工具函数: 在 src/Dom/focus.ts 中引入了一个新的 triggerFocus 函数。
  • 光标位置控制: 此工具函数允许聚焦元素,并可选地为输入/文本区域元素设置光标位置(开始、结束或全选)。
  • 解耦 rc-input-number: 该函数从 rc-input 移出,成为一个共享工具,以解耦 rc-input-numberrc-input 的依赖。
  • 单元测试: 为 triggerFocus 函数添加了全面的单元测试,涵盖了各种光标定位场景。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@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

这个 PR 将 triggerFocus 函数移入 rc-util 中,以便在 rc-input, rc-textarea, rc-input-number 中共享,从而实现解耦,这是一个很好的重构。代码实现基本正确,并且包含了相应的单元测试。我只提一个关于代码可读性和健壮性的小建议。

Comment on lines +126 to +137
switch (cursor) {
case 'start':
element.setSelectionRange(0, 0);
break;

case 'end':
element.setSelectionRange(len, len);
break;

default:
element.setSelectionRange(0, len);
}

Choose a reason for hiding this comment

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

medium

switch 语句使用 default 来处理 cursor'all' 的情况。虽然功能上是正确的,但这不够明确。如果未来 cursor 的类型增加了新的选项,它们会意外地执行 default 的逻辑(即全选文本),这可能不是期望的行为。建议明确地处理 'all' 情况,让代码更具可读性和健壮性。

    switch (cursor) {
      case 'start':
        element.setSelectionRange(0, 0);
        break;

      case 'end':
        element.setSelectionRange(len, len);
        break;

      case 'all':
        element.setSelectionRange(0, len);
        break;
    }

@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.47%. Comparing base (7558cb8) to head (23a8940).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #709      +/-   ##
==========================================
+ Coverage   90.28%   90.47%   +0.19%     
==========================================
  Files          37       37              
  Lines         885      903      +18     
  Branches      301      311      +10     
==========================================
+ Hits          799      817      +18     
  Misses         84       84              
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 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.

@zombieJ zombieJ merged commit e213fe2 into master Nov 10, 2025
11 of 12 checks passed
@zombieJ zombieJ deleted the triggerFocus branch November 10, 2025 08:59
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Dom/focus.ts (1)

115-115: 建议解构 option 参数以避免传递额外属性。

当前实现将整个 option 对象(包含自定义的 cursor 属性)传递给 element.focus()。虽然浏览器会忽略额外的属性,但更规范的做法是仅传递标准的 FocusOptions 属性。

应用此 diff 来改进实现:

-  element.focus(option);
+  const { cursor, ...focusOptions } = option || {};
+  element.focus(focusOptions);

   // Selection content
-  const { cursor } = option || {};
   if (
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7558cb8 and 23a8940.

📒 Files selected for processing (2)
  • src/Dom/focus.ts (1 hunks)
  • tests/focus.test.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/focus.test.ts (1)
src/Dom/focus.ts (1)
  • triggerFocus (109-139)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / react component workflow
🔇 Additional comments (2)
src/Dom/focus.ts (1)

101-103: 接口设计合理!

InputFocusOptions 接口扩展了标准的 FocusOptions,添加了 cursor 选项,设计清晰且类型安全。

tests/focus.test.ts (1)

35-58: 测试覆盖全面!

新增的测试用例全面验证了 triggerFocus 函数的三种光标位置选项('start'、'end'、'all'),并正确验证了 focussetSelectionRange 的调用。测试实现遵循最佳实践,包括正确的 spy 清理。

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