Skip to content

feat:Using the showScrollBar property of the List component #1122

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 2 commits into from
Feb 8, 2025

Conversation

EmilyyyLiu
Copy link
Contributor

@EmilyyyLiu EmilyyyLiu commented Feb 8, 2025

相关链接:ant-design/ant-design#33915

Summary by CodeRabbit

  • 新特性
    • 选择组件现支持配置滚动条显示,通过新增属性实现灵活控制(支持布尔值与"optional"选项)。
  • 测试
    • 增加了自动化测试用例,确保滚动条在不同配置下能够正确显示或隐藏。

Copy link

vercel bot commented Feb 8, 2025

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

Name Status Preview Comments Updated (UTC)
select ❌ Failed (Inspect) Feb 8, 2025 6:15am

Copy link
Contributor

coderabbitai bot commented Feb 8, 2025

Walkthrough

本次更改在 BaseSelect 和 OptionList 组件中增加了新的可选属性 showScrollBar,该属性支持 boolean 和字符串 "optional" 两种类型,用于控制组件内部滚动条的显示。该属性通过 BaseSelectContext 传递给子组件,并在 OptionList 中传递给 List 组件。同时,OptionList 的属性定义由接口调整为 Record<string, never>,并新增了相关测试用例,验证不同情况下滚动条的显示逻辑。

Changes

文件 修改摘要
src/BaseSelect/index.tsx
src/OptionList.tsx
新增 showScrollBar 属性(类型:boolean 或 "optional"),在 BaseSelect 中更新上下文并在 OptionList 中传递给 List 组件;同时调整 OptionListProps 的类型为 Record<string, never>。
tests/OptionList.test.tsx 引入 waitFor 函数,新增名为 List.ScrollBar 的测试套件,包含对 showScrollBar 为 true 和 false 时滚动条显示行为的测试。

Sequence Diagram(s)

sequenceDiagram
    participant 用户 as User
    participant BaseSelect
    participant Context as BaseSelectContext
    participant OptionList
    participant List

    用户->>BaseSelect: 传入 showScrollBar 属性
    BaseSelect->>Context: 在上下文中注入 showScrollBar
    Context-->>OptionList: 传递 showScrollBar 属性
    OptionList->>List: 将 showScrollBar 属性传递给 List
    List-->>用户: 渲染组件(带或不带滚动条)
Loading

Possibly related PRs

Suggested reviewers

  • zombieJ

Poem

我是小兔跳跃代码间,
showScrollBar 新添进组件边;
上下文传递如风轻,
测试验证步步稳,
代码花园满春天 🐇✨!

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/OptionList.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+eslintrc@2.1.4/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/eslint@8.57.1/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/eslint@8.57.1/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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

codecov bot commented Feb 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.26%. Comparing base (3ff731d) to head (f993af8).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1122   +/-   ##
=======================================
  Coverage   98.26%   98.26%           
=======================================
  Files          39       39           
  Lines        1498     1499    +1     
  Branches      451      452    +1     
=======================================
+ Hits         1472     1473    +1     
  Misses         26       26           

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

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

🧹 Nitpick comments (3)
tests/ListScrollBar.test.tsx (2)

8-25: 虚拟列表的模拟实现可以更完善。

当前的虚拟列表模拟主要关注 scrollTo 方法,建议增加对其他重要属性和方法的模拟。

