Hone logo
Hone
Problems

Vue Macro Task: Dynamic Component Rendering

This challenge focuses on implementing a "macro task" system within a Vue 3 application using TypeScript. You'll create a flexible component that can dynamically render a list of other components based on a configuration, allowing for complex UIs to be built compositionally.

Problem Description

Your task is to build a MacroTaskRenderer Vue component that accepts a configuration object and dynamically renders a series of child components. This is useful for creating flexible dashboards, forms, or any UI where the structure needs to be defined programmatically or changed at runtime.

Key Requirements:

  • The MacroTaskRenderer component should accept a prop named taskConfig which is an array of objects.
  • Each object in the taskConfig array should define a component to be rendered and its associated props.
  • The MacroTaskRenderer should iterate through the taskConfig and render the specified components.
  • Components should be registered globally or locally with the MacroTaskRenderer. For simplicity, assume components are globally registered for this challenge.
  • Props defined in the taskConfig object should be correctly passed to their respective child components.

Expected Behavior:

When MacroTaskRenderer receives a taskConfig, it should render each item in the array as a distinct Vue component. The componentName property from the taskConfig item will determine which registered component to render, and the props object will be passed down to that component.

Edge Cases:

  • What happens if a componentName in taskConfig does not correspond to a registered component?
  • What happens if taskConfig is an empty array?
  • What happens if a props object is missing or invalid?

Examples

Example 1:

// Assume the following components are globally registered:
// HelloWorld: <template>{{ msg }}</template><script setup lang="ts">defineProps<{ msg: string }>()</script>
// AnotherComponent: <template><div>Static Content</div></template>

Input `taskConfig`:
[
  {
    componentName: 'HelloWorld',
    props: { msg: 'Hello from MacroTaskRenderer!' }
  },
  {
    componentName: 'AnotherComponent',
    props: {}
  }
]

Output:
The rendered output will contain:
1.  A `HelloWorld` component displaying "Hello from MacroTaskRenderer!".
2.  An `AnotherComponent` displaying "Static Content".

Example 2:

Input `taskConfig`:
[
  {
    componentName: 'HelloWorld',
    props: { msg: 'Another Message' }
  }
]

Output:
The rendered output will contain:
1.  A `HelloWorld` component displaying "Another Message".

Example 3 (Edge Case):

Input `taskConfig`:
[
  {
    componentName: 'NonExistentComponent', // This component is not registered
    props: {}
  },
  {
    componentName: 'HelloWorld',
    props: { msg: 'This should still render' }
  }
]

Output:
The rendered output will contain:
1.  An error message or a placeholder indicating that 'NonExistentComponent' could not be rendered.
2.  A `HelloWorld` component displaying "This should still render".

Constraints

  • The taskConfig prop will always be an array of objects.
  • Each object in taskConfig will have at least a componentName property (string).
  • The props property within each taskConfig object, if present, will be an object.
  • Performance is not a primary concern for this challenge, but avoid excessively inefficient rendering loops.

Notes

  • Consider using Vue's built-in <component :is="componentName"> feature for dynamic component rendering.
  • How will you handle situations where a component name is not found? Displaying an error or a placeholder is a good approach.
  • Think about how to pass props correctly. The spread operator (...) can be very useful here.
  • For this challenge, assume all components are registered globally or are available in the scope of the MacroTaskRenderer. You do not need to implement a dynamic component registration system.
Loading editor...
typescript