Frontend School

Dynamic Tag Input Component for ServiceNow Forms

Machine Coding
medium

Dynamic Tag Input Component for ServiceNow Forms

Medium
React
TypeScript
UI Components
Forms
State Management
Accessibility

Overview

In ServiceNow, effective data categorization is paramount for efficient incident management, knowledge base organization, and robust reporting. A flexible and intuitive tag input component is a common requirement across various forms and interfaces, allowing users to quickly classify records. This problem challenges you to build a dynamic tag input component that allows users to add and remove tags seamlessly, enhancing data entry and improving user experience.

What you need to do / Task

Your primary goal is to implement a React component that functions as a tag input field. Specifically, you must:

  1. Render an input field: This field will be used for typing new tags.
  2. Display existing tags: Render current tags as visual 'pills' or 'badges' within the component, preceding the input field.
  3. Add new tags: Allow users to add a new tag by typing text into the input field and pressing Enter or , (comma).
  4. Remove existing tags: Provide a mechanism for users to remove a tag, either by clicking an 'X' button associated with each tag or by pressing Backspace when the input field is empty (to remove the most recently added tag).
  5. Handle duplicates: Prevent the addition of tags that already exist in the list (case-insensitive).
  6. Basic validation: Do not allow empty tags to be added, and enforce a maximum tag length.

UI/UX expectations

  • The input field should be clearly visible and functional.
  • Each tag should be rendered as a distinct visual element (e.g., a rounded rectangle with text and a small 'X' button for removal).
  • The 'X' button on each tag should be easily clickable.
  • After adding a tag, the input field should clear automatically.
  • When removing a tag, the visual element should disappear instantly.
  • The input field should remain focused after adding/removing a tag to facilitate continuous entry.

Acceptance criteria

  • Users can type a tag and press Enter to add it to the list.
  • Users can type a tag and press , (comma) to add it to the list.
  • Clicking the 'X' button on a tag successfully removes it from the list.
  • Pressing Backspace when the input field is empty removes the last tag in the list.
  • Attempting to add a duplicate tag (case-insensitive) does not add it to the list.
  • Attempting to add an empty tag (e.g., just pressing Enter or , without text) does not add it.
  • Tags exceeding MAX_TAG_LENGTH are not added.
  • The input field value clears after a tag is successfully added.
  • The input field remains focused after tag operations (add/remove).

Constraints

Constraints & assumptions

  • Maximum tags: The component should support a maximum of 10 tags (MAX_TAGS). If the limit is reached, the input field should be disabled.
  • Tag length: Each tag should be a simple string, with a maximum length of 25 characters (MAX_TAG_LENGTH).
  • Data handling: All tag data should be managed locally within the component's state. No external API calls or persistent storage are required.
  • Environment: The solution should be implemented using React and TypeScript.

Out of scope

  • Autocomplete or tag suggestions based on existing data.
  • Drag-and-drop reordering of tags.
  • Complex styling beyond basic layout and visual distinction of tags (e.g., theming).
  • Saving/loading tags from a backend service.

What interviewers evaluate

  • Correctness and completeness: Does the component correctly implement all tag addition and removal logic, including edge cases (duplicates, empty, max length, max count)?
  • Code quality: Is the code well-structured, readable, and maintainable, adhering to React best practices and TypeScript typing?
  • State management: Is the component's internal state managed efficiently and logically, demonstrating a good understanding of React hooks?
  • UI/UX and accessibility: Does the component meet the specified UI/UX expectations and demonstrate consideration for basic user interaction patterns and focus management?

Loading editor...