select icon indicating copy to clipboard operation
select copied to clipboard

fix: Select dropdown on search not scroll to the correct options

Open cactuser-Lu opened this issue 4 months ago • 2 comments

fix: https://github.com/ant-design/ant-design/issues/54884 related issiue: https://github.com/ant-design/ant-design/issues/53752 related PR: https://github.com/react-component/select/pull/1146

问题分析: OptionList 中:使用 startsWith + 硬编码 data.value useFilterOptions 中:使用 includes + 大小写不敏感 过滤逻辑 (useFilterOptions) 和 定位逻辑 (OptionList)应该一致

Summary by CodeRabbit

  • 新功能
    • 新增 optionFilterProp,可指定按特定字段对选项进行筛选与匹配,并通过上下文向下传递。
    • 搜索改为不区分大小写,支持将非字符串字段转换为字符串参与匹配。
    • 单选列表的自动滚动定位现会应用筛选逻辑,prop 变化时即时生效。
    • 未输入搜索词时保持原有按值匹配的行为,确保向后兼容。

cactuser-Lu avatar Sep 07 '25 10:09 cactuser-Lu

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
select Ready Ready Preview Comment Sep 7, 2025 10:46am

vercel[bot] avatar Sep 07 '25 10:09 vercel[bot]

Walkthrough

在 Select 组件链路中新增可选属性 optionFilterProp,并将其通过 SelectContext 传递到 OptionList。OptionList 更新搜索匹配逻辑:按 optionFilterProp 或 value 进行不区分大小写的包含匹配,调整单选自动滚动的匹配路径,并更新相关 effect 的依赖。

Changes

Cohort / File(s) Summary
OptionList 过滤与滚动匹配
src/OptionList.tsx
引入 toArray 与 includes 辅助,基于 optionFilterProp 或 value 的不区分大小写包含匹配;在有 searchValue 时按匹配值匹配并影响单选自动滚动;扩展 effect 依赖包含 optionFilterProp。
Select 与上下文传递
src/Select.tsx, src/SelectContext.ts, src/SelectContext.tsx
对外 API 新增 optionFilterProp?: string;Select 解构并将其注入 SelectContext;更新 context memo 依赖以在该属性变化时重新计算。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as User
  participant S as Select
  participant C as SelectContext
  participant L as OptionList

  U->>S: 输入搜索词 (onSearch)
  S->>C: 更新上下文 { searchValue, optionFilterProp }
  Note over C: Context 提供过滤配置
  C-->>L: 提供 searchValue / optionFilterProp
  rect rgba(230,245,255,0.6)
    L->>L: 计算 matchValue: data[optionFilterProp] 或 data.value
    L->>L: includes(matchValue, searchValue)(不区分大小写)
  end
  alt 单选模式
    L->>L: 根据匹配结果确定自动滚动目标
  end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
下拉选项在搜索时应置顶(#54884) 变更调整了匹配与自动滚动的依据,但未明确实现“滚动条置顶”或滚动至首项的逻辑。无法确定是否满足“置顶”的具体需求。

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
新增对外 API:optionFilterProp(src/SelectContext.ts, src/Select.tsx,多个位置) 该 API 与筛选字段选择相关,未直接对应“搜索时滚动置顶”的问题目标,可能超出本次需求范围。
OptionList 新增 includes 辅助与大小写无关匹配(src/OptionList.tsx) 匹配方式从精确/前缀逻辑转为包含逻辑,与“滚动置顶”目标无直接对齐,可能为额外行为改动。

Possibly related PRs

  • react-component/select#1157: 同样涉及 optionFilterProp,防止其泄漏到 DOM,和本次新增并传递该属性直接相关。
  • react-component/select#1146: 调整单选自动滚动匹配逻辑为前缀比较,与本次包含匹配的改动在同一代码路径上相关。
  • react-component/select#1152: 扩展并传播 optionFilterProp 的处理逻辑,与本次对上下文与 OptionList 的传播一致。

Suggested reviewers

  • afc163
  • zombieJ

Poem

兔子敲键轻轻跳,
搜索一声云中飘。
选项闻风齐靠拢,
字段一改路更巧。
滚轮欲顶等号召,
耳朵竖起等你瞧。 🐇✨

[!WARNING]

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Sep 07 '25 10:09 coderabbitai[bot]