-
Notifications
You must be signed in to change notification settings - Fork 272
Find by component #66
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b7fae3c
feat: basic searching for nested component instances
dobromir-hristov 4988539
refactor: refactor findComponent and findAllComponents
dobromir-hristov 212ca20
feat: return ComponentPublicInstance from findComponent
dobromir-hristov a8269a6
refactor: update find matching methods
dobromir-hristov 92de550
tests: improve findComponent tests
dobromir-hristov 1f31a51
refactor: Refactor the way VueWrapper is created, to allow for wrappi…
dobromir-hristov bf9cde8
refactor: cleanup ts ignore
dobromir-hristov 07a203b
chore: code review and type improvements
dobromir-hristov 1bb0884
Merge branch 'master' into feat/find-by-component
dobromir-hristov 7052f2b
chore: resolve merge
dobromir-hristov fd242b8
chore: add docs
dobromir-hristov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const MOUNT_ELEMENT_ID = 'app' | ||
export const MOUNT_COMPONENT_REF = 'VTU_COMPONENT' | ||
export const MOUNT_PARENT_NAME = 'VTU_ROOT' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { VNode, ComponentPublicInstance } from 'vue' | ||
import { FindAllComponentsSelector } from '../types' | ||
import { matchName } from './matchName' | ||
|
||
/** | ||
* Detect whether a selector matches a VNode | ||
* @param node | ||
* @param selector | ||
* @return {boolean | ((value: any) => boolean)} | ||
*/ | ||
function matches(node: VNode, selector: FindAllComponentsSelector): boolean { | ||
// do not return none Vue components | ||
if (!node.component) return false | ||
|
||
if (typeof selector === 'string') { | ||
return node.el?.matches?.(selector) | ||
} | ||
|
||
if (typeof selector === 'object' && typeof node.type === 'object') { | ||
if (selector.name && ('name' in node.type || 'displayName' in node.type)) { | ||
dobromir-hristov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// match normal component definitions or functional components | ||
return matchName(selector.name, node.type.name || node.type.displayName) | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* Collect all children | ||
* @param nodes | ||
* @param children | ||
*/ | ||
function aggregateChildren(nodes, children) { | ||
if (children && Array.isArray(children)) { | ||
;[...children].reverse().forEach((n: VNode) => { | ||
nodes.unshift(n) | ||
}) | ||
} | ||
} | ||
|
||
function findAllVNodes(vnode: VNode, selector: any): VNode[] { | ||
const matchingNodes = [] | ||
const nodes = [vnode] | ||
while (nodes.length) { | ||
const node = nodes.shift() | ||
aggregateChildren(nodes, node.children) | ||
aggregateChildren(nodes, node.component?.subTree.children) | ||
if (matches(node, selector)) { | ||
matchingNodes.push(node) | ||
} | ||
} | ||
|
||
return matchingNodes | ||
} | ||
|
||
export function find(root: VNode, selector: any): ComponentPublicInstance[] { | ||
return findAllVNodes(root, selector).map( | ||
(vnode: VNode) => vnode.component.proxy | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { camelize, capitalize } from '@vue/shared' | ||
|
||
dobromir-hristov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export function matchName(target, sourceName) { | ||
const camelized = camelize(target) | ||
const capitalized = capitalize(camelized) | ||
|
||
return ( | ||
sourceName && | ||
(sourceName === target || | ||
sourceName === camelized || | ||
sourceName === capitalized || | ||
capitalize(camelize(sourceName)) === capitalized) | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.