Skip to content

Commit

Permalink
fix(check-cascader): 修复checkedMode="PARENT"模式下搜索后子节点无法勾选的问题 (#2993)
Browse files Browse the repository at this point in the history
* fix(check-cascader): 优化搜索算法

* fix(check-cascader): 修复 checkedMode="PARENT"模式下搜索后子节点无法选中问题 (#2992)
  • Loading branch information
zyprepare authored Sep 13, 2024
1 parent 6eb73ce commit 102d01a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-berries-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/check-cascader": patch
---

fix: 优化搜索算法
5 changes: 5 additions & 0 deletions .changeset/grumpy-tips-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/check-cascader": patch
---

fix: 修复 checkedMode="PARENT"模式下搜索后子节点无法选中问题
5 changes: 5 additions & 0 deletions .changeset/lovely-suns-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(check-cascader): 优化搜索算法
5 changes: 5 additions & 0 deletions .changeset/silver-bulldogs-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(check-cascader): 修复 checkedMode="PARENT"模式下搜索后子节点无法选中问题
1 change: 1 addition & 0 deletions packages/ui/check-cascader/src/CheckCascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp
flatted={flatted || !!searchValue}
// @ts-ignore
flattedData={selectProps.data}
originalFlattedData={flattedData}
data={cascaderData}
onChangeData={setCascaderData}
checkedMode={checkedMode}
Expand Down
8 changes: 7 additions & 1 deletion packages/ui/check-cascader/src/CheckCascaderMenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const CheckCascaderMenuList = forwardRef<HTMLDivElement | null, CascaderM
children,
data,
flattedData,
originalFlattedData,
onChangeData,
value: valueProp,
defaultValue = NOOP_ARRAY,
Expand Down Expand Up @@ -59,7 +60,7 @@ export const CheckCascaderMenuList = forwardRef<HTMLDivElement | null, CascaderM
const [onOptionCheck, isCheckedId, isSemiCheckedId] = useCheck(
checkedMode,
disabled,
flattedData,
originalFlattedData,
defaultValue,
valueProp,
({ checkedIds, semiCheckedIds }, target, shouldChecked) => {
Expand Down Expand Up @@ -164,6 +165,11 @@ export interface CascaderMenusProps {
* 设置选择项数据源
*/
flattedData: FlattedCheckCascaderDataItem[]
/**
* 未被过滤的选择项数据源,Hotfix:https://github.com/XiaoMi/hiui/issues/2992
* @private
*/
originalFlattedData: FlattedCheckCascaderDataItem[]
/**
* 设置当前多选值
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/check-cascader/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ export const processCheckedIds = (
allowCheck: (node: any) => boolean
) => {
const keySet = new Set(checkedIds)
const flattenDataMap = new Map(flattenData.map((node: any) => [node.id, node]))

switch (type) {
case 'CHILD':
return checkedIds.filter((id) => {
const node = fFindNodeById(flattenData, id)
const node = flattenDataMap.get(id) as any

if (node) {
const { children } = node
Expand All @@ -144,7 +145,8 @@ export const processCheckedIds = (

case 'PARENT':
return checkedIds.filter((id) => {
const node = fFindNodeById(flattenData, id) as any
const node = flattenDataMap.get(id) as any

if (node) {
// 向上递归遍历是否被勾选
const ancestors = getNodeAncestors(node)
Expand Down

0 comments on commit 102d01a

Please sign in to comment.