Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getInputElement为textarea时,上下键用默认行为 #1072

Merged
merged 2 commits into from
Sep 28, 2024

Conversation

Jarryxin
Copy link
Contributor

@Jarryxin Jarryxin commented Sep 27, 2024

使用多行文本,listbox未展示时,上下键用默认行为
fix ant-design/ant-design#50935

Summary by CodeRabbit

  • 新功能

    • 更新了选择器组件的键盘事件处理,以支持文本框和文本区域的输入。
    • 允许在文本区域中使用箭头键进行光标导航。
  • 测试

    • 为选择器组件添加了新的测试用例,以验证文本区域的光标移动和输入行为。

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
select ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 10:44am

Copy link
Contributor

coderabbitai bot commented Sep 27, 2024

Walkthrough

此次更改涉及Selector组件的onInternalInputKeyDown事件处理程序,更新了其类型定义以支持HTMLInputElementHTMLTextAreaElement。逻辑调整确保在文本区域关闭下拉菜单时,用户可以使用箭头键进行光标导航。此外,在tests/Select.test.tsx中新增了多个测试用例,验证了在Select组件中使用文本区域时的用户交互行为及其对下拉列表的影响。

Changes

文件 更改摘要
src/Selector/index.tsx 更新onInternalInputKeyDown方法签名以支持HTMLTextAreaElement,并调整逻辑以改善键盘导航行为。
tests/Select.test.tsx 新增测试用例“move the cursor in the textarea”和“Moving the cursor in TextArea does not display the listbox”,验证文本区域在Select组件中的光标移动行为及下拉列表的显示状态。

Assessment against linked issues

