Skip to content

Commit

Permalink
fix(tree): may throw error on node selection expanded-keys, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Jul 21, 2022
1 parent 1df7641 commit ea0310d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Fix `n-input` has is no indent at the prefix if `type="textarea"` and `:autosize="true"`, closes [#3238](https://github.com/TuSimple/naive-ui/issues/3238).
- Fix `n-select` focus loss when click `action` slot in `filterable` and `multiple`, closes [#3247](https://github.com/TuSimple/naive-ui/issues/3247).
- Fix `n-carousel`'s `autoplay` prop be `true` `hover` can't stop the play, closes [#3304](https://github.com/TuSimple/naive-ui/issues/3304).
- Fix `n-tree` may throw error on node selection `expanded-keys`, closes [#3319](https://github.com/TuSimple/naive-ui/issues/3319).

### Feats

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- 修复 `n-input``type="textarea"` 并且 `:autosize="true"` 时添加 prefix 后内容没有对齐,关闭 [#3238](https://github.com/TuSimple/naive-ui/issues/3238)
- 修复 `n-select` 当在同时设置 `filterable``multiple` 时点击 `action` 焦点丢失的问题,关闭 [#3247](https://github.com/TuSimple/naive-ui/issues/3247)
- 修复 `n-carousel``autoplay``true``hover` 不停止播放,关闭 [#3304](https://github.com/TuSimple/naive-ui/issues/3304)
- 修复 `n-tree` 在选中节点时设定 `expanded-keys` 时可能会抛出异常,关闭 [#3319](https://github.com/TuSimple/naive-ui/issues/3319)

### Feats

Expand Down
40 changes: 40 additions & 0 deletions src/tree/demos/zhCN/expand-debug.demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<markdown>
# Expand debug
</markdown>

<template>
<n-tree
v-model:expanded-keys="expandedKeys"
block-line
:data="data"
selectable
@update:selected-keys="onSelect"
/>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
export default defineComponent({
setup () {
const expandedKeys = ref<string[]>([])
return {
data: [
{
key: 'node-root',
label: '根节点',
children: [
{ key: 'node-1', label: '节点1' },
{ key: 'node-2', label: '节点2' }
]
}
],
expandedKeys,
onSelect (keys: string[]) {
expandedKeys.value.push(keys[0])
}
}
}
})
</script>
1 change: 1 addition & 0 deletions src/tree/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ change-debug.vue
scrollbar-debug.vue
scroll-debug.vue
rtl-debug.vue
expand-debug.vue
```

## API
Expand Down
30 changes: 15 additions & 15 deletions src/tree/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,21 @@ export default defineComponent({
(node) => (node as any).key === addedKey
)
if (~expandedNodeIndex) {
const expandedChildren = flatten(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(afNodesRef.value[expandedNodeIndex] as TmNode).children!,
value
)
afNodesRef.value.splice(expandedNodeIndex + 1, 0, {
__motion: true,
mode: 'expand',
height: virtualScroll
? expandedChildren.length * ITEM_SIZE
: undefined,
nodes: virtualScroll
? expandedChildren.slice(0, viewportItemCount)
: expandedChildren
})
const children = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(afNodesRef.value[expandedNodeIndex] as TmNode).children
if (children) {
const expandedChildren = flatten(children, value)
afNodesRef.value.splice(expandedNodeIndex + 1, 0, {
__motion: true,
mode: 'expand',
height: virtualScroll
? expandedChildren.length * ITEM_SIZE
: undefined,
nodes: virtualScroll
? expandedChildren.slice(0, viewportItemCount)
: expandedChildren
})
}
}
}
if (removedKey !== null) {
Expand Down

0 comments on commit ea0310d

Please sign in to comment.