Angular Component Autocompletion with TypeScript
Implementing intellisense (code completion, suggestions, and type checking) within Angular components significantly boosts developer productivity and reduces errors. This challenge focuses on creating a simple, reusable Angular component that leverages TypeScript's type system to provide autocompletion suggestions for a predefined set of options within an input field. This will demonstrate how to leverage TypeScript's type safety and Angular's reactive forms to achieve a basic intellisense-like experience.
Problem Description
You are tasked with building an Angular component called AutocompleteInput that provides autocompletion suggestions for a user typing into an input field. The component should:
- Accept an array of strings as input: This array represents the possible completion options. The input will be named
options. - Use a reactive form: The component should utilize Angular's reactive forms to manage the input field's value.
- Filter suggestions: As the user types, the component should filter the
optionsarray to display only the suggestions that match the current input value. - Display suggestions: The filtered suggestions should be displayed in a dropdown list below the input field.
- Update the input field: When the user selects a suggestion from the dropdown, the input field's value should be updated to the selected suggestion.
- Handle empty input: When the input field is empty, no suggestions should be displayed.
- Handle no matches: If the input field contains text but no matching suggestions are found, a "No matches found" message should be displayed.
Expected Behavior:
- The component should render an input field and a dropdown list.
- As the user types in the input field, the dropdown list should dynamically update to show matching suggestions.
- Clicking on a suggestion in the dropdown list should populate the input field with that suggestion.
- The component should gracefully handle cases where there are no suggestions or no matches.
Examples
Example 1:
Input: options = ["apple", "banana", "cherry", "date"]
Input: User types "ap"
Output: Dropdown displays: ["apple"]
Explanation: The component filters the options array and displays only "apple" because it starts with "ap".
Example 2:
Input: options = ["apple", "banana", "cherry", "date"]
Input: User types "grape"
Output: Dropdown displays: "No matches found"
Explanation: The component filters the options array and finds no matches for "grape", so it displays the "No matches found" message.
Example 3:
Input: options = ["apple", "banana", "cherry", "date"]
Input: Input field is empty
Output: Dropdown is hidden.
Explanation: When the input field is empty, no suggestions are displayed.
Constraints
- The component should be written in TypeScript.
- The component should use Angular's reactive forms.
- The component should be reusable and accept the
optionsarray as an@Input(). - The component should not use any external libraries beyond Angular itself.
- The filtering should be case-insensitive.
- The component should handle a maximum of 100 options in the
optionsarray. Performance beyond this is not a primary concern for this exercise.
Notes
- Consider using
debounceTimefrom RxJS to optimize the filtering process and prevent excessive updates to the dropdown list. This will improve performance, especially with larger option sets. - Think about how to handle the selection of a suggestion (e.g., emitting an event). For this challenge, simply updating the input field is sufficient.
- Focus on the core functionality of filtering and displaying suggestions. Styling and advanced features are not required.
- The
optionsarray will always be an array of strings. You do not need to handle other data types. - Use Angular's
FormsModuleandReactiveFormsModulein your module.