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: onSearch be call onBlur #1085

Closed

Conversation

su-muzhi
Copy link

@su-muzhi su-muzhi commented Nov 12, 2024

fix ant-design/ant-design#50642

Summary by CodeRabbit

  • 新功能
    • 改进了下拉选择组件的搜索和下拉可见性逻辑,提升了用户交互体验。
  • 测试
    • 更新了选择组件的测试用例,确保在用户输入和选择项时的行为符合预期。
    • 新增了针对选择组件的模糊事件的单元测试,验证了不同模式下的功能表现。
    • 修改了输入过滤测试中的断言逻辑,使其更准确地反映选择后的输入值。

Copy link

vercel bot commented Nov 12, 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 Dec 10, 2024 9:34am

Copy link
Contributor

coderabbitai bot commented Nov 12, 2024

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.

tests/Blur.test.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/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/[email protected]/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)
## Walkthrough
该拉取请求对 `BaseSelect` 组件进行了更新,主要改进了下拉菜单的可见性和搜索功能的逻辑。具体来说,`onInternalSearch` 函数增加了条件判断,以确保在关闭下拉菜单时,只有在模式不为 'combobox'、'multiple' 或 'tags' 时才重置搜索值。此外,`onInternalKeyDown` 函数被修改,以防止在按下 Enter 键时提交表单,前提是模式不是 'combobox'。这些更改旨在提升搜索输入和下拉菜单的交互逻辑。

## Changes

| 文件路径                      | 更改摘要                                                                                  |
|-----------------------------|---------------------------------------------------------------------------------------|
| src/BaseSelect/index.tsx    | 更新了 `BaseSelect` 组件的下拉菜单可见性和搜索功能逻辑,修改了 `onInternalSearch``onInternalKeyDown` 函数。 |
| tests/Select.test.tsx       | 修改了测试用例以确保搜索输入和下拉项之间的交互正确,更新了快照测试。                          |
| tests/Blur.test.tsx         | 新增了 `Blur.test.tsx` 测试文件,验证 `Select` 组件在失去焦点时的行为。                     |
| tests/shared/inputFilterTest.tsx | 修改了 `inputFilterTest` 中的测试逻辑,以根据模式更新输入值的期望。                       |

## Assessment against linked issues

| Objective                                                                 | Addressed | Explanation                  |
|---------------------------------------------------------------------------|-----------|------------------------------|
| 当mode为multiple时,失去焦点后不再请求sug接口 ||                              |

## Possibly related PRs
- **#1060**: 涉及 `BaseSelect` 组件的下拉菜单可见性和搜索功能的更改,可能与 `Select` 组件的文档和属性更新相关。
- **#1068**: 对 `Select` 组件排序功能的修改可能影响选项的显示和交互,相关于下拉菜单可见性和搜索逻辑的增强。
- **#1072**: `Selector` 组件中 `onInternalInputKeyDown` 事件处理程序的更改可能与下拉菜单可见性和键盘交互相关。
- **#1077**: `OptionList` 组件的可访问性特性和选择逻辑的更新可能与下拉菜单可见性和搜索功能的改进相关。
- **#1078**: `OptionList` 组件中键盘导航能力的增强与主要拉取请求的下拉交互改进相一致。
- **#1090**: `Select` 组件中 `onChange` 回调类型定义的更改可能与下拉菜单的交互逻辑相关。
- **#1092**: 修复 Edge 浏览器中键触发问题的更改可能与下拉菜单可见性和键盘交互的改进相关。

## Suggested reviewers
- afc163

> 兔子欢快地跳跃,  
> 下拉菜单更灵活,  
> 搜索功能更智能,  
> 用户体验真美好!  
> 让我们一起欢庆,  
> 代码更新真精彩! 🐇✨

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4402aa5 and 0152915.

📒 Files selected for processing (1)
  • tests/Blur.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/Blur.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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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
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: 2

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

128-149: 建议完善测试用例的覆盖范围

当前测试用例存在以下需要改进的地方:

  1. 测试名称应该反映showSearch={true}的设置
  2. 缺少对初始searchValue="1"的验证
  3. 建议添加showSearch={false}的场景测试

建议修改如下:

