Frontend School

Build a Dynamic Multi-Select Dropdown Component

Machine Coding
medium

Build a Dynamic Multi-Select Dropdown Component

Medium
React
TypeScript
Frontend
UI
Component
Dropdown
Multi-Select

Overview

Modern web applications often require sophisticated filtering and selection mechanisms. A multi-select dropdown is a fundamental UI component that allows users to pick multiple items from a predefined list, commonly used in dashboards, form inputs, and data filtering interfaces. This problem challenges you to build a robust and user-friendly multi-select dropdown component, focusing on core functionality, reusability, and good user experience.

What you need to do / Task

You are tasked with creating a MultiSelectDropdown React component that fulfills the following requirements:

  1. Component Design: Create a reusable MultiSelectDropdown component that accepts a list of Option objects and manages its internal state for selected items.
  2. Dropdown Toggle: Implement a button that toggles the visibility of the dropdown list.
  3. Multi-Selection: Allow users to select and deselect multiple options from the list. Each option should have a checkbox or similar visual indicator.
  4. Display Selected Items: Display the currently selected items as interactive "chips" or "tags" outside the dropdown list itself. Each chip should include the item's label and a button to remove it from the selection.
  5. Clear All: Provide a "Clear All" action within the dropdown list to deselect all chosen options.
  6. Outside Click: The dropdown list should close when a user clicks anywhere outside of the component.

UI/UX expectations

  • Dropdown Toggle: A clear button or input field that indicates the current selection count or placeholder text when nothing is selected. It should visually respond to user interaction.
  • Option List: When open, the dropdown should display a scrollable list of available options. Each option should clearly show its selected/unselected state (e.g., via a checkbox).
  • Selected Chips: Chips for selected items should be visually distinct, clearly labeled, and include an 'x' button for deselection. They should appear below or adjacent to the dropdown toggle.
  • Responsiveness: The component should be reasonably responsive, adapting to typical desktop screen sizes.
  • Accessibility (Basic): Ensure basic ARIA attributes are used for the dropdown toggle (aria-haspopup, aria-expanded) and options (role="option", aria-selected) to convey state to assistive technologies.

Acceptance criteria

  • The MultiSelectDropdown component renders correctly with provided options.
  • Clicking the toggle button opens and closes the dropdown list.
  • Clicking an option in the list toggles its selection state (adds/removes it from the selected list).
  • Selected options are visually represented as chips outside the dropdown.
  • Clicking the 'x' button on a chip removes that item from the selection.
  • Clicking the "Clear All" button deselects all options and removes all chips.
  • Clicking outside the dropdown component closes the dropdown list.
  • Basic ARIA attributes are applied to enhance accessibility.

Constraints

Constraints & assumptions

  • You should use React with TypeScript.
  • Do not use any external UI component libraries (e.g., Material UI, Ant Design); build the component from scratch.
  • The options will be an array of objects with value: string and label: string properties.
  • The number of options will be relatively small (e.g., up to 50-100 items), so no advanced virtualization is required.
  • Focus on functional correctness and clean component design.

Out of scope

  • Advanced search or filtering capabilities within the dropdown list.
  • Complex keyboard navigation beyond basic open/close.
  • Full ARIA compliance for all interactive elements (focus on key dropdown and selection states).
  • Performance optimizations for extremely large datasets (e.g., thousands of options).

What interviewers evaluate

  • Component Design: Reusability, modularity, and clean separation of concerns.
  • State Management: Effective use of React hooks (useState, useEffect, useRef) for managing component state and side effects.
  • Interaction & UI/UX: Correct implementation of dropdown toggle, multi-selection, and chip display, including handling outside clicks.
  • Accessibility: Basic application of ARIA attributes to improve usability for assistive technologies.
  • Code Quality: Readability, maintainability, and adherence to TypeScript best practices.

Loading editor...