-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix(tree): throw exception when onLoad callback does not add children #786
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tusimple/naive-ui/75BvyJzejwFMJLyzM7pBQGQgdoWw |
Codecov Report
@@ Coverage Diff @@
## main #786 +/- ##
==========================================
- Coverage 45.07% 45.06% -0.02%
==========================================
Files 509 509
Lines 12464 12467 +3
Branches 3503 3504 +1
==========================================
Hits 5618 5618
- Misses 5847 5850 +3
Partials 999 999
Continue to review full report at Codecov.
|
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.
我简单过了一下这个 PR,感觉处理的太复杂了。
从根本原因上讲,是试图去展开一个没有孩子的节点。
所以我们应该避免这个事,所以我们应该检查一下这个节点有没有 isShallowLoaded
就行了。
当然,不能用同样的 tmNode
,因为增加 children
会导致整个 props.data
改变,影响 treeMateRef
,所以需要从 treeMateRef
上面用同样的 key
来拿到新的对应节点,然后检查是不是 isShallowLoaded
。
至于 catch 的问题或许我们可以不处理(只要这个是用户自己导致的),加载出错就应该爆炸,框架兜底的话感觉不太好。
但是得给用户一个关闭loading的能力吧,不然爆炸了就一直loading只能刷新页面解决了,有可能就是个网络超时,重试一次就好了 |
resolved 的时候 loadingKey 肯定是要去掉的,但是是否 expand 最后还是要看 children 有没有。 问题在于 loading resolve 之后有没有孩子我们是不知道的,需要我们自己来检查。 去掉 loadingKey 和 expand 是两件事。 |
src/tree/src/Tree.tsx
Outdated
const toExpandedNode = displayTreeMateRef | ||
.value!.getFlattenedNodes([...mergedExpandedKeys, key]) | ||
.find((node) => (node as any).key === key) | ||
if (!toExpandedNode?.rawNode.children) { | ||
return | ||
} |
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.
nodeToBeExpanded
= displayTreeMateRef.getNode(key)
src/tree/src/TreeNode.tsx
Outdated
const removeFromLoadingKeysRef = (tmNode: TmNode): void => { | ||
NTree.loadingKeysRef.value.splice( | ||
NTree.loadingKeysRef.value.findIndex((key) => key === tmNode.key), | ||
1 | ||
) | ||
NTree.handleSwitcherClick(tmNode) | ||
}) | ||
} | ||
void onLoad(tmNode.rawNode) | ||
.then(() => { | ||
removeFromLoadingKeysRef(tmNode) | ||
NTree.handleSwitcherClick(tmNode) | ||
}) | ||
.catch((switcherClickError) => { | ||
console.error(switcherClickError) | ||
removeFromLoadingKeysRef(tmNode) | ||
}) |
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.
void onLoad(tmNode.rawNode)
.then(() => {
NTree.handleSwitcherClick(tmNode)
})
.finally(() => {
NTree.loadingKeysRef.value.splice(
NTree.loadingKeysRef.value.findIndex((key) => key === tmNode.key),
1
)
})
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.
代码尽量要简洁
…tusen-ai#786) * fix(tree): throw exception when onLoad callback does not add children * refactor(tree): not expand when node has no children * refactor(tree): handle no-children expand error * Update src/tree/src/Tree.tsx * Update src/tree/src/TreeNode.tsx Co-authored-by: 07akioni <[email protected]>
Closes #772