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: tab to select an active option #1078

Merged

Conversation

nathanlao
Copy link
Contributor

@nathanlao nathanlao commented Oct 13, 2024

This is a

keyboard accessibility

Background

When the dropdown is open and an option is active, pressing Tab should select the currently active option.
Fix ant-design/ant-design#26876

Solution

  • Handle the case for key TAB in onSelect event
  • Added test case

Summary by CodeRabbit

  • 新特性
    • 增强了OptionList组件的键盘导航功能,支持使用Tab和Enter键选择选项。
  • 测试
    • 添加了新的测试用例,验证Tab键选择活动选项的行为。

Copy link

vercel bot commented Oct 13, 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 Oct 13, 2024 9:15am

Copy link
Contributor

coderabbitai bot commented Oct 13, 2024

Walkthrough

在此拉取请求中,src/OptionList.tsx 组件进行了更新,以增强键盘导航功能。onKeyDown 方法现在处理 KeyCode.TAB 键,允许用户使用 Tab 和 Enter 键选择选项。同时,测试文件 tests/OptionList.test.tsx 中增加了一个新测试用例,以验证在按下 Tab 键时是否能够正确选择活动选项。整体结构和逻辑保持不变,未对核心功能或控制流进行修改。

Changes

文件 更改摘要
src/OptionList.tsx 更新 onKeyDown 方法以处理 KeyCode.TAB 键,注释已修改为“Select (Tab / Enter)”。
tests/OptionList.test.tsx 添加新测试用例“should tab key select an active option”,验证 Tab 键的选择功能。

Assessment against linked issues

Objective Addressed Explanation
Tabbing to select active option in (#26876)

Possibly related PRs

Poem

在选项中跳跃,兔子欢快,
Tab 键轻触,选择无碍。
选项如花,随风摇摆,
兔子欢呼,乐在其中,
让我们一起,享受这场盛会! 🐇✨


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

codecov bot commented Oct 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.24%. Comparing base (601a209) to head (e18d975).
Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1078   +/-   ##
=======================================
  Coverage   98.24%   98.24%           
=======================================
  Files          39       39           
  Lines        1482     1483    +1     
  Branches      418      447   +29     
=======================================
+ Hits         1456     1457    +1     
  Misses         26       26           

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

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 (2)
tests/OptionList.test.tsx (1)

205-243: 测试用例结构清晰,覆盖了关键功能点

这个新增的测试用例很好地验证了Tab键选择活动选项的功能,符合PR的目标和#26876问题的要求。测试步骤逻辑清晰,包括了打开下拉菜单、更改活动选项、选择选项和关闭下拉菜单等关键操作。

为了进一步提高可读性,建议在每个主要步骤前添加简短的注释,例如:

// 打开下拉菜单
act(() => {
  toggleOpen(true);
});

// 模拟按下向下箭头键
act(() => {
  listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
});

// 验证活动选项已更改
expect(onActiveValue).toHaveBeenCalledWith(
  '2',
  expect.anything(),
  expect.objectContaining({ source: 'keyboard' }),
);

// 模拟按下Tab键
act(() => {
  listRef.current.onKeyDown({ which: KeyCode.TAB } as any);
});

// 验证选项被选中并且下拉菜单关闭
expect(onSelect).toHaveBeenCalledTimes(1);
expect(onSelect).toHaveBeenCalledWith('2', expect.objectContaining({ selected: true }));
expect(toggleOpen).toHaveBeenCalledWith(false);

这样可以使测试用例的结构和目的更加清晰。

src/OptionList.tsx (1)

Line range hint 220-234: Tab键处理实现正确,建议小幅改进

Tab键的处理逻辑与Enter键一致,这是正确的实现。它检查了活动项是否存在、是否禁用,以及是否超过最大数量限制,这些都是必要的检查。

为了提高代码的可读性,我们可以考虑将Tab和Enter的处理逻辑提取到一个单独的函数中。例如:

+const handleSelection = () => {
+  const item = memoFlattenOptions[activeIndex];
+  if (item && !item?.data?.disabled && !overMaxCount) {
+    onSelectValue(item.value);
+  } else {
+    onSelectValue(undefined);
+  }
+};

 case KeyCode.TAB:
 case KeyCode.ENTER: {
-  const item = memoFlattenOptions[activeIndex];
-  if (item && !item?.data?.disabled && !overMaxCount) {
-    onSelectValue(item.value);
-  } else {
-    onSelectValue(undefined);
-  }
+  handleSelection();

   if (open) {
     event.preventDefault();
   }

   break;
 }

这样可以减少重复代码,并使逻辑更加清晰。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 601a209 and e18d975.

📒 Files selected for processing (2)
  • src/OptionList.tsx (1 hunks)
  • tests/OptionList.test.tsx (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
src/OptionList.tsx (2)

Line range hint 1-624: 总体评审:键盘导航功能得到改进

此PR成功地实现了使用Tab键选择活动选项的功能,提高了组件的键盘可访问性。更改是针对性的,并且与现有代码库保持一致。主要修改点如下:

  1. onKeyDown处理程序中添加了对Tab键的支持。
  2. Tab键的处理逻辑与Enter键保持一致,确保了行为的一致性。

这些更改很好地解决了issue #26876中提出的问题,同时没有引入明显的回归或副作用。

建议在合并此PR之前,确保进行充分的测试,特别是针对键盘导航和选择行为的测试。


218-219: 键盘导航功能增强

这个更改增加了对Tab键的支持,使其能够选择当前活动的选项。这符合PR的目标,提高了键盘导航的可访问性。

为了验证这个更改的影响,我们可以运行以下脚本来检查是否有其他地方使用了类似的键盘事件处理逻辑:

✅ Verification successful

为了修正之前脚本中的错误,请运行以下修正后的脚本以重新验证:


键盘导航功能增强已验证

通过运行搜索脚本,我们确认除了 src/OptionList.tsx 外,没有其他文件使用类似的键盘事件处理逻辑。该更改专注于增强选项列表的键盘导航功能,因此本次审查通过。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:搜索其他可能需要类似Tab键处理的地方

# 测试:搜索其他包含键盘事件处理的文件
rg --type typescript "case KeyCode\.(ENTER|ESC|UP|DOWN)"

Length of output: 93


Script:

#!/bin/bash
# 描述:搜索其他可能需要类似Tab键处理的地方

# 测试:搜索其他包含键盘事件处理的文件
rg "case KeyCode\.(ENTER|ESC|UP|DOWN)" --glob "*.ts" --glob "*.tsx"

Length of output: 257

@nathanlao
Copy link
Contributor Author

@afc163 麻烦有空可以看看这个小改动, 支持tab to select 感谢!

@override-apps
Copy link

+1

@afc163 afc163 merged commit 33a598f into react-component:master Nov 7, 2024
11 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.

Tabbing to select active option in <Select>
3 participants