Objective Addressed Explanation
AutoComplete嵌套Input.TextArea当浮层没展示时,arrow up down也不生效 (#50935)

Poem

在草丛中跳跃的小兔子,
改变代码如春风拂面,
箭头键舞动,光标轻盈,
文本区域中欢声笑语,
让我们一起庆祝这变化,
编程的乐趣,永不停歇! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1bd097d and 59210e0.

📒 Files selected for processing (1)
  • tests/Select.test.tsx (1 hunks)
🔇 Additional comments (2)
tests/Select.test.tsx (2)

979-1050: 代码结构清晰,测试用例设计合理。

这个测试用例很好地覆盖了在Select组件中使用textarea的主要功能。以下是一些小建议:

  1. 考虑将一些重复的模拟函数初始化抽取到一个辅助函数中,以提高代码复用性。
  2. 可以添加更多的断言来验证中间状态,而不仅仅是最终状态。
  3. 考虑测试一些边缘情况,比如空输入、非常长的输入等。

总的来说,这是一个很好的补充,能够提高代码的可靠性。


1052-1083: 测试用例针对性强,验证了重要的交互行为。

这个测试用例很好地验证了在textarea中移动光标不会触发下拉列表的显示,这是一个重要的交互细节。建议:

  1. 考虑添加对其他可能影响下拉列表显示的按键的测试,如Tab键或Esc键。
  2. 可以在每次按键操作后都验证一下下拉列表的状态,而不仅仅是在最后进行验证。
  3. 考虑测试在不同的初始状态下(如已经有文本输入的情况)的行为。

总的来说,这个测试用例是一个很好的补充,有助于确保组件的正确行为。


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
src/Selector/index.tsx (1)

Line range hint 142-164: 逻辑更新正确处理了文本区域的箭头键行为

这些更改很好地解决了问题 #50935 中描述的行为。现在,当输入元素是文本区域且下拉列表关闭时,箭头键可以正常用于文本导航。这提高了组件的可用性和一致性。

建议优化:考虑将 isTextAreaElement 的检查移到函数开头,这样可以避免在每次键盘事件中重复进行这个检查。

您可以考虑如下重构:

  const onInternalInputKeyDown: React.KeyboardEventHandler<
    HTMLInputElement | HTMLTextAreaElement
  > = (event) => {
    const { which } = event;
+   const isTextAreaElement = inputRef.current instanceof HTMLTextAreaElement;

-   // Compatible with multiple lines in TextArea
-   const isTextAreaElement = inputRef.current instanceof HTMLTextAreaElement;
    if (!isTextAreaElement && open && (which === KeyCode.UP || which === KeyCode.DOWN)) {
      event.preventDefault();
    }

    // ... (rest of the function)

    // Move within the text box
    if (
      isTextAreaElement &&
      !open &&
      ~[KeyCode.UP, KeyCode.DOWN, KeyCode.LEFT, KeyCode.RIGHT].indexOf(which)
    ) {
      return;
    }
    // ... (rest of the function)
  };

这个小改动可以稍微提高性能,特别是在频繁触发键盘事件的情况下。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 561f8b7 and 1bd097d.

📒 Files selected for processing (2)
  • src/Selector/index.tsx (2 hunks)
  • tests/Select.test.tsx (1 hunks)
🔇 Additional comments (3)
src/Selector/index.tsx (2)

137-139: 类型定义更新有助于支持多行输入

这个更改扩展了 onInternalInputKeyDown 事件处理器的类型定义,使其同时支持 HTMLInputElementHTMLTextAreaElement。这是一个很好的改进,因为它允许组件在处理单行输入和多行文本区域时使用相同的事件处理逻辑。


Line range hint 137-164: 总体评价:改进了文本区域的处理,提高了组件的可用性

这些更改很好地解决了 AutoComplete 组件中使用文本区域时的箭头键导航问题。主要改进包括:

  1. 扩展了事件处理器的类型定义,支持both HTMLInputElementHTMLTextAreaElement
  2. 为文本区域输入添加了特殊的逻辑处理,允许在下拉列表关闭时使用箭头键进行文本导航。

这些修改提高了组件的灵活性和用户体验,特别是在处理多行输入时。代码质量良好,符合现有的编码风格。

建议:考虑添加单元测试来验证这些新的行为,确保在未来的更改中不会意外破坏这些功能。

tests/Select.test.tsx (1)

978-1050: 代码看起来不错,但有一些改进的空间。

这个测试用例很好地覆盖了在Select组件中使用textarea的关键行为。然而,我有以下几点建议来增强测试的清晰度和完整性:

  1. 考虑添加对模拟函数(如onKeyDown、onChange等)被调用的明确断言。这将确保事件处理器按预期工作。

  2. 为某些操作添加注释,解释它们的目的。例如,mousedown事件的作用可能不是立即明显的。

  3. 使用更具描述性的变量名。例如,startend可以改为cursorStartcursorEnd

  4. 考虑将测试拆分为多个较小的测试用例,每个用例专注于特定的行为(如换行、光标移动等)。这将提高测试的可读性和可维护性。

  5. 添加更多的断言来验证中间状态,而不仅仅是最终状态。这将有助于在失败时更容易定位问题。

总的来说,这是一个很好的测试用例,只需要一些小的调整就能变得更加强大和清晰。

Copy link

codecov bot commented Sep 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.23%. Comparing base (561f8b7) to head (59210e0).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1072   +/-   ##
=======================================
  Coverage   98.22%   98.23%           
=======================================
  Files          39       39           
  Lines        1467     1473    +6     
  Branches      411      444   +33     
=======================================
+ Hits         1441     1447    +6     
  Misses         26       26           

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

@afc163
Copy link
Member

afc163 commented Sep 27, 2024

用例要补一下。

if (which === KeyCode.UP || which === KeyCode.DOWN) {
// Compatible with multiple lines in TextArea
const isTextAreaElement = inputRef.current instanceof HTMLTextAreaElement;
if (!isTextAreaElement && open && (which === KeyCode.UP || which === KeyCode.DOWN)) {
Copy link
Member

Choose a reason for hiding this comment

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

open 且显示 not found 的时候,也可以走默认行为。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

测试 open 且显示 not found 的时候会走默认行为

Copy link
Contributor Author

Choose a reason for hiding this comment

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

只有textarea时,才需要处理上下的默认行为,这的判断只要是textarea就不会阻止默认行为。
如果是 (单行&open&not found),按上下只能到行首和行尾,需要处理这种吗。

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.

AutoComplete嵌套Input.TextArea当浮层没展示时,arrow up down也不生效
2 participants