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: search no scroll #532

Merged
merged 3 commits into from
Sep 3, 2024
Merged

Conversation

crazyair
Copy link
Contributor

@crazyair crazyair commented Sep 2, 2024

优化为 searchValue 有值,阻止滚动,解决搜索结果选中后,没有默认激活当前选中项

优化前

iShot_2024-09-02_18.13.05.mp4

优化后

iShot_2024-09-02_18.13.35.mp4

Summary by CodeRabbit

  • 新功能
    • 改进了组件在搜索时的滚动行为,确保在有活动搜索时不会执行滚动逻辑。
  • bug 修复
    • 简化了组件逻辑,移除了对 searchValue 的依赖,确保组件行为更直接。
  • 文档
    • 增强了对 Cascader 组件的测试覆盖,验证了其在特定用户交互下的行为。

Copy link

vercel bot commented Sep 2, 2024

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

Name Status Preview Comments Updated (UTC)
cascader ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 3, 2024 1:51am

Copy link

codecov bot commented Sep 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (d15d83b) to head (68e5182).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #532   +/-   ##
=======================================
  Coverage   99.67%   99.67%           
=======================================
  Files          23       23           
  Lines         609      611    +2     
  Branches      184      185    +1     
=======================================
+ Hits          607      609    +2     
  Misses          2        2           

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

src/OptionList/List.tsx Outdated Show resolved Hide resolved
Copy link

coderabbitai bot commented Sep 2, 2024

Walkthrough

此次更改涉及移除 ColumnProps 接口中的 searchValue 属性及其在 Column 组件中的使用。同时,triggerOpenPath 函数的逻辑也进行了调整,去除了对 searchValue 的检查,仅保留了对 disabled 状态的判断。在 RawOptionList 组件中,useEffect 钩子增加了对 searchValue 的条件检查,以避免在搜索时执行滚动逻辑,并更新了依赖数组。

Changes

文件路径 更改摘要
src/OptionList/Column.tsx 移除 ColumnProps 接口中的 searchValue 属性及其在 Column 组件中的使用,调整 triggerOpenPath 函数逻辑。
src/OptionList/List.tsx useEffect 中增加对 searchValue 的检查,更新依赖数组以包含 searchValue,并移除 Column 组件中的 searchValue 属性。
tests/index.spec.tsx Cascader.Basic 测试套件中新增测试用例,模拟悬停与搜索功能的交互,修改导入语句以包含 fireEvent

Poem

在草地上跳跃的小兔,
新变化让我们欢呼,
搜索不再打扰我,
轻松自在乐无忧。
兔子们齐声歌唱,
祝贺新逻辑真美妙! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

Tip

Early access features: enabled

We are currently testing the following features in early access:

  • Anthropic claude-3-5-sonnet for code reviews: Anthropic claims that the new Claude model has stronger code understanding and code generation capabilities than their previous models. Note: Our default code review model was also updated late last week. Please compare the quality of the reviews between the two models by toggling the early access feature.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues on the discussion post.

@afc163
Copy link
Member

afc163 commented Sep 2, 2024

用例能补一个么

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d15d83b and f25ec01.

Files selected for processing (2)
  • src/OptionList/Column.tsx (3 hunks)
  • src/OptionList/List.tsx (3 hunks)
Additional comments not posted (2)
src/OptionList/Column.tsx (1)

Line range hint 118-126: 代码变更看起来不错,但需要验证搜索功能

删除 searchValue 检查简化了 triggerOpenPath 函数的逻辑,这与修复搜索滚动问题的 PR 目标一致。

请运行以下脚本来验证这个变更对搜索功能的影响:

src/OptionList/List.tsx (1)

Line range hint 169-182: 优化搜索时的滚动行为

这个改动解决了搜索时不必要滚动的问题,符合PR的目标。

注意:这个改动与之前的评论建议相似。可以考虑采用更简洁的条件检查:

