Overview
In modern web applications, effective data categorization is crucial for user experience and information retrieval. Imagine building a feature for a project management tool where users can assign multiple tags to tasks, enhancing searchability and organization. This problem challenges you to implement a robust and user-friendly tag input component, focusing on interactive features and validation.
What you need to do / Task
Your goal is to create a React component, TagInput, that allows users to add and manage tags with advanced features:
- Tag Addition: Users should be able to type a tag and add it by pressing
Enter or , (comma).
- Tag Removal: Tags can be removed by clicking a designated 'x' button on each tag. Additionally, pressing
Backspace when the input field is empty should remove the last added tag.
- Typeahead Suggestions: As the user types, display a list of relevant suggestions from a predefined
availableTags list. This suggestion fetching should be debounced to prevent excessive filtering/rendering on rapid input.
- Suggestion Interaction: Users should be able to navigate the suggestions list using
ArrowUp and ArrowDown keys, and select a highlighted suggestion by pressing Enter or Tab.
- Validation & Feedback:
- Prevent adding duplicate tags.
- Enforce a maximum number of tags (e.g., 5 tags).
- Provide clear visual feedback (e.g., error messages) for validation failures.
UI/UX expectations
- Tag Display: Added tags should be displayed as distinct 'pills' or 'badges' within the input area, each with a clickable 'x' for removal.
- Input Field: The input field for typing new tags should be integrated smoothly within the tag container, allowing tags to wrap if needed.
- Suggestions List: Suggestions should appear as a dropdown list directly below the input field, visually distinct and easy to read.
- Active Suggestion: The currently highlighted suggestion (via keyboard navigation) should have a clear visual indicator (e.g., different background color).
- Error Messages: Validation errors (e.g., duplicate tag, max tags reached) should be displayed prominently and temporarily near the input field.
Acceptance criteria
- [ ] User can add tags using
Enter or ,.
- [ ] User can remove tags by clicking 'x' on a tag.
- [ ] User can remove the last tag by pressing
Backspace when the input is empty.
- [ ] Typeahead suggestions appear as the user types, filtered from
availableTags.
- [ ] Suggestion filtering is debounced (e.g., 300ms).
- [ ] User can navigate suggestions using
ArrowUp/ArrowDown keys.
- [ ] User can select a highlighted suggestion using
Enter or Tab.
- [ ] Duplicate tags cannot be added.
- [ ] A maximum of 5 tags can be added.
- [ ] Clear visual feedback is provided for validation errors.
- [ ] The component maintains its state correctly (added tags, input value, suggestions).