-  it('mode with null, onContainerBlur params is blur', () => {
+  it('mode为null且showSearch为true时,onContainerBlur触发onSearch', () => {
     const onSearch = jest.fn();
     const { container } = render(
       // ... existing code ...
     );
+    // 验证初始值
+    expect(container.querySelector('input').value).toBe('1');
     // ... remaining code ...
   });

+  it('mode为null且showSearch为false时,onContainerBlur不触发onSearch', () => {
+    // 添加showSearch=false的测试用例
+  });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

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

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

127-127: 测试结构清晰明确!

测试套件的命名准确反映了其测试目的,结构符合最佳实践。


127-174: 建议增加测试用例以确保完整的行为覆盖

当前的测试用例集没有完全覆盖PR的目标场景。为确保修复的完整性,建议添加以下测试场景:

  1. 单选模式下的行为测试,以便与多选模式进行对比
  2. 验证重新获得焦点时是否会触发新的搜索请求
  3. 测试空值和特殊字符的搜索行为
src/BaseSelect/index.tsx (1)

438-438: 搜索事件源的逻辑修改看起来符合预期

这个改动通过在 mergedShowSearch 为 true 时将搜索事件源从 'typing' 或 'effect' 更改为 'blur',解决了重新获得焦点时不必要的建议请求问题。这与 PR 的目标一致。

运行以下脚本验证此更改是否正确处理了所有搜索事件源:

✅ Verification successful

Let me gather more context about the search event handling to ensure a comprehensive verification.


Let me try one more verification to check the component's search-related logic:


让我检查一下相关的测试文件以确保这个改动的行为是否符合预期:


搜索事件源的修改已得到验证,实现符合预期

根据代码分析和测试用例验证,这个改动是正确的:

  • 在输入时触发 source: 'typing'
  • 在失焦时触发 source: 'blur'
  • 在其他情况下触发 source: 'effect'source: 'submit'

这个修改完全符合 PR 的目标,能够正确处理重新获得焦点时的搜索行为,避免不必要的建议请求。测试用例也验证了这些行为。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查代码库中是否存在其他使用 onSearch 的地方
ast-grep --pattern 'onSearch($_, { source: $_ })'

Length of output: 571


Script:

#!/bin/bash
# 检查 onSearch 的定义和类型
ast-grep --pattern 'onSearch?: $_'

# 检查相关的测试用例
rg "onSearch.*source" -A 3 -B 3 "test|spec|__tests__"

Length of output: 152


Script:

#!/bin/bash
# 检查组件中与搜索相关的状态和处理逻辑
rg "onSearch|showSearch|searchValue" -A 2 -B 2 src/BaseSelect/index.tsx

# 查找测试文件
fd "test|spec" -e tsx -e ts

Length of output: 3571


Script:

#!/bin/bash
# 检查测试文件中与搜索事件源相关的测试用例
rg "onSearch.*source|showSearch.*blur" -A 3 -B 3 tests/

# 特别关注与 blur 事件相关的测试
rg "test.*blur" -A 5 -B 2 tests/

Length of output: 1397

tests/BaseSelect.test.tsx Outdated Show resolved Hide resolved
tests/Select.test.tsx Outdated Show resolved Hide resolved
src/BaseSelect/index.tsx Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.31%. Comparing base (fd1f076) to head (4da3e29).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1085   +/-   ##
=======================================
  Coverage   98.31%   98.31%           
=======================================
  Files          39       39           
  Lines        1487     1487           
  Branches      443      443           
=======================================
  Hits         1462     1462           
  Misses         25       25           

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

tests/BaseSelect.test.tsx Outdated Show resolved Hide resolved
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: 1

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

7-9: 建议优化模拟组件的实现

当前的 OptionList 模拟实现过于简单,建议添加必要的 props 类型检查和基本交互功能,以便更好地模拟真实场景。

 const OptionList = forwardRef<RefOptionListProps, OptionListProps>(() => (
-  <div className="popup">Popup</div>
+  <div className="popup" role="listbox">
+    <div role="option">Option 1</div>
+    <div role="option">Option 2</div>
+  </div>
 ));
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 53fef45 and 4402aa5.

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

25-26: 测试用例逻辑优化得当!

通过引入 isMultiple 变量来同时处理 'multiple' 和 'tags' 模式的测试场景,使测试用例更加完整和准确。这个改动与 PR 的目标保持一致,有效验证了不同模式下的搜索行为。

tests/Blur.test.tsx (2)

11-32: 测试结构设计合理!

该测试文件很好地验证了 Select 组件在失焦时的搜索行为,特别是对于默认模式下的情况。测试用例清晰地验证了 onSearch 在输入和失焦时的不同行为。


80-102: 测试用例完整性良好!

该测试用例很好地验证了点击选项后的失焦行为,确保了:

  1. 下拉框正确隐藏
  2. onSearch 只被调用一次
  3. onBlur 事件被正确触发

这与 PR 的目标完全一致,有效验证了修复后的行为。

tests/Blur.test.tsx Show resolved Hide resolved
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.

【Select】select组件的showSearch功能在mode是否为multiple时下拉选项表现不一致
3 participants