Skip to content

fix: Tabs press Enter/Space on active tab should not trigger onChange #790

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 4 commits into from
Apr 1, 2025

Conversation

afc163
Copy link
Member

@afc163 afc163 commented Apr 1, 2025

Summary by CodeRabbit

  • 新功能

    • 为选项卡组件新增了事件处理逻辑,使其能够及时响应选项卡状态变化,提升了交互体验。
    • 在提交前新增了代码检查机制,确保提交的代码符合规范。
    • 新增了对选项卡切换事件的日志记录功能,增强了事件追踪的清晰度。
  • Bug 修复

    • 优化了键盘(Enter/Space 键)操作时选项卡的激活逻辑,确保用户操作后正确切换活跃选项卡。

Copy link

vercel bot commented Apr 1, 2025

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

Name Status Preview Comments Updated (UTC)
tabs ❌ Failed (Inspect) Apr 1, 2025 7:57am

Copy link

coderabbitai bot commented Apr 1, 2025

Walkthrough

本次改动在预提交钩子中增加了 npx lint-staged 命令,用于在提交前对暂存文件执行 lint 检查。同时,对 Tabs 组件的行为做出了调整:新增了 onChange 事件处理方法,并修改了 onTabClick 的日志输出以及键盘事件处理逻辑。此外,项目配置文件 package.json 更新了脚本、依赖项(增加了 huskylint-stagedprettier,移除了 coveralls)和 lint-staged 配置,测试用例也做了相应的修改以验证新逻辑。

Changes

文件(s) 改动说明
.husky/pre-commit 添加了 npx lint-staged 命令,用于在提交前执行 linting 检查
package.json 新增 "prepare": "husky && dumi setup" 脚本;移除 coveralls 依赖;新增依赖 huskylint-stagedprettier;添加 lint-staged 配置
docs/.../basic.tsx, src/TabNavList/index.tsx,
tests/accessibility.test.tsx
在 Tabs 组件中新增 onChange 方法,修改 onTabClick 的日志信息与参数(由 focusKey 改为 activeKey);调整键盘事件处理逻辑及测试验证

Sequence Diagram(s)

sequenceDiagram
    participant Dev as 开发者
    participant Git as Git 钩子
    participant Lint as lint-staged

    Dev->>Git: 执行 git commit
    Git->>Lint: 调用 npx lint-staged
    Lint-->>Git: 返回 lint 结果
    Git-->>Dev: 继续提交(若 lint 校验通过)
Loading
sequenceDiagram
    participant User as 用户
    participant Tabs as Tabs 组件
    participant ClickHandler as onTabClick
    participant ChangeHandler as onChange

    User->>Tabs: 点击或按键选择标签页
    Tabs->>ClickHandler: 调用 onTabClick(activeKey) 并记录日志
    alt 非键盘触发且标签页变化
        Tabs->>ChangeHandler: 触发 onChange (传递新选中 key)
    else 若为键盘事件
        Note right of Tabs: onChange 不会被调用
    end
Loading

Possibly related PRs

Poem

我是一只快乐的小兔子,
跳跃在代码的森林里;
lint-staged 护航每次提交,
Tabs 变换如风般轻盈;
键盘敲响,日志飞扬,
春日代码,灿烂如花 🌸!


📜 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 66dddce and 6937485.

📒 Files selected for processing (1)
  • package.json (3 hunks)
🔇 Additional comments (6)
package.json (6)

37-38: 新增脚本命令的改动
这里新增了 "test": "rc-test""prepare": "husky && dumi setup" 命令,用于改进测试流程以及在提交前自动执行钩子与文档配置。请确认这些命令与项目其它脚本协同工作,并验证相关配置(例如 .husky 文件)是否正确设置。


49-50: 新增 ESLint TypeScript 插件与解析器
devDependencies 中新增了 "@typescript-eslint/eslint-plugin": "^5.59.7""@typescript-eslint/parser": "^5.59.7"。建议确认这些新依赖与现有 ESLint 配置及项目环境兼容,确保 lint 检查运行无误。


70-70: 添加 husky 依赖
新增 "husky": "^9.1.7" 依赖,为项目提供提交钩子支持,从而在提交前执行必要的检查和格式化。请检查 husky 配置文件(如 .husky/pre-commit)是否同时更新并与该版本兼容。


72-72: 新增 lint-staged 依赖
devDependencies 中加入 "lint-staged": "^15.5.0",用于在提交前对暂存的文件进行预格式化。请确认 lint-staged 在项目中的集成无误,并与 husky 配合正常工作。


73-73: 新增 prettier 依赖
添加了 "prettier": "^3.5.3" 用于代码格式化。建议检查 Prettier 配置是否符合项目代码风格,并确保与 lint-staged 的调用配置一致。


86-88: 新增 lint-staged 配置
在根配置中引入了 "lint-staged": { "*": "prettier --write --ignore-unknown" },以便在提交前自动格式化所有暂存文件。建议根据项目需求确认通配符的范围是否合适,或是否需要针对特定文件类型做更精细的配置。


