Skip to content

feat(a11y): improve treeitem check/select acessibility #963

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tommie-lie
Copy link

@tommie-lie tommie-lie commented Jul 18, 2025

Our team welcomed the latest changes to improve accessibility in Tree component.
As we rely heavily on a11y for our tests (using testing-library), we found some problems with the current state:

  • aria-selected was set when the node was selectable not when the node was actually selected.
  • aria-checked is supported not only on the checkbox, but on the whole treeitem as well (cf. https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/treeitem_role)
  • The aria-label for checkable tree items was "Select <title> <title>" because the tree item contains both, the checkbox (which has a good label) and the title itself, hence the double title in the tree items label.

This PR fixes all three problems and makes the tree component even better when used with assistive technology.

Summary by CodeRabbit

  • 新功能

    • 支持通过属性传递多选能力,从树组件传递到节点列表组件,并在相应元素上添加多选相关的 ARIA 属性,提升可访问性。
  • 无障碍改进

    • 树节点标题现在具有关联的无障碍标识符,增强屏幕阅读器的可读性。
    • ARIA 相关属性(如 aria-selected、aria-checked)根据节点的实际状态动态添加,提升语义准确性。
  • 测试

    • 更新了对 aria-selected 属性的测试断言,以反映无障碍属性行为的调整。

Copy link

vercel bot commented Jul 18, 2025

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

Name Status Preview Comments Updated (UTC)
tree ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 18, 2025 5:28pm

Copy link

coderabbitai bot commented Jul 18, 2025

Walkthrough

本次变更主要聚焦于树组件的可访问性增强。具体包括:支持多选状态的属性传递(multiple → multiselectable),为树节点标题生成唯一 ARIA 标识符,并根据节点的可选/可勾选状态精确设置 ARIA 属性。同时,相关测试用例也同步调整以反映这些行为。

Changes

文件或分组 变更摘要
src/NodeList.tsx NodeListProps 新增可选属性 multiselectable,VirtualList 组件根据该属性设置 aria-multiselectable。
src/Tree.tsx render 方法中解构 multiple,并将其作为 multiselectable 传递给 NodeList。
src/TreeNode.tsx 为树节点标题生成唯一 ariaId,根 div 设置 aria-labelledby,aria-selected/aria-checked 逻辑调整。
tests/TreeNodeProps.spec.tsx 测试用例调整,校验 aria-selected 属性的行为与实际实现一致,更新相关注释。

Suggested reviewers

  • zombieJ

Poem

🐇
小兔子跳进树丛间,
多选单选皆可见。
ARIA 标签明又亮,
无障碍路更宽广。
测试同步不落下,
代码如诗写春天。

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/NodeList.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@30.0.4_@types+node@24.0.14__postcss@8.5.6/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

src/TreeNode.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@30.0.4_@types+node@24.0.14__postcss@8.5.6/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

src/Tree.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@30.0.4_@types+node@24.0.14__postcss@8.5.6/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

  • 1 others

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8d8d791 and 99f7743.

⛔ Files ignored due to path filters (3)
  • tests/__snapshots__/Tree.spec.tsx.snap is excluded by !**/*.snap
  • tests/__snapshots__/TreeNodeProps.spec.tsx.snap is excluded by !**/*.snap
  • tests/__snapshots__/TreeProps.spec.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • src/NodeList.tsx (3 hunks)
  • src/Tree.tsx (2 hunks)
  • src/TreeNode.tsx (4 hunks)
  • tests/TreeNodeProps.spec.tsx (2 hunks)
🔇 Additional comments (12)
src/Tree.tsx (2)

375-375: 良好的实现:正确提取 multiple 属性

正确地从 props 中提取 multiple 属性,为后续传递给 NodeList 做准备。


1477-1477: 正确传递多选状态给 NodeList

multiple 属性作为 multiselectable 传递给 NodeList 组件,这样可以正确地设置 aria-multiselectable 属性来提升可访问性。

src/NodeList.tsx (3)

77-77: 正确添加 multiselectable 属性接口

在 NodeListProps 接口中添加了可选的 multiselectable 布尔属性,为多选功能提供了类型支持。


145-145: 正确提取 multiselectable 属性

从 props 中正确提取 multiselectable 属性,为后续使用做准备。


335-335: 正确实现 aria-multiselectable 属性

按照 ARIA 最佳实践,仅在 multiselectable 为真时才设置 aria-multiselectable="true",否则不设置该属性。这种条件性应用方式提升了可访问性。

tests/TreeNodeProps.spec.tsx (2)

201-202: 正确更新测试断言

更新了注释和断言,正确反映了当树不可选择时,没有树项应该具有 aria-selected 属性的新行为。这与 TreeNode.tsx 中的可访问性改进一致。


212-213: 正确测试单节点可选择场景

更新了测试逻辑,正确验证当树可选择但只有一个树项可选择时,只有一个元素应该具有 aria-selected 属性。这与新的条件性 ARIA 属性应用逻辑一致。

src/TreeNode.tsx (5)

50-50: 正确使用 React.useId() 生成唯一标识符

使用 React.useId() 为树节点标题生成唯一的 ARIA 标识符,这是生成唯一 ID 的推荐方式。


387-387: 正确添加标题元素 ID

为标题 span 元素添加了唯一的 id 属性,使其可以被 aria-labelledby 引用,提升了可访问性。


421-422: 正确实现条件性 ARIA 属性

根据节点的可选择性和可勾选性条件性地设置 ARIA 属性:

  • aria-selected 仅在节点可选择时才应用
  • aria-checked 仅在节点可勾选时才应用

这符合 ARIA 最佳实践,避免了不必要的属性。


429-429: 正确实现 aria-labelledby 关联

通过 aria-labelledby 属性将树项与其标题建立语义关联,为屏幕阅读器用户提供了清晰的标签引用。


461-461: 正确应用 aria-checked 属性

ariaChecked 对象正确地展开到根 div 元素上,确保 ARIA 属性得到正确应用。

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this 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.

@tommie-lie tommie-lie changed the title feat(a11y): improve treeitem acessibility feat(a11y): improve treeitem check/select acessibility Jul 18, 2025
Copy link

codecov bot commented Jul 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.84%. Comparing base (8d8d791) to head (99f7743).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #963   +/-   ##
=======================================
  Coverage   99.84%   99.84%           
=======================================
  Files          14       14           
  Lines        1275     1278    +3     
  Branches      393      386    -7     
=======================================
+ Hits         1273     1276    +3     
  Misses          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.

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.

1 participant