Hone logo
Hone
Problems

Implementing Key Modifiers in a Vue Component

This challenge focuses on building a simplified version of Vue's key modifiers within a custom component. Key modifiers are crucial for efficient list rendering and updates in Vue, allowing the framework to track changes and optimize DOM manipulation. By implementing this functionality, you'll gain a deeper understanding of how Vue handles list updates and improves performance.

Problem Description

You are tasked with creating a KeyedList component that accepts an array of data and renders a list of items. Each item in the list should have a unique key attribute. The component should efficiently update the list when the data array changes, re-using existing DOM elements whenever possible based on the key attribute. The component should handle additions, deletions, and reordering of items in the data array.

Key Requirements:

  • items prop: The component should accept a prop named items which is an array of objects. Each object in the array should have a unique key property (string or number).
  • Rendering: The component should render each item in the items array as a div element. The content of each div should be the id property of the item (if it exists, otherwise display the entire object as a string).
  • Key Attribute: Each rendered div element must have a key attribute set to the item's key property.
  • Efficient Updates: When the items array changes (additions, deletions, reordering), the component should efficiently update the list, re-using existing DOM elements whenever possible based on the key attribute. Avoid unnecessary re-renders of unchanged items.
  • Reordering: The component must correctly handle reordering of items in the items array.

Expected Behavior:

  • Initial render: The component should render all items from the items array with the correct key attributes.
  • Adding an item: A new div element should be added to the list with the new item's data and key.
  • Deleting an item: The corresponding div element should be removed from the list.
  • Reordering items: The order of the div elements should reflect the new order of the items array.
  • Updating an item: If an item's data changes, only the content of the corresponding div element should be updated.

Edge Cases to Consider:

  • Empty items array: The component should render an empty list.
  • items array containing objects without an id property: The component should gracefully handle this by displaying the entire object as a string.
  • Duplicate keys: While not explicitly required to handle, consider how your implementation might behave with duplicate keys (it's best to avoid them in real-world scenarios).

Examples

Example 1:

Input: items = [{ id: 1, key: 'a' }, { id: 2, key: 'b' }, { id: 3, key: 'c' }]
Output:
<div>1</div>
<div>2</div>
<div>3</div>
Explanation: The component renders three divs, each with the correct key and content.

Example 2:

Input: items = [{ id: 1, key: 'a' }, { id: 2, key: 'b' }, { id: 3, key: 'c' }];
// After: items.push({ id: 4, key: 'd' });
Output:
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
Explanation: A new div is added to the list with the new item's data and key.

Example 3:

Input: items = [{ id: 1, key: 'a' }, { id: 2, key: 'b' }, { id: 3, key: 'c' }];
// After: items.splice(1, 1); // Remove item with key 'b'
Output:
<div>1</div>
<div>3</div>
Explanation: The div with key 'b' is removed from the list.

Constraints

  • The component must be written in TypeScript.
  • You are allowed to use Vue's reactivity system (e.g., ref, reactive).
  • The component should be as performant as possible, minimizing unnecessary DOM manipulations.
  • The key property must be a string or number.
  • The items array will always contain objects.

Notes

  • This is a simplified implementation of Vue's key modifiers. A real-world implementation would be more complex and handle various edge cases.
  • Focus on the core functionality of efficiently updating the list based on the key attribute.
  • Consider using Vue's v-for directive as a reference for how Vue handles list rendering. However, you are implementing the logic yourself, not just using v-for.
  • Think about how to track the existing DOM elements and reuse them when the data changes. A simple map of keys to DOM elements can be helpful.
  • Debugging tools in your IDE or browser developer tools will be invaluable for inspecting the DOM and understanding how your component is updating.
Loading editor...
typescript