Question presented to candidate: "Our post editor needs a topics field. Users type a topic and press Enter or a comma to turn it into a chip; each chip has a remove button; Backspace in the empty box removes the last chip. No duplicates, at most five topics. Build it as a reusable component whose parent receives the list."
Requirements:
- Enter or a comma turns the typed text into a chip. Text is trimmed and inner spaces collapsed; empty text is ignored.
- Pasting "a, b, c" adds several chips at once; text after the last comma stays in the box.
- Duplicates are rejected case-insensitively, tags longer than 20 characters are rejected, and at most 5 tags are allowed. Each rejection is explained.
- Each chip has an accessible remove button; after removing, focus returns to the text box.
- Backspace in an empty box first highlights the last chip, and a second Backspace removes it.
- Clicking anywhere in the field focuses the text box. The parent owns the array of tags.
- Messages ("Added vue", "vue is already added") are announced to screen readers.
What a strong answer should cover:
- The state split: the tag array (owned by the parent, controlled) versus the draft text, the armed flag and the message (local).
- A pure
addPartshelper that validates and returns the next list plus a message, which makes the rules easy to test. - Handling commas in the
inputevent, notkeydown, so paste and mobile keyboards work; Enter inkeydownwith an IME composition guard. - Focus management after removal, and a live region for announcements.
- Stable keys for the chips (the tag text, since tags are unique).
Clarifying questions expected:
- "Are tags case-sensitive? Should 'React' and 'react' count as the same?"
- "Free text, or only tags from a suggestion list?"
- "What separators besides Enter and comma: Tab, space, semicolon?"
Code / implementation expected: Yes. A working component with add, dedupe, limits, remove by button and Backspace, paste splitting and announcements.