Frontend School

Advanced Tag Input with Typeahead and Validation

Machine Coding
hard

Advanced Tag Input with Typeahead and Validation

Hard
React
JavaScript
Frontend
UI/UX
Accessibility
Debouncing
Forms
Hooks

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:

  1. Tag Addition: Users should be able to type a tag and add it by pressing Enter or , (comma).
  2. 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.
  3. 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.
  4. 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.
  5. 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).

Constraints

Constraints & assumptions

  • You should use React for building the UI component.
  • The list of availableTags for suggestions is provided as a static array within the component or passed as a prop.
  • Focus on functional correctness and clean code; extensive styling beyond basic layout and visual feedback is not required.
  • The debounce time for suggestions should be approximately 300ms.
  • Maximum allowed tags is 5.
  • Tags should be trimmed of leading/trailing whitespace before being added.

Out of scope

  • Full ARIA attribute implementation for advanced accessibility beyond basic keyboard navigation.
  • Persistent storage of tags (e.g., local storage, backend API integration).
  • Extensive error handling for network requests (not applicable here).
  • Complex styling frameworks (e.g., Material UI, Ant Design) – use plain CSS.

What interviewers evaluate

  • Functional Correctness: Does the component fully implement all specified features for tag addition, removal, typeahead, and validation?
  • Code Quality & Design: Is the code clean, well-structured, and easy to understand? Are React best practices for state management, component lifecycle, and reusability followed?
  • Performance & Efficiency: Are mechanisms like debouncing correctly applied to optimize user experience and resource usage?
  • User Experience & Accessibility: Is the component intuitive to use, and does it provide adequate keyboard navigation and visual feedback for interactions and errors?

Loading editor...