🪧 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@afc163 afc163 force-pushed the fix/keyboard-trigger-onChange branch from daa7e48 to ad3c99b Compare April 1, 2025 07:26
Copy link

socket-security bot commented Apr 1, 2025

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] None 0 4.04 kB typicode
npm/[email protected] Transitive: environment +12 328 kB
npm/[email protected] None 0 0 B

🚮 Removed packages: npm/@types/[email protected], npm/[email protected]

View full report↗︎

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)
package.json (1)

90-92: 新增 lint-staged 配置
在 package.json 中添加了 lint-staged 配置,针对所有文件执行 prettier --write --ignore-unknown 操作。这一配置可以帮助自动格式化代码,但建议检查是否对所有文件执行格式化是合理的。如果项目中存在某些不需要格式化或格式化后可能出问题的文件(比如编译生成文件),可以考虑进一步细化匹配模式。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 629f13b and ad3c99b.

📒 Files selected for processing (5)
  • .husky/pre-commit (1 hunks)
  • docs/examples/basic.tsx (1 hunks)
  • package.json (3 hunks)
  • src/TabNavList/index.tsx (1 hunks)
  • tests/accessibility.test.tsx (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
src/TabNavList/index.tsx (1)
docs/examples/mix.tsx (1)
  • activeKey (20-174)
docs/examples/basic.tsx (1)
docs/examples/animated.tsx (1)
  • React (14-50)
🔇 Additional comments (12)
.husky/pre-commit (1)

1-1: 预提交钩子添加了 lint-staged 校验

添加 npx lint-staged 命令确保提交前执行代码校验,有助于维护代码质量。

src/TabNavList/index.tsx (1)

378-380: 修复了在激活标签上按Enter/Space键时触发onChange的问题

按下Enter/Space键时,现在传递activeKey代替之前的focusKeyonTabClick函数,确保用户在当前激活的标签上按Enter/Space时不会触发不必要的状态变更。

  1. 添加了按键事件日志输出
  2. 修改了传递给onTabClick的参数

这个修改符合PR标题描述的行为修复需求。

tests/accessibility.test.tsx (3)

94-97: 为键盘交互测试添加onChange断言检查

添加了onChange模拟函数并在创建测试组件时传入,这样可以验证修复的有效性。


105-105: 验证Space键不触发onChange

确保按下空格键激活标签时不会触发onChange回调,只会调用onTabClick


113-113: 验证Enter键不触发onChange

确保按下Enter键激活标签时不会触发onChange回调,只会调用onTabClick

docs/examples/basic.tsx (3)

40-42: 改进了onTabClick回调的日志记录

修改了日志输出,从简单的'key'改为更有描述性的'onTabClick',便于理解日志输出的来源。


44-46: 添加了onChange回调函数

新增了onChange函数,用于处理标签切换事件,并通过日志输出记录切换的标签key。这与修复内容相关,用于区分点击事件与实际的标签切换事件。


53-53: 为Tabs组件添加onChange属性

将新增的onChange函数传给Tabs组件,完成了对标签切换事件的监听。这个改动与PR的修复目标一致,使组件能够在标签实际变更时触发onChange回调。

package.json (4)

71-71: 新增 Husky 依赖
在 devDependencies 中新增了 "husky": "^9.1.7",这为 Git 钩子的设置提供支持。请确保该版本与项目中其它工具的集成没有冲突。


74-74: 新增 lint-staged 依赖
新增了 "lint-staged": "^15.5.0" 依赖,用于在提交前对暂存文件自动运行代码格式化操作。此改动符合代码质量要求,建议确认版本与项目中其他工具的兼容性。


76-76: 新增 Prettier 依赖
新增了 "prettier": "^3.5.3" 依赖,与 lint-staged 配置一起用于统一代码风格。请确认 Prettier 的配置文件(如果存在)与项目需求一致。


37-38:

❓ Verification inconclusive

新增 prepare 脚本的调整
在 scripts 中新增的 "prepare": "husky && dumi setup" 脚本用于在提交前设置 Git 钩子和执行 dumi 的初始化工作。请确认这里的 husky 命令是否满足预期,通常 Husky 的官方推荐是使用 "husky install" 来初始化 Git 钩子。如果这个命令是经过验证符合项目需求,则此改动是合理的。


请确认 prepare 脚本中 husky 命令的正确性

在 package.json 文件的 scripts 部分新增的

"prepare": "husky && dumi setup"

用于在提交前配置 Git 钩子和执行 dumi 初始化。需要注意,Husky 官方推荐的初始化方式通常为 "husky install"。如果当前项目经过验证后确实适用于直接使用 husky 命令,则该改动无碍;如果验证后存在问题,建议将该命令调整为 "husky install && dumi setup" 以确保 Git 钩子能正确安装。

  • 文件位置:package.json (行 37-38)

Copy link

codecov bot commented Apr 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.96%. Comparing base (629f13b) to head (6937485).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #790   +/-   ##
=======================================
  Coverage   98.96%   98.96%           
=======================================
  Files          18       18           
  Lines         772      773    +1     
  Branches      227      220    -7     
=======================================
+ Hits          764      765    +1     
  Misses          8        8           

☔ 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