Overview
At Spotify, robust tools for managing content metadata are crucial. Tags are used across various features, from organizing user-generated content to categorizing internal data for recommendations. A well-designed tag input component significantly improves the usability of these tools.
This problem challenges you to build a dynamic tag editor for playlist metadata, enabling users to easily add, view, and remove tags associated with a playlist.
What you need to do / Task
Your primary task is to create a reusable TagInput React component that fulfills the following requirements:
- Display Existing Tags: The component should accept an initial list of tags and render them visually.
- Add New Tags: Users must be able to add new tags by:
- Typing text into an input field and pressing the
Enter key.
- Typing text into an input field and pressing the
comma (,) key.
- Remove Tags: Each displayed tag must have a clickable 'x' button (or similar icon) that removes the tag when clicked.
- Input Clearing: After a tag is successfully added, the input field should automatically clear.
- Prevent Duplicates: The component must prevent adding tags that are already present in the list (case-insensitive comparison).
- Validate Tag Length: Tags should not exceed a maximum length (e.g., 20 characters).
- Handle Empty Input: Submitting an empty string should not create a new tag.
- Propagate Changes: The component should notify its parent whenever the list of tags changes.
UI/UX expectations
- Visual Distinction: Tags should appear as distinct, visually separated elements (e.g., rounded rectangles or badges).
- Clear Input: Provide a clear and accessible input field for adding new tags.
- Removal Mechanism: Each tag should clearly display an interactive element (e.g., an 'x' icon) for removal.
- Focus Management: The input field should ideally retain focus after adding or removing a tag to allow for continuous input, enhancing user flow.
- Basic Accessibility: Ensure interactive elements (input, remove buttons) are keyboard navigable and have appropriate labels if needed.
Acceptance criteria
- User can type text into the input field and press
Enter to add a new tag.
- User can type text into the input field and press
comma (,) to add a new tag.
- Newly added tags are immediately visible within the component.
- Clicking the 'x' button on an existing tag successfully removes it from the display.
- Attempting to add a duplicate tag (case-insensitive) does not result in its addition.
- Attempting to add a tag with an empty string (or only whitespace) does not create a new tag.
- Attempting to add a tag exceeding the maximum allowed length is prevented.
- The input field clears automatically after a tag has been successfully added.
- The parent component (
App) receives updated tags via the onTagsChange prop.