Skip to content

Commit

Permalink
fix(tree): fix tree throw error when use pattern prop filter the tree…
Browse files Browse the repository at this point in the history
… node, close tusen-ai#2960
  • Loading branch information
jizai1125 committed May 18, 2022
1 parent 8465cab commit 2d2f439
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Fixes

- Fix `n-tree` throw error when use `pattern` prop filter the tree node, closes [#2960].

## 2.29.0

### Breaking Changes
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Fixes

- 修复 `n-tree` 使用 `pattern` 属性过滤树节点时报错, 关闭 [#2960].

## 2.29.0

### Breaking Changes
Expand Down
5 changes: 3 additions & 2 deletions src/tree/demos/enUS/filter.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Tree accept `pattern` and `filter` to do searching.
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { defineComponent, h, ref } from 'vue'
import { TreeOption } from 'naive-ui'
const data: TreeOption[] = [
Expand All @@ -52,7 +52,8 @@ const data: TreeOption[] = [
]
},
{
label: '1',
// label: '1',
prefix: () => h('text', 'prefix'),
key: '1',
children: [
{
Expand Down
5 changes: 3 additions & 2 deletions src/tree/demos/zhCN/filter.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { defineComponent, h, ref } from 'vue'
import { TreeOption } from 'naive-ui'
const data: TreeOption[] = [
Expand All @@ -52,7 +52,8 @@ const data: TreeOption[] = [
]
},
{
label: '1',
// label: '1',
prefix: () => h('text', 'prefix'),
key: '1',
children: [
{
Expand Down
4 changes: 2 additions & 2 deletions src/tree/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ export default defineComponent({
const { labelField } = props
return (pattern: string, node: TreeOption): boolean => {
if (!pattern.length) return true
return ((node as any)[labelField] as string)
.toLowerCase()
return (node[labelField] as string)

This comment has been minimized.

Copy link
@07akioni

07akioni May 18, 2022

const label = node[labelField]
if (typeof label === 'string') {
   xxx
}
return false

This comment has been minimized.

Copy link
@jizai1125

jizai1125 May 18, 2022

Author Owner
 const label = node[labelField]
 if (typeof label === 'string') {
    return label.toLowerCase().includes(pattern.toLowerCase())
 }
 return false

这样嘛

?.toLowerCase()
.includes(pattern.toLowerCase())
}
})
Expand Down

2 comments on commit 2d2f439

@clabnet
Copy link

@clabnet clabnet commented on 2d2f439 May 18, 2022

Choose a reason for hiding this comment

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

Hi @jizai1125 @07akioni, thank's for your work.
Can I try the change ? How to ?

@jizai1125
Copy link
Owner Author

@jizai1125 jizai1125 commented on 2d2f439 May 18, 2022

Choose a reason for hiding this comment

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

@clabnet you can set label prop to empty string, like { label: '', ... }.

Please # to comment.