- if (searchValue) {
-   return;
- }
+ if (!searchValue) {

这样可以减少代码的缩进层级,提高可读性。

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f25ec01 and 68e5182.

Files selected for processing (1)
  • tests/index.spec.tsx (2 hunks)

Comment on lines +1025 to +1103
it('hover + search', () => {
let getOffesetTopTimes = 0;
const spyElement = spyElementPrototypes(HTMLElement, {
offsetTop: {
get: () => (getOffesetTopTimes++ % 2 === 0 ? 100 : 0),
},
scrollTop: {
get: () => 0,
},
offsetHeight: {
get: () => 10,
},
});

const wrapper = render(
<Cascader
expandTrigger="hover"
options={[
{
label: 'Women Clothing',
value: '1',
children: [
{
label: 'Women Tops, Blouses & Tee',
value: '11',
children: [
{
label: 'Women T-Shirts',
value: '111',
},
{
label: 'Women Tops',
value: '112',
},
{
label: 'Women Tank Tops & Camis',
value: '113',
},
{
label: 'Women Blouses',
value: '114',
},
],
},
{
label: 'Women Suits',
value: '2',
children: [
{
label: 'Women Suit Pants',
value: '21',
},
{
label: 'Women Suit Sets',
value: '22',
},
{
label: 'Women Blazers',
value: '23',
},
],
},
],
},
]}
showSearch
checkable
open
/>,
);
fireEvent.change(wrapper.container.querySelector('input') as HTMLElement, {
target: { value: 'w' },
});
const items = wrapper.container.querySelectorAll('.rc-cascader-menu-item');
fireEvent.mouseEnter(items[9]);
expect(mockScrollTo).toHaveBeenCalledTimes(0);

spyElement.mockRestore();
});
Copy link

@coderabbitai coderabbitai bot Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新测试用例看起来不错,但可以考虑添加更多断言。

新添加的"hover + search"测试用例很好地覆盖了悬停和搜索功能的交互。代码结构清晰,模拟了用户交互,并验证了scrollTo函数未被调用。

建议添加更多断言来增强测试的可靠性。例如,可以验证搜索结果的数量,或者检查特定元素是否存在于DOM中。这将使测试更加健壮。

 expect(mockScrollTo).toHaveBeenCalledTimes(0);

+expect(items).toHaveLength(10); // 验证搜索结果的数量
+expect(wrapper.container.querySelector('.rc-cascader-menu-item-active')).toBeTruthy(); // 检查是否有活动项
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('hover + search', () => {
let getOffesetTopTimes = 0;
const spyElement = spyElementPrototypes(HTMLElement, {
offsetTop: {
get: () => (getOffesetTopTimes++ % 2 === 0 ? 100 : 0),
},
scrollTop: {
get: () => 0,
},
offsetHeight: {
get: () => 10,
},
});
const wrapper = render(
<Cascader
expandTrigger="hover"
options={[
{
label: 'Women Clothing',
value: '1',
children: [
{
label: 'Women Tops, Blouses & Tee',
value: '11',
children: [
{
label: 'Women T-Shirts',
value: '111',
},
{
label: 'Women Tops',
value: '112',
},
{
label: 'Women Tank Tops & Camis',
value: '113',
},
{
label: 'Women Blouses',
value: '114',
},
],
},
{
label: 'Women Suits',
value: '2',
children: [
{
label: 'Women Suit Pants',
value: '21',
},
{
label: 'Women Suit Sets',
value: '22',
},
{
label: 'Women Blazers',
value: '23',
},
],
},
],
},
]}
showSearch
checkable
open
/>,
);
fireEvent.change(wrapper.container.querySelector('input') as HTMLElement, {
target: { value: 'w' },
});
const items = wrapper.container.querySelectorAll('.rc-cascader-menu-item');
fireEvent.mouseEnter(items[9]);
expect(mockScrollTo).toHaveBeenCalledTimes(0);
spyElement.mockRestore();
});
it('hover + search', () => {
let getOffesetTopTimes = 0;
const spyElement = spyElementPrototypes(HTMLElement, {
offsetTop: {
get: () => (getOffesetTopTimes++ % 2 === 0 ? 100 : 0),
},
scrollTop: {
get: () => 0,
},
offsetHeight: {
get: () => 10,
},
});
const wrapper = render(
<Cascader
expandTrigger="hover"
options={[
{
label: 'Women Clothing',
value: '1',
children: [
{
label: 'Women Tops, Blouses & Tee',
value: '11',
children: [
{
label: 'Women T-Shirts',
value: '111',
},
{
label: 'Women Tops',
value: '112',
},
{
label: 'Women Tank Tops & Camis',
value: '113',
},
{
label: 'Women Blouses',
value: '114',
},
],
},
{
label: 'Women Suits',
value: '2',
children: [
{
label: 'Women Suit Pants',
value: '21',
},
{
label: 'Women Suit Sets',
value: '22',
},
{
label: 'Women Blazers',
value: '23',
},
],
},
],
},
]}
showSearch
checkable
open
/>,
);
fireEvent.change(wrapper.container.querySelector('input') as HTMLElement, {
target: { value: 'w' },
});
const items = wrapper.container.querySelectorAll('.rc-cascader-menu-item');
fireEvent.mouseEnter(items[9]);
expect(mockScrollTo).toHaveBeenCalledTimes(0);
expect(items).toHaveLength(10); // 验证搜索结果的数量
expect(wrapper.container.querySelector('.rc-cascader-menu-item-active')).toBeTruthy(); // 检查是否有活动项
spyElement.mockRestore();
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai 不用管,只需要断言是否执行 scroll 就行。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afc163 明白了,我会保持测试用例的简洁性,只断言是否执行了 scrollTo。感谢您的指导!

@crazyair
Copy link
Contributor Author

crazyair commented Sep 3, 2024

用例能补一个么

好了

@@ -1022,6 +1022,85 @@ describe('Cascader.Basic', () => {
wrapper.find(`li[data-path-key]`).at(0).simulate('click');
wrapper.find(`li[data-path-key]`).at(1).simulate('click');
});
it('hover + search', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('hover + search', () => {
it('hover + search', () => {

@afc163 afc163 merged commit f5669c6 into react-component:master Sep 3, 2024
10 checks passed
This was referenced Oct 16, 2024
lihongxiangfrontend pushed a commit to lihongxiangfrontend/cascader that referenced this pull request Oct 25, 2024
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