diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index fef316aa2..725e0f674 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -5,6 +5,7 @@ ## Fixes - Fix `n-tabs` switch tab does not work when adding a new tab, closes [#1417](https://github.com/TuSimple/naive-ui/issues/1417). +- Fix `n-tree`'s `filter` prop does not work when assigned `children-field` , closes [#1477](https://github.com/TuSimple/naive-ui/issues/1477). ## 2.20.0 (2021-10-28) diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index 3c224cf38..bcc7cdcc1 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -4,7 +4,8 @@ ## Fixes -- 修复 `n-tabs` 在新增 tab 后切换 tab 无法生效,关闭 [#1417]https://github.com/TuSimple/naive-ui/issues/1417) +- 修复 `n-tabs` 在新增 tab 后切换 tab 无法生效,关闭 [#1417](https://github.com/TuSimple/naive-ui/issues/1417) +- 修复 `n-tree` 当指定`children-field`时过滤不生效,关闭 [#1477](https://github.com/TuSimple/naive-ui/issues/1477) ## 2.20.0 (2021-10-28) diff --git a/src/tree/src/Tree.tsx b/src/tree/src/Tree.tsx index 4ed226744..bcdb43f10 100644 --- a/src/tree/src/Tree.tsx +++ b/src/tree/src/Tree.tsx @@ -374,6 +374,7 @@ export default defineComponent({ props.data, props.pattern, props.keyField, + props.childrenField, mergedFilterRef.value ) uncontrolledHighlightKeySetRef.value = highlightKeySet diff --git a/src/tree/src/utils.ts b/src/tree/src/utils.ts index be9c3a883..94fd15ac2 100644 --- a/src/tree/src/utils.ts +++ b/src/tree/src/utils.ts @@ -2,12 +2,18 @@ import { Key, TreeOption } from './interface' function traverse ( nodes: TreeOption[] | undefined, + childrenField: string, callback: (node: TreeOption) => void, callbackAfter: (node: TreeOption) => void ): void { nodes?.forEach((node) => { callback(node) - traverse(node.children, callback, callbackAfter) + traverse( + (node as any)[childrenField], + childrenField, + callback, + callbackAfter + ) callbackAfter(node) }) } @@ -16,6 +22,7 @@ export function keysWithFilter ( nodes: TreeOption[], pattern: string, keyField: string, + childrenField: string, filter: (pattern: string, node: TreeOption) => boolean ): { expandedKeys: Key[] @@ -26,6 +33,7 @@ export function keysWithFilter ( const path: TreeOption[] = [] traverse( nodes, + childrenField, (node) => { path.push(node) if (filter(pattern, node)) {