-
Notifications
You must be signed in to change notification settings - Fork 272
chore(find): proper typings #129
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { VNode, ComponentPublicInstance } from 'vue' | ||
import { | ||
ComponentPublicInstance, | ||
VNode, | ||
VNodeArrayChildren, | ||
VNodeNormalizedChildren | ||
} from 'vue' | ||
import { FindAllComponentsSelector } from '../types' | ||
import { matchName } from './matchName' | ||
|
||
|
@@ -28,26 +33,55 @@ function matches(node: VNode, selector: FindAllComponentsSelector): boolean { | |
return false | ||
} | ||
|
||
/** | ||
* Filters out the null, undefined and primitive values, | ||
* to only keep VNode and VNodeArrayChildren values | ||
* @param value | ||
*/ | ||
function nodesAsObject<Node>( | ||
value: | ||
| string | ||
| number | ||
| boolean | ||
| VNodeArrayChildren | ||
| VNode | ||
| null | ||
| undefined | ||
| void | ||
): value is VNodeArrayChildren | VNode { | ||
return !!value && typeof value === 'object' | ||
} | ||
|
||
/** | ||
* Collect all children | ||
* @param nodes | ||
* @param children | ||
*/ | ||
function aggregateChildren(nodes, children) { | ||
function aggregateChildren(nodes: VNode[], children: VNodeNormalizedChildren) { | ||
if (children && Array.isArray(children)) { | ||
;[...children].reverse().forEach((n: VNode) => { | ||
nodes.unshift(n) | ||
const reversedNodes = [...children].reverse().filter(nodesAsObject) | ||
reversedNodes.forEach((node: VNodeArrayChildren | VNode) => { | ||
if (Array.isArray(node)) { | ||
aggregateChildren(nodes, node) | ||
} else { | ||
nodes.unshift(node) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need help here, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I was just collecting all children, then in the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I did not manage to reproduce a case where we have |
||
} | ||
}) | ||
} | ||
} | ||
|
||
function findAllVNodes(vnode: VNode, selector: any): VNode[] { | ||
const matchingNodes = [] | ||
const nodes = [vnode] | ||
function findAllVNodes( | ||
vnode: VNode, | ||
selector: FindAllComponentsSelector | ||
): VNode[] { | ||
const matchingNodes: VNode[] = [] | ||
const nodes: VNode[] = [vnode] | ||
while (nodes.length) { | ||
const node = nodes.shift() | ||
const node = nodes.shift()! | ||
aggregateChildren(nodes, node.children) | ||
aggregateChildren(nodes, node.component?.subTree.children) | ||
if (node.component) { | ||
aggregateChildren(nodes, node.component.subTree.children) | ||
} | ||
if (matches(node, selector)) { | ||
matchingNodes.push(node) | ||
} | ||
|
@@ -56,8 +90,11 @@ function findAllVNodes(vnode: VNode, selector: any): VNode[] { | |
return matchingNodes | ||
} | ||
|
||
export function find(root: VNode, selector: any): ComponentPublicInstance[] { | ||
export function find( | ||
root: VNode, | ||
selector: FindAllComponentsSelector | ||
): ComponentPublicInstance[] { | ||
return findAllVNodes(root, selector).map( | ||
(vnode: VNode) => vnode.component.proxy | ||
(vnode: VNode) => vnode.component!.proxy! | ||
) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.