Angular Search Component Challenge: Filtering a Product List
This challenge focuses on building a reusable search component in Angular that filters a list of products based on user input. Implementing effective search functionality is a common requirement in web applications, and this exercise will test your understanding of Angular's data binding, event handling, and component interaction.
Problem Description
You are tasked with creating an Angular component that displays a list of products and allows users to filter this list by searching for a keyword. The component should:
- Display a list of products: The component will receive an array of product objects as input. Each product object will have at least a
nameproperty (string) and adescriptionproperty (string). - Provide a search input: A text input field should be provided for the user to enter their search query.
- Filter the product list: As the user types in the search input, the displayed product list should be dynamically filtered. Only products whose
nameordescriptioncontain the search query (case-insensitive) should be shown. - Handle empty search: When the search input is empty, the component should display the entire original list of products.
- Handle no results: If the search query doesn't match any products, display a message indicating that no results were found.
Expected Behavior:
- The component should be reusable and easily integrated into other Angular applications.
- The filtering should be performed efficiently, especially with large product lists.
- The UI should update smoothly and responsively as the user types.
Examples
Example 1:
Input: products = [
{ name: 'Laptop', description: 'Powerful laptop for work and gaming' },
{ name: 'Mouse', description: 'Ergonomic wireless mouse' },
{ name: 'Keyboard', description: 'Mechanical keyboard with RGB lighting' }
]
searchQuery = 'lap'
Output: [
{ name: 'Laptop', description: 'Powerful laptop for work and gaming' }
]
Explanation: Only the 'Laptop' product matches the search query 'lap' (case-insensitive).
Example 2:
Input: products = [
{ name: 'Laptop', description: 'Powerful laptop for work and gaming' },
{ name: 'Mouse', description: 'Ergonomic wireless mouse' },
{ name: 'Keyboard', description: 'Mechanical keyboard with RGB lighting' }
]
searchQuery = ''
Output: [
{ name: 'Laptop', description: 'Powerful laptop for work and gaming' },
{ name: 'Mouse', description: 'Ergonomic wireless mouse' },
{ name: 'Keyboard', description: 'Mechanical keyboard with RGB lighting' }
]
Explanation: An empty search query should display the entire product list.
Example 3:
Input: products = [
{ name: 'Laptop', description: 'Powerful laptop for work and gaming' },
{ name: 'Mouse', description: 'Ergonomic wireless mouse' },
{ name: 'Keyboard', description: 'Mechanical keyboard with RGB lighting' }
]
searchQuery = 'monitor'
Output: "No products found matching your search."
Explanation: No products have 'monitor' in their name or description.
Constraints
- Product List Size: The product list can contain up to 1000 products.
- Search Query Length: The search query will be a string with a maximum length of 50 characters.
- Performance: Filtering should be reasonably efficient. Avoid unnecessary DOM manipulations. The filtering logic should complete within 200ms for the given product list size.
- Case-Insensitive Search: The search should be case-insensitive.
- Input Type:
productswill be an array of objects, each withname(string) anddescription(string) properties.searchQuerywill be a string.
Notes
- Consider using Angular's
ngModelfor two-way data binding between the search input and the component's property. - Use array methods like
filter()to efficiently filter the product list. - Think about how to handle the "no results found" message gracefully.
- Focus on creating a clean, well-structured, and reusable Angular component.
- You don't need to create a full Angular application; just the component itself is sufficient. Assume the product data is already available.
- Error handling (e.g., invalid product data) is not required for this challenge.