Skip to content

fix: allow keyboard navigation on selected options when maxCount is reached #1088

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

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

aojunhao123
Copy link
Contributor

@aojunhao123 aojunhao123 commented Nov 15, 2024

🐛 问题描述

rc-select 在达到 maxCount 时存在交互不一致的问题:

  • 鼠标操作:可以继续激活(hover)已选中的选项
  • 键盘操作:无法通过上下键激活任何选项(包括已选中的选项)
  • 8518369ec42b82f1d7acba429f78feaf

🔨 修复方案

统一了鼠标与键盘的交互行为:

  • 当达到 maxCount 时,允许通过键盘上下键在已选中的选项间切换
  • 保持未选中选项不可激活的限制
  • 确保键盘操作的可访问性体验与鼠标操作保持一致

🧪 测试覆盖

添加了专门的测试用例验证:

  • 达到 maxCount 时可以通过键盘在已选项间导航
  • 上下键都能正常工作
  • 未选中的选项会被正确跳过

📝 备注

这个修复提升了组件的可访问性,使键盘操作的用户获得与鼠标操作相同的体验。

Summary by CodeRabbit

  • 新功能

    • 引入了新的选择逻辑,优化了选项列表组件的选择状态判断。
  • 测试

    • 增加了针对选项列表组件的键盘导航行为的测试,确保在达到最大选择数量时的用户交互限制。

Copy link
Contributor

coderabbitai bot commented Nov 15, 2024

Walkthrough

此次更改在 OptionList 组件中引入了一个新的 isSelected 函数,该函数根据当前模式和 rawValues 集来判断一个值是否被选中。此函数使用 React.useCallback 进行定义,以优化性能。getEnabledActiveIndex 函数的逻辑进行了修改,以包含 isSelected 函数,从而检查项目是否被选中或是否达到最大选择计数。此外,OptionList 组件的测试也进行了增强,增加了对键盘导航行为的测试,确保在达到最大选择计数时的用户交互限制。

Changes

文件路径 更改摘要
src/OptionList.tsx - 新增 isSelected 函数,优化选择逻辑。
- 修改 getEnabledActiveIndex 函数以集成 isSelected
- 删除之前的 isSelected 定义。
- 更新 OptionListProps 类型。
tests/OptionList.test.tsx - 新增测试用例,验证最大选择计数下的键盘导航行为,包括选项间的导航和限制。

Possibly related PRs

Suggested reviewers

  • zombieJ

在兔子洞里跳跃,
新的选择逻辑飞跃。
按键导航不再难,
最大计数有保障。
让我们欢庆这变化,
兔子们一起乐哈哈! 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/OptionList.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Cannot read config file: /.eslintrc.js
Error: Cannot find module '@umijs/fabric/dist/eslint'
Require stack:

  • /.eslintrc.js
  • /node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/eslint/lib/eslint/index.js
  • /node_modules/eslint/lib/cli.js
  • /node_modules/eslint/bin/eslint.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at Object. (/.eslintrc.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)

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

vercel bot commented Nov 15, 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 Nov 15, 2024 2:58pm

@aojunhao123
Copy link
Contributor Author

@yoyo837 @li-jia-nan @afc163 @zombieJ 几位大佬有空看看,小改动

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

421-461: 键盘导航测试逻辑完整,建议补充边缘场景!

当前测试用例已经很好地覆盖了主要功能:

  • 验证了向下键导航到已选选项
  • 验证了在到达末尾时的环绕行为
  • 验证了向上键导航功能
  • 验证了在达到最大选择数量时无法激活未选中选项

建议考虑添加以下测试场景:

  • 当没有选中任何选项时的键盘导航行为
  • 当只有一个选中选项时的键盘导航行为
  • 在选项之间快速连续按键的情况
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 27ac66f and ec6eade.

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

88-97: 实现良好,建议批准!

新增的 isSelected 函数实现清晰,并且通过 useCallback 进行了性能优化。依赖项列表完整,包含了 mode、rawValues 的值和大小变化。


108-108: 键盘导航逻辑修改正确,建议验证边界情况

修改后的条件 isSelected(data.value) || !overMaxCount 很好地实现了PR的目标,允许在达到最大选择数时仍可以通过键盘导航已选中的选项。

建议验证以下场景:

  1. 达到 maxCount 时的键盘导航
  2. 未选中选项的跳过逻辑
  3. 循环导航功能
✅ Verification successful

Let me gather more specific information about the test cases.


Let me check the actual test implementation to verify the test coverage.


键盘导航功能已得到充分验证,无需额外测试

测试用例已经完整覆盖了关键场景:

  • 已验证达到 maxCount (2) 时的键盘导航功能
  • 测试包含了预选状态('1'和'2'被选中)
  • 验证了键盘导航事件的正确触发

代码实现和测试用例都表明该修改是安全的。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索相关测试用例,确保边界情况得到覆盖

# 查找与键盘导航相关的测试
rg -l "keyDown|maxCount.*keyboard|navigate.*selected" --type ts

# 检查测试文件中的相关测试用例
ast-grep --pattern 'test($_, function() {
  $$$
  maxCount
  $$$
  keyDown
  $$$
})'

Length of output: 384


Script:

#!/bin/bash
# 检查 OptionList 相关的键盘导航测试用例
rg "keyDown.*maxCount|maxCount.*keyDown" -B 2 -A 10 tests/OptionList.test.tsx

# 检查键盘导航测试文件中的相关用例
rg "describe.*keyboard|test.*keyboard" -B 1 -A 5 tests/shared/keyDownTest.tsx

# 查找与 maxCount 相关的测试设置
rg "maxCount.*=|maxCount:" -B 2 -A 2 tests/OptionList.test.tsx

Length of output: 663


Script:

#!/bin/bash
# 获取完整的测试用例实现
rg -A 30 "should allow keyboard navigation on selected options when reach maxCount" tests/OptionList.test.tsx

# 检查其他相关的键盘导航测试
rg "it\(.*keyboard.*maxCount" -A 20 tests/

Length of output: 2132

tests/OptionList.test.tsx (1)

397-419: 测试用例结构清晰,设置合理!

测试用例的设置包含了以下优点:

  • 测试目标明确,注释详细说明了三个验证点
  • 正确配置了多选模式和最大选择数量限制
  • 预选值的初始化符合测试场景要求

}
return rawValues.has(value);
},
[mode, [...rawValues].toString(), rawValues.size],
Copy link
Member

@afc163 afc163 Nov 15, 2024

Choose a reason for hiding this comment

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

rawValues.size 放 ...rawValues].toString() 前面,性能好点。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

大佬你好快的合(

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.24%. Comparing base (27ac66f) to head (ec6eade).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1088   +/-   ##
=======================================
  Coverage   98.24%   98.24%           
=======================================
  Files          39       39           
  Lines        1483     1483           
  Branches      441      447    +6     
=======================================
  Hits         1457     1457           
  Misses         26       26           

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

@afc163 afc163 merged commit ec182e5 into react-component:master Nov 15, 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.

2 participants