Frontend School

Build a React Tag Input Component

Machine Coding
medium

Build a React Tag Input Component

Medium
Frontend
React
JavaScript
UI Components
Tag Input
Accessibility

Overview

Postman users frequently organize their work (collections, APIs, requests) using tags for better discoverability and management. A robust and intuitive tag input component is crucial for this experience, allowing users to quickly categorize items and improve team collaboration. This problem focuses on building the core functionality of such a component.

What you need to do / Task

Your task is to build a TagInput React component that allows users to add and remove tags.

  1. Input Field: Render an input field where users can type tag names.
  2. Add Tags:
    • When a user types a tag name and presses Enter or Tab, the tag should be added to the list.
    • The input field should clear after a tag is added.
  3. Display Tags: Display the added tags as distinct visual "chips" or "pills" within the same container as the input field.
  4. Remove Tags: Each displayed tag chip should have a clickable 'x' icon (or similar) that, when clicked, removes the tag from the list.
  5. Duplicate Handling: Prevent adding duplicate tags. Tags should be considered duplicates regardless of case (e.g., "react" and "React" are the same).
  6. Empty Tag Prevention: Do not add empty tags (i.e., tags consisting only of whitespace).

UI/UX expectations

  • The input field should be clearly visible and allow text entry.
  • Tags should be displayed horizontally, wrapping to the next line if space is limited, alongside the input field within a single bordered container.
  • Each tag chip should have a distinct background and text color, making it easily distinguishable.
  • The 'x' icon for removing tags should be clearly visible and clickable within each tag chip.
  • After adding or removing a tag, the input field should retain focus, allowing for continuous tag entry.
  • Consider basic accessibility for the input and tag removal buttons (e.g., aria-label).

Acceptance criteria

  • User can type "frontend" and press Enter to add "frontend" as a tag.
  • User can type "javascript" and press Tab to add "javascript" as a tag.
  • Added tags appear as distinct chips within the tag input container.
  • Clicking the 'x' icon on a "frontend" tag chip removes it from the list.
  • Attempting to add "frontend" again (case-insensitive) when it already exists does not add a new chip.
  • Pressing Enter or Tab with an empty or whitespace-only input does not add a tag.
  • The input field clears and retains focus after a tag is successfully added.

Constraints

Constraints & assumptions

  • Technology Stack: React (functional components with Hooks) and CSS.
  • Maximum Tags: A maximum of 10 tags can be added. If the limit is reached, the input should visually indicate this (e.g., disable input or show a message) and prevent further additions.
  • Maximum Tag Length: Each tag should be no longer than 25 characters.
  • Case Insensitivity: Tag uniqueness check should be case-insensitive (e.g., "React" and "react" are the same tag).
  • No Persistence: Tags do not need to be saved or loaded from any external source; they only exist in the component's state.
  • No External Libraries: Do not use any third-party UI component libraries (e.g., Material UI, Ant Design).

Out of scope

  • Backend integration or API calls for saving/loading tags.
  • Advanced tag features like autosuggestion, tag categories, or drag-and-drop reordering.
  • Complex form validation beyond the specified length and duplicate checks.
  • Full-fledged accessibility audit; focus on basic ARIA attributes for input and buttons.

What interviewers evaluate

  • Core Functionality: Correct implementation of adding, displaying, and removing tags, including handling edge cases like duplicates and empty inputs.
  • Component Design: Logical structure, state management using React Hooks, and separation of concerns.
  • User Experience: Smooth interactions, clear visual feedback, and adherence to UI/UX expectations.
  • Code Quality: Readability, maintainability, use of idiomatic React patterns, and basic error handling.
  • Accessibility: Consideration for basic accessibility features for interactive elements.

Loading editor...