Overview
Amazon's product catalog relies heavily on accurate tagging for search, categorization, and recommendation systems. Product managers frequently need to add and manage various tags for thousands of products. This problem focuses on building a robust and intuitive tag input component that allows users to easily add and remove tags.
What you need to do / Task
Your goal is to implement a TagInput React component that facilitates adding and removing tags.
- Tag Input Field: Render an input field where users can type tag names.
- Add Tags:
- A tag should be added when the user types text into the input field and presses
Enter or , (comma).
- After adding a tag, the input field should be cleared.
- Display Tags: Display the added tags as individual, distinct "chips" below the input field.
- Remove Tags: Each tag chip should have a "remove" button (e.g., an 'X' icon) that, when clicked, removes the corresponding tag from the list.
- Handle Duplicates: Prevent the addition of duplicate tags. If a user tries to add a tag that already exists, it should not be added, and the input field should still clear.
- Empty Input: Do not add empty strings or strings consisting only of whitespace as tags.
UI/UX expectations
- The input field should be clearly visible and allow text entry.
- Tags should be rendered as distinct, visually separated "chips" or "pills" (e.g., with a background color, border, and rounded corners).
- Each tag chip must include a visible and clickable element (e.g., an 'X' or 'Remove' button) to delete the tag.
- Basic responsiveness is expected, ensuring the component looks reasonable on common screen sizes. Focus on functionality first.
Acceptance criteria
- User can type text into the tag input field.
- Pressing
Enter adds the current input value as a tag.
- Pressing
, (comma) adds the current input value as a tag.
- After adding a tag, the input field is cleared.
- Added tags are displayed as visually distinct chips below the input.
- Clicking the 'X' button on a tag chip removes it from the list.
- Attempting to add an existing tag does not add a duplicate.
- Empty or whitespace-only inputs do not create tags.