Skip to content

Commit

Permalink
LDP-2575: Use Default suffix and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
vloss3 committed Aug 29, 2024
1 parent ed3095f commit d6a338c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
18 changes: 14 additions & 4 deletions src/runtime/composables/useDrupalCe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,34 @@ export const useDrupalCe = () => {
* @param element The custom element name to resolve
*/
const resolveCustomElement = (element: string) => {
const nuxtApp = useNuxtApp()
const formatName = (name: string) => name.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('')

// Try resolving the full component name.
const component = resolveComponent(element)
const component = nuxtApp.vueApp.component(formatName(element))
if (typeof component === 'object' && component.name) {
return resolveComponent(element)
return component
}

// Try to lookup a fallback component name.
const regex = /-[a-z]+$/
let componentName = element
while (regex.test(componentName)) {
componentName = componentName.replace(regex, '')
const component = resolveComponent(componentName)
const component = nuxtApp.vueApp.component(formatName(componentName) + 'Default')
if (typeof component === 'object' && component.name) {
return component
}
}

return null
// Try resolving by adding 'Default' suffix.
const defaultComponent = nuxtApp.vueApp.component(formatName(element) + 'Default')
if (typeof defaultComponent === 'object' && defaultComponent.name) {
return defaultComponent
}

// If not found, try with resolveComponent. This provides a warning if the component is not found.
return typeof resolveComponent(element) === 'object' ? resolveComponent(element) : null
}

/**
Expand Down

0 comments on commit d6a338c

Please sign in to comment.