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: fix error when pass disabled and press enter. #81

Merged
merged 5 commits into from
Nov 29, 2024

Conversation

r0mm4k
Copy link
Contributor

@r0mm4k r0mm4k commented Nov 5, 2024

Resolve: #79

Summary by CodeRabbit

  • 新功能
    • 改进了输入组件对组合事件和按键管理的处理。
    • 增强了在禁用状态下的键锁状态管理。
    • 优化了输入事件的触发逻辑,避免了重复触发。
  • 测试
    • 添加了新的测试用例,以验证在输入框失去焦点后和禁用状态下的按键事件触发。

Copy link

coderabbitai bot commented Nov 5, 2024

Walkthrough

此次更改主要集中在 src/Input.tsx 文件中的 Input 组件。通过在 useEffect 钩子中添加新行,确保在 disabled 属性变化时重置 keyLockRef.currentfalse。此外,更新了 triggerChange 函数,以避免在组合事件中重复触发变更事件。对 onInternalCompositionEnd 函数进行了调整,以正确处理组合结束事件,并修改了 handleKeyDown 函数,以确保在按下 Enter 键时仅在 keyLockRef.current 未设置的情况下调用 onPressEnter 回调。最后,新增了一些测试用例以验证这些更改。

Changes

文件路径 更改摘要
src/Input.tsx - 在 useEffect 中添加重置 keyLockRef.current 的逻辑。
- 更新 triggerChange 函数,避免在 info.source'compositionEnd' 时执行逻辑。
- 修改 onInternalCompositionEnd 函数以正确处理组合结束事件。
- 更新 handleKeyDown 函数以检查 onPressEnter 回调的调用条件。
tests/index.test.tsx - 新增测试用例以验证 onPressEnter 在失去焦点后仍能触发的情况,以及在输入框禁用后仍能触发的情况。

Assessment against linked issues

Objective Addressed Explanation
在输入框失去焦点后,确保 onPressEnter 能够触发 (#79)

🐰 在输入框中跳跃,轻松应对,
按下回车,状态不再错落。
组合结束,事件不再混淆,
小兔子欢快,输入流畅无忧! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 038c941 and f354b57.

📒 Files selected for processing (1)
  • tests/index.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/index.test.tsx

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

@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: 1

🧹 Outside diff range and nitpick comments (2)
src/Input.tsx (2)

Line range hint 169-174: 需要在 handleBlur 中重置 keyLockRef

根据 PR 的目标,当输入框失去焦点时应该重置 keyLockRef,但当前实现中缺少这个逻辑。这可能导致在某些场景下 onPressEnter 事件仍然无法正常触发。

建议在 handleBlur 函数中添加重置逻辑:

  const handleBlur: React.FocusEventHandler<HTMLInputElement> = (e) => {
    setFocused(false);
+   keyLockRef.current = false;
    onBlur?.(e);
  };
🧰 Tools
🪛 Biome

[error] 97-97: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

🪛 eslint

[error] 97-97: Expected an assignment or function call and instead saw an expression.

(@typescript-eslint/no-unused-expressions)


Line range hint 40-42: 建议添加状态管理相关的代码注释

组件使用了多个 ref 来管理不同的状态(compositionRef, keyLockRef),建议添加注释说明每个 ref 的具体用途和状态转换逻辑,以提高代码的可维护性。

建议添加如下注释:

+ // 用于追踪 IME 输入状态
  const compositionRef = useRef(false);
+ // 用于防止重复触发 Enter 键事件
  const keyLockRef = useRef(false);
🧰 Tools
🪛 Biome

[error] 97-97: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

🪛 eslint

[error] 97-97: Expected an assignment or function call and instead saw an expression.

(@typescript-eslint/no-unused-expressions)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e3b6dd6 and 7aacd22.

📒 Files selected for processing (1)
  • src/Input.tsx (1 hunks)
🧰 Additional context used
🪛 Biome
src/Input.tsx

[error] 97-97: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

🪛 eslint
src/Input.tsx

[error] 97-97: Expected an assignment or function call and instead saw an expression.

(@typescript-eslint/no-unused-expressions)

🔇 Additional comments (1)
src/Input.tsx (1)

Line range hint 126-131: 组合输入事件处理逻辑正确

针对 IME 输入重复触发的问题(#46587)的修复方案是合理的。通过在组合输入结束时提前返回,可以有效避免重复触发事件。

🧰 Tools
🪛 Biome

[error] 97-97: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

🪛 eslint

[error] 97-97: Expected an assignment or function call and instead saw an expression.

(@typescript-eslint/no-unused-expressions)

src/Input.tsx Outdated Show resolved Hide resolved
@afc163
Copy link
Member

afc163 commented Nov 7, 2024

Test case is needed.

Copy link

codecov bot commented Nov 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.34%. Comparing base (e3b6dd6) to head (f354b57).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #81      +/-   ##
==========================================
+ Coverage   92.20%   92.34%   +0.14%     
==========================================
  Files           5        5              
  Lines         218      222       +4     
  Branches       77       79       +2     
==========================================
+ Hits          201      205       +4     
  Misses         17       17              

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

@r0mm4k
Copy link
Contributor Author

r0mm4k commented Nov 11, 2024

Test case is needed.

Added.

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

🧹 Outside diff range and nitpick comments (1)
tests/index.test.tsx (1)

77-87: 测试用例基本合理,建议增强验证

测试用例验证了禁用状态切换后 onPressEnter 的行为,这是个很好的边界场景测试。建议增加以下改进:

  1. 添加对 disabled 状态的断言
  2. 验证输入框的实际状态变化

建议按如下方式增强测试:

 it('should trigger onPressEnter after disabled', () => {
   const onPressEnter = jest.fn();
   const { container, rerender } = render(
     <Input onPressEnter={onPressEnter} />,
   );
   const inputEl = container.querySelector('input')!;
+  expect(inputEl.disabled).toBe(false);
   fireEvent.keyDown(inputEl, { key: 'Enter' });
   rerender(<Input onPressEnter={onPressEnter} disabled={true} />);
+  expect(inputEl.disabled).toBe(true);
   fireEvent.keyDown(inputEl, { key: 'Enter' });
   expect(onPressEnter).toBeCalledTimes(2);
+  expect(inputEl).toBeDisabled();
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 963ab6c and 038c941.

📒 Files selected for processing (2)
  • src/Input.tsx (2 hunks)
  • tests/index.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Input.tsx
🔇 Additional comments (1)
tests/index.test.tsx (1)

63-75: 测试用例设计合理!

该测试用例很好地验证了失焦后 onPressEnter 事件的触发行为,完全符合修复需求。测试结构清晰,断言准确。

@r0mm4k
Copy link
Contributor Author

r0mm4k commented Nov 29, 2024

@afc163 Can you take a look?

@afc163 afc163 merged commit f615ec6 into react-component:master Nov 29, 2024
9 checks passed
@r0mm4k r0mm4k deleted the fix-pass-disabled-press-enter branch December 2, 2024 09:24
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.

onPressEnter failed to trigger after the input blurs. 输入框失焦后onPressEnter不会被触发
2 participants