Skip to content
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(comp: overlay): first vnode is comment node #1116

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cdk/utils/__tests__/vNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe('vNode.ts', () => {
expect(getFirstValidNode(arrayValue as VNodeChild)).toBeUndefined()
expect(getFirstValidNode(vNode as VNodeChild)).toBeUndefined()

vNode.type = Comment
const textvNode: FakeVNode = {
type: Text,
}
const startCommentNodeArrayValue: FakeVNode[] = [vNode, textvNode]
expect(getFirstValidNode(startCommentNodeArrayValue as VNodeChild, 0)).toEqual(textvNode)
expect(getFirstValidNode(vNode as VNodeChild, 0)).toBeUndefined()
expect(getFirstValidNode(startCommentNodeArrayValue as VNodeChild, 1)).toEqual(textvNode)
expect(getFirstValidNode(vNode as VNodeChild, 1)).toBeUndefined()

vNode.type = Fragment
expect(getFirstValidNode(arrayValue as VNodeChild, 0)).toBeUndefined()
expect(getFirstValidNode(vNode as VNodeChild, 0)).toBeUndefined()
Expand Down
9 changes: 5 additions & 4 deletions packages/cdk/utils/src/vNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ function getChildren(node: VNode, depth: number): VNode | undefined {
* @param maxDepth depth to be searched, default is 3
*/
export function getFirstValidNode(nodes: VNodeChild, maxDepth = 3): VNode | undefined {
if (isNil(nodes) || (Array.isArray(nodes) && !nodes.length)) {
const targetNodes = Array.isArray(nodes) ? nodes.filter(item => !isComment(item)) : nodes
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const targetNodes = Array.isArray(nodes) ? nodes.filter(item => !isComment(item)) : nodes
const targetNodes = Array.isArray(nodes) ? nodes.filter(item => !getChildren(item, maxDepth)) : nodes

这里同样需要过滤掉其他的节点。

Copy link
Member

Choose a reason for hiding this comment

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

我先合并吧,这里我需要重构一下。

if (isNil(targetNodes) || (Array.isArray(targetNodes) && !targetNodes.length)) {
return
}
if (Array.isArray(nodes) && nodes.length > 0) {
return getChildren(nodes[0] as VNode, maxDepth)
if (Array.isArray(targetNodes) && targetNodes.length > 0) {
return getChildren(targetNodes[0] as VNode, maxDepth)
}
return getChildren(nodes as VNode, maxDepth)
return getChildren(targetNodes as VNode, maxDepth)
}

/**
Expand Down