Skip to content

Commit

Permalink
feat(roadiz): add RoadizBlockFactory component
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelodelain committed Mar 15, 2024
1 parent b662643 commit 5ef8434
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
31 changes: 31 additions & 0 deletions components/RoadizBlockFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { FunctionalComponent, VNodeChild } from 'vue'
import { h, resolveDynamicComponent, resolveComponent } from 'vue'
import type { RoadizWalker } from '@roadiz/types'

export interface RoadizBlockFactoryProps {
prefix?: string
blocks: RoadizWalker[]
[key: string]: unknown
}

const isComponent = (component: string): boolean => {
return typeof resolveDynamicComponent(component) !== 'string'
}

const RoadizBlockFactory: FunctionalComponent<RoadizBlockFactoryProps> = ({ blocks, prefix }, context): VNodeChild => {
const blocksWithComponent = blocks.filter((block) => {
const componentName = prefix ? prefix + block.item['@type'] : block.item['@type']
return isComponent(componentName)
})
return blocksWithComponent.map((block, index, blocks) => {
const componentName = prefix ? prefix + block.item['@type'] : block.item['@type']
return h(resolveComponent(componentName), {
walker: block,
index,
numBlocks: blocks.length,
...context.attrs,
})
})
}

export default RoadizBlockFactory
9 changes: 0 additions & 9 deletions components/VRequiredMark.vue

This file was deleted.

0 comments on commit 5ef8434

Please sign in to comment.