Dynamic Data Selection with Props in Angular
This challenge focuses on building a reusable Angular component that can display data from a provided list based on a configurable selector mechanism. You will implement a component that accepts a list of items and a "selector function" as input, allowing the consumer to dynamically determine which item from the list should be displayed. This is a common pattern for creating flexible and data-driven UI elements.
Problem Description
You need to create an Angular component, let's call it DynamicDisplayComponent, that takes two primary inputs:
items: An array of any type of data.selector: A function that receives theitemsarray and a current "state" or "key" (which will also be an input to the component) and returns the index of the item to be displayed from theitemsarray.
The DynamicDisplayComponent should render the item at the index returned by the selector function. You'll also need to handle cases where the selector function might return an invalid index.
Key Requirements:
- Create a component
DynamicDisplayComponentthat accepts@Input()properties:itemsandselector. - The
selectorfunction signature should be(items: any[], key: any) => number. It takes theitemsarray and akeyand returns the index of the item to display. - The component should also accept an
@Input()propertykeywhich will be passed to theselectorfunction. - The component should display the selected item. For simplicity, assume the items are strings or have a simple
toString()method. - Implement basic error handling for invalid indices returned by the
selector.
Expected Behavior:
- When
itemsandkeyare provided, and theselectorfunction returns a valid index within the bounds ofitems, the component should display the item at that index. - If the
selectorreturns an index less than 0 or greater than or equal toitems.length, or ifitemsis empty, the component should display a default message like "No item selected." or "Invalid selection.". - The component should re-render if
items,key, orselectorchanges.
Edge Cases:
itemsarray is empty.selectorfunction returns an index outside the valid range ofitems.keyinput is not provided or is of an unexpected type for theselectorfunction.
Examples
Example 1:
Input:
DynamicDisplayComponentinputs:items:['Apple', 'Banana', 'Cherry']key:'B'selector:(items, key) => items.findIndex(item => item.startsWith(key))
Output:
<p>Banana</p>
Explanation: The selector function finds the index of the first item starting with 'B', which is 'Banana' at index 1. The component displays this item.
Example 2:
Input:
DynamicDisplayComponentinputs:items:[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]key:2selector:(items, key) => items.findIndex(item => item.id === key)
Output:
<p>Bob</p>
Explanation: The selector function finds the index of the item with id equal to 2, which is 'Bob' at index 1. The component displays this item.
Example 3: (Edge Case - Invalid Selection)
Input:
DynamicDisplayComponentinputs:items:['Dog', 'Cat']key:'Z'selector:(items, key) => items.findIndex(item => item.startsWith(key))
Output:
<p>Invalid selection.</p>
Explanation: The selector function returns -1 because no item starts with 'Z'. The component displays the "Invalid selection." message.
Constraints
- The
itemsarray can contain up to 1000 elements. - The
keyinput can be of any primitive type (string, number, boolean) ornull/undefined. - The
selectorfunction is assumed to be a pure function and should not have side effects. - The component should aim for efficient re-rendering by Angular's change detection.
Notes
- Consider using
OnChangeslifecycle hook to detect changes in inputs if necessary, though Angular's default change detection will likely handle most cases. - The
selectorfunction is where the dynamic logic resides. Think about how differentkeytypes anditemsstructures might influence its implementation. - For displaying the selected item, you can use string interpolation
{{ selectedItem }}in your template. Ensure you handle the case whereselectedItemmight benullorundefinedbefore rendering. - This challenge is designed to be implemented as a standalone component.