-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow find component without name in script setup by name (#1171)
- Loading branch information
1 parent
a46b1c4
commit 36c3d4f
Showing
5 changed files
with
118 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
import { ComponentInternalInstance } from '@vue/runtime-core' | ||
import { VNodeTypes } from 'vue' | ||
import { isFunctionalComponent, isObjectComponent } from '../utils' | ||
import { | ||
isLegacyExtendedComponent, | ||
unwrapLegacyVueExtendComponent | ||
} from './vueCompatSupport' | ||
|
||
const getComponentNameInSetup = ( | ||
instance: any | null, | ||
type: VNodeTypes | ||
): string | undefined => | ||
Object.keys(instance?.setupState || {}).find( | ||
(key) => instance.setupState[key] === type | ||
) | ||
|
||
export const getComponentRegisteredName = ( | ||
instance: ComponentInternalInstance | null, | ||
type: VNodeTypes | ||
): string | null => { | ||
if (!instance || !instance.parent) return null | ||
|
||
// try to infer the name based on local resolution | ||
const registry = (instance.type as any).components | ||
for (const key in registry) { | ||
if (registry[key] === type) { | ||
return key | ||
} | ||
} | ||
|
||
// try to retrieve name imported in script setup | ||
return getComponentNameInSetup(instance.parent, type) || null | ||
} | ||
|
||
export const getComponentName = ( | ||
instance: any | null, | ||
type: VNodeTypes | ||
): string => { | ||
if (isObjectComponent(type)) { | ||
return getComponentNameInSetup(instance, type) || type.name || '' | ||
} | ||
|
||
if (isLegacyExtendedComponent(type)) { | ||
return unwrapLegacyVueExtendComponent(type).name || '' | ||
} | ||
|
||
if (isFunctionalComponent(type)) { | ||
return type.displayName || type.name | ||
} | ||
|
||
return '' | ||
} |
This file contains 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 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 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