-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat: Merge searchValue,autoClearSearchValue and onSearch into the showSearch field #593
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
feat: Merge searchValue,autoClearSearchValue and onSearch into the showSearch field #593
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
## Walkthrough
本次变更将搜索相关的状态和回调集中到 `showSearch` 配置对象中。新增了 `searchValue`、`onSearch` 和 `autoClearSearchValue` 可选属性到 `ShowSearchType`(现重命名为 `SearchConfig`)接口,并在组件内部优先使用 `showSearch` 内的配置。原有的顶层 `searchValue`、`onSearch` 和 `autoClearSearchValue` 属性已标记为废弃,保留兼容。测试用例新增了对新用法的验证,文档重构了 `showSearch` 相关说明。
## Changes
| 文件/路径 | 变更摘要 |
|-------------------------------|-----------------------------------------------------------------------------------------------|
| src/Cascader.tsx | `ShowSearchType` 重命名为 `SearchConfig`,新增 `searchValue`、`onSearch`、`autoClearSearchValue` 属性;顶层同名属性标记废弃;组件内部通过 `useSearchConfig` 统一管理搜索配置。 |
| src/hooks/useSearchConfig.ts | `useSearchConfig` 接口新增 `props` 参数,合并顶层废弃属性到返回的搜索配置中,类型从 `ShowSearchType` 更新为 `SearchConfig`。 |
| src/hooks/useSearchOptions.ts | 类型从 `ShowSearchType` 更新为 `SearchConfig`,无逻辑变更。 |
| src/index.tsx | 导出类型由 `ShowSearchType` 改为 `SearchConfig`。 |
| tests/search.spec.tsx | 新增三条测试用例,验证 `showSearch` 下的 `onSearch` 回调、`searchValue` 绑定和 `autoClearSearchValue` 行为;修正部分测试中缺少的 `showSearch` 属性。 |
| README.md | 重构文档,将 `autoClearSearchValue` 从主表移除,整合到 `showSearch` 详细子属性说明中,新增 `showSearch` 属性描述和子属性说明。 |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant User
participant Cascader
participant useSearchConfig
User->>Cascader: 输入搜索内容
Cascader->>useSearchConfig: 获取合并后的搜索配置(showSearch + 顶层废弃属性)
useSearchConfig-->>Cascader: 返回搜索配置对象
Cascader->>Cascader: 根据配置更新搜索状态,调用 onSearch 回调
Cascader-->>User: 展示搜索结果 Possibly related PRs
Poem
|
README 里加一下。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/hooks/useSearchConfig.ts (1)
28-34
:(searchConfig.limit as number)
的类型断言过于粗暴
limit
允许为false
,此处直接断言为number
再比较会把false
强制转换为0
,逻辑虽能工作,但易读性与类型安全均受损。可先判断类型再比较,避免断言。-if ((searchConfig.limit as number) <= 0) { +if (typeof searchConfig.limit === 'number' && searchConfig.limit <= 0) {这样更符合预期语义,也无需类型断言。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
README.md
(1 hunks)src/Cascader.tsx
(4 hunks)src/hooks/useSearchConfig.ts
(2 hunks)src/hooks/useSearchOptions.ts
(1 hunks)src/index.tsx
(1 hunks)tests/search.spec.tsx
(2 hunks)
✅ Files skipped from review due to trivial changes (2)
- src/index.tsx
- src/hooks/useSearchOptions.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- tests/search.spec.tsx
- src/Cascader.tsx
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/hooks/useSearchConfig.ts (2)
src/index.tsx (2)
CascaderProps
(7-7)SearchConfig
(9-9)src/Cascader.tsx (2)
CascaderProps
(157-175)SearchConfig
(43-69)
🪛 markdownlint-cli2 (0.17.2)
README.md
244-244: Table column count
Expected: 5; Actual: 4; Too few cells, row will be missing data
(MD056, table-column-count)
250-250: Table column count
Expected: 5; Actual: 4; Too few cells, row will be missing data
(MD056, table-column-count)
251-251: Table column count
Expected: 5; Actual: 4; Too few cells, row will be missing data
(MD056, table-column-count)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: WIP
🔇 Additional comments (1)
src/hooks/useSearchConfig.ts (1)
6-6
: 避免使用any
,加强类型安全
props?: any
失去 TypeScript 带来的约束价值。可使用Partial<Pick<CascaderProps, ...>>
精确声明,只暴露当前 hook 真实依赖的字段,既防止误用又提升 IDE 提示。
[ suggest_optional_refactor ]
export default function useSearchConfig(showSearch?: CascaderProps['showSearch'], props?: any) { | ||
const { autoClearSearchValue, searchValue, onSearch } = props; | ||
return React.useMemo<[boolean, SearchConfig]>(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
props 解构存在潜在运行时异常
当 props
为空或未传入时,const { autoClearSearchValue, searchValue, onSearch } = props;
会直接对 undefined
进行解构,运行时将抛出 TypeError: Cannot destructure property ... of 'undefined' or 'null'
.
建议在形参或解构处增加默认值,避免意外崩溃。
-export default function useSearchConfig(showSearch?: CascaderProps['showSearch'], props?: any) {
- const { autoClearSearchValue, searchValue, onSearch } = props;
+export default function useSearchConfig(
+ showSearch?: CascaderProps['showSearch'],
+ props: Partial<Pick<CascaderProps, 'autoClearSearchValue' | 'searchValue' | 'onSearch'>> = {},
+) {
+ const { autoClearSearchValue, searchValue, onSearch } = props;
这样既提供了类型约束,也为缺省情况兜底。
📝 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.
export default function useSearchConfig(showSearch?: CascaderProps['showSearch'], props?: any) { | |
const { autoClearSearchValue, searchValue, onSearch } = props; | |
return React.useMemo<[boolean, SearchConfig]>(() => { | |
export default function useSearchConfig( | |
showSearch?: CascaderProps['showSearch'], | |
props: Partial<Pick<CascaderProps, 'autoClearSearchValue' | 'searchValue' | 'onSearch'>> = {}, | |
) { | |
const { autoClearSearchValue, searchValue, onSearch } = props; | |
return React.useMemo<[boolean, SearchConfig]>(() => { | |
// …rest of implementation | |
}); | |
} |
🤖 Prompt for AI Agents
In src/hooks/useSearchConfig.ts around lines 6 to 8, the destructuring of props
without a default value can cause a runtime TypeError if props is undefined or
null. To fix this, provide a default empty object for props either in the
function parameter (e.g., props = {}) or during destructuring to ensure safe
access to its properties and prevent crashes.
README.md
Outdated
<tr> | ||
<td>showSearch</td> | ||
<td>Boolean | Object</td> | ||
<td>>false</td> | ||
<td>hide popup on select</td> | ||
</tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
showSearch
行描述与含义不符且默认值箭头错误
- 描述 “hide popup on select” 与搜索功能毫无关联,应改为 “启用搜索或自定义搜索配置”。
>
字符应去掉;表格默认值应直接写false
。
-<td>showSearch</td>
-<td>Boolean | Object</td>
-<td>>false</td>
-<td>hide popup on select</td>
+<td>showSearch</td>
+<td>boolean | object</td>
+<td>false</td>
+<td>启用搜索功能或传入搜索配置对象</td>
📝 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.
<tr> | |
<td>showSearch</td> | |
<td>Boolean | Object</td> | |
<td>>false</td> | |
<td>hide popup on select</td> | |
</tr> | |
<tr> | |
<td>showSearch</td> | |
<td>boolean | object</td> | |
<td>false</td> | |
<td>启用搜索功能或传入搜索配置对象</td> | |
</tr> |
🤖 Prompt for AI Agents
In README.md around lines 231 to 236, the description for the showSearch
property is incorrect and the default value formatting is wrong. Change the
description from "hide popup on select" to "启用搜索或自定义搜索配置" to accurately reflect
its purpose. Also, remove the '>' character before false in the default value
column so it simply shows "false".
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #593 +/- ##
=======================================
Coverage 99.83% 99.83%
=======================================
Files 23 23
Lines 611 612 +1
Branches 176 176
=======================================
+ Hits 610 611 +1
Misses 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ant-design/ant-design#33482
Summary by CodeRabbit
showSearch
对象集中设置搜索值、搜索回调及自动清除搜索值功能,且默认自动清除搜索值为开启状态。showSearch
配置搜索值、回调及自动清除搜索值行为的正确性,包括搜索结果展示和选中后搜索值保留。searchValue
、onSearch
和autoClearSearchValue
属性已标记为弃用,建议改用showSearch
对象中的对应配置。showSearch
相关属性的文档说明,新增详细子属性说明,明确autoClearSearchValue
仅在可勾选时生效。