建议添加以下增强:

 return OriReact.forwardRef((props, ref) => {
   const oriRef = OriReact.useRef();
+  // 添加更多虚拟列表的核心功能模拟
+  const mockListRef = {
+    scrollHeight: 1000,
+    clientHeight: 200,
+    scrollTop: 0,
+    ...oriRef.current,
+  };

   OriReact.useImperativeHandle(ref, () => ({
-    ...oriRef.current,
+    ...mockListRef,
     scrollTo: (arg) => {
       global.scrollToArgs = arg;
-      oriRef.current.scrollTo(arg);
+      mockListRef.scrollTo(arg);
     },
   }));

69-81: 测试用例需要覆盖更多场景。

当前测试用例仅验证了基本的显示和隐藏场景,建议增加更多边界情况的测试。

建议添加以下测试场景:

  1. 测试 showScrollBar='optional' 的情况
  2. 测试列表项数量少于可视区域时的滚动条行为
  3. 测试动态切换 showScrollBar 属性的情况
  4. 测试不同设备(移动端/桌面端)的滚动条表现

Also applies to: 82-94

src/BaseSelect/index.tsx (1)

139-139: 属性定义需要添加文档注释。

showScrollBar 属性缺少 JSDoc 注释来说明其用途和可选值的含义。

建议添加如下文档注释:

+/**
+ * 控制下拉列表滚动条的显示方式
+ * @default 'optional'
+ */
 showScrollBar?: boolean | 'optional';
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3ff731d and a7801a8.

📒 Files selected for processing (4)
  • src/BaseSelect/index.tsx (3 hunks)
  • src/OptionList.tsx (2 hunks)
  • tests/ListScrollBar.test.tsx (1 hunks)
  • tests/utils/domHook.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
tests/utils/domHook.ts

[warning] 23-23: tests/utils/domHook.ts#L23
Added line #L23 was not covered by tests


[warning] 25-25: tests/utils/domHook.ts#L25
Added line #L25 was not covered by tests


[warning] 27-27: tests/utils/domHook.ts#L27
Added line #L27 was not covered by tests


[warning] 33-33: tests/utils/domHook.ts#L33
Added line #L33 was not covered by tests


[warning] 47-47: tests/utils/domHook.ts#L47
Added line #L47 was not covered by tests


[warning] 56-57: tests/utils/domHook.ts#L56-L57
Added lines #L56 - L57 were not covered by tests

🔇 Additional comments (6)
tests/utils/domHook.ts (2)

4-54: 代码实现完善,但需要增加测试覆盖率。

spyElementPrototypes 函数的实现逻辑清晰,但有几个关键路径缺少测试覆盖:

  • 属性的 set 方法调用
  • 原始属性的 get 方法调用
  • 函数类型属性的恢复

建议添加以下场景的测试用例:

  1. 测试属性的 set/get 方法
  2. 测试函数类型属性的 spy 和恢复
  3. 测试 NO_EXIST 情况的处理
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 23-23: tests/utils/domHook.ts#L23
Added line #L23 was not covered by tests


[warning] 25-25: tests/utils/domHook.ts#L25
Added line #L25 was not covered by tests


[warning] 27-27: tests/utils/domHook.ts#L27
Added line #L27 was not covered by tests


[warning] 33-33: tests/utils/domHook.ts#L33
Added line #L33 was not covered by tests


[warning] 47-47: tests/utils/domHook.ts#L47
Added line #L47 was not covered by tests


56-60: 实用的便捷函数封装。

spyElementPrototype 函数提供了一个简洁的接口来监视单个属性,这是一个很好的抽象。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 56-57: tests/utils/domHook.ts#L56-L57
Added lines #L56 - L57 were not covered by tests

src/OptionList.tsx (2)

19-20: 类型定义的改变可能影响向后兼容性。

OptionListProps 从泛型接口改为 Record<string, never> 可能会影响现有代码。

请确认这个改动不会影响现有的使用场景。如果有使用这个接口的代码,需要相应更新。


47-47: 正确传递了 showScrollBar 属性。

useBaseProps 中解构并传递 showScrollBar 属性到 List 组件的实现是正确的。

Also applies to: 329-329

src/BaseSelect/index.tsx (2)

226-226: 默认值设置合理。

showScrollBar 的默认值设置为 'optional' 是一个合理的选择,这样可以在需要时显示滚动条。


697-709: 上下文依赖项更新完整。

正确地将 showScrollBar 添加到了上下文值和依赖数组中。

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

🧹 Nitpick comments (3)
tests/OptionList.test.tsx (3)

9-11: 导入语句的顺序需要调整

建议将相关的导入语句进行分组和排序:

  1. 外部依赖
  2. 内部组件
  3. 工具函数
-import { createEvent, fireEvent, render, waitFor } from '@testing-library/react';
-import Select from '../src';
-import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
+import { createEvent, fireEvent, render, waitFor } from '@testing-library/react';
+import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
+import Select from '../src';

507-519: 建议增强滚动条显示测试用例

当前测试用例可以进一步完善:

  1. 测试不同的滚动条状态切换
  2. 验证滚动条样式属性
 it('should show scrollbar when showScrollBar is true', async () => {
   const options = Array.from({ length: 10 }, (_, index) => ({
     label: `${index + 1}`,
     value: `${index + 1}`,
   }));

-  const { container } = render(<Select open showScrollBar options={options} />);
+  const { container, rerender } = render(<Select open showScrollBar options={options} />);

   await waitFor(() => {
     const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
     expect(scrollbarElement).not.toBeNull();
+    expect(scrollbarElement).toHaveStyle({ display: 'block' });
   });
+
+  // 测试状态切换
+  rerender(<Select open showScrollBar={false} options={options} />);
+  await waitFor(() => {
+    const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
+    expect(scrollbarElement).toBeNull();
+  });
 });

520-532: 建议补充边界情况测试

当前测试用例可以添加以下场景:

  1. 选项数量少于显示区域时的滚动条行为
  2. 测试 showScrollBar="optional" 的情况
 it('not have scrollbar when showScrollBar is false', async () => {
   const options = Array.from({ length: 10 }, (_, index) => ({
     label: `${index + 1}`,
     value: `${index + 1}`,
   }));

   const { container } = render(<Select open showScrollBar={false} options={options} />);

   await waitFor(() => {
     const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
     expect(scrollbarElement).toBeNull();
   });
 });
+
+it('should handle optional scrollbar correctly', async () => {
+  // 测试选项数量少时不显示滚动条
+  const fewOptions = Array.from({ length: 3 }, (_, index) => ({
+    label: `${index + 1}`,
+    value: `${index + 1}`,
+  }));
+
+  const { container, rerender } = render(
+    <Select open showScrollBar="optional" options={fewOptions} />
+  );
+
+  await waitFor(() => {
+    const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
+    expect(scrollbarElement).toBeNull();
+  });
+
+  // 测试选项数量多时显示滚动条
+  const manyOptions = Array.from({ length: 20 }, (_, index) => ({
+    label: `${index + 1}`,
+    value: `${index + 1}`,
+  }));
+
+  rerender(<Select open showScrollBar="optional" options={manyOptions} />);
+
+  await waitFor(() => {
+    const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
+    expect(scrollbarElement).not.toBeNull();
+  });
+});
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a7801a8 and f993af8.

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

465-506: 测试套件的设置逻辑清晰且完整

测试套件中的 mock 设置和清理逻辑完整,包括:

  • 模拟 HTMLElement 的必要属性
  • 正确的生命周期钩子使用
  • 合理的默认值设置

@zombieJ zombieJ merged commit 14824f5 into react-component:master Feb 8, 2025
10 of 11 checks passed
@EmilyyyLiu EmilyyyLiu deleted the liuh-0106 branch February 18, 2025 03:34
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