Frontend School

Expandable Category Tree View

Machine Coding
easy

Expandable Category Tree View

Easy
Tree View
React
Frontend
Machine Coding
State Management

Overview

At Urban Company, we offer a wide range of services organized into a hierarchical category structure. To enhance user experience and simplify navigation, we need a component that can display these categories in an expandable and collapsible tree view. This problem focuses on building a foundational UI component for presenting hierarchical data effectively.

What you need to do / Task

Your task is to implement a React component, CategoryTreeView, that renders a list of service categories and their subcategories in an interactive tree structure. The component should allow users to expand and collapse parent categories to reveal or hide their children.

  1. Render Categories: Display the name of each category.
  2. Hierarchical Display: Visually indent subcategories to indicate their nested level.
  3. Expand/Collapse Functionality: Implement controls (e.g., an arrow icon) that, when clicked, toggle the visibility of a parent category's children.
  4. Initial State: All parent categories should be collapsed by default when the component first renders.

UI/UX expectations

  • Each category item should display its name clearly.
  • Parent categories (those with children) must have a visual indicator (e.g., a right-pointing triangle ▶ when collapsed, and a down-pointing triangle ▼ when expanded) next to their name.
  • Clicking on this indicator or the parent category's name should toggle its expanded/collapsed state.
  • Child categories must be indented relative to their parent to visually represent the hierarchy.

Acceptance criteria

  • The component renders the provided hierarchical category data correctly.
  • All parent categories are initially collapsed on load.
  • Clicking the expand/collapse indicator (or category name) of a parent category toggles the visibility of its direct children.
  • Subcategories are visibly indented according to their depth in the hierarchy.
  • The UI remains responsive and free of console errors during interactions.
  • The component correctly handles categories with no children (leaf nodes) without showing an expand/collapse indicator.

Constraints

Constraints & assumptions

  • The input data will be a nested array of Category objects, where each object has an id, name, and an optional children array (containing more Category objects). You do not need to reconstruct the tree from a flat list.
  • The maximum depth of the category tree will be 3-4 levels.
  • No external API calls are required; use the provided mock data.
  • The solution should be implemented using React (functional components).

Out of scope

  • Implementing search or filtering functionality for categories.
  • Advanced styling beyond basic layout and expand/collapse indicators.
  • Keyboard navigation or complex accessibility features.
  • Persistence of expanded states across reloads.

What interviewers evaluate

  • Correct implementation of recursive rendering for hierarchical data.
  • Effective use of React state to manage UI interactions (expand/collapse).
  • Clean, modular, and readable component structure.
  • Adherence to basic UI/UX requirements for tree views.

Loading editor...