Frontend School

Item Editor Modal

Machine Coding
medium

Item Editor Modal

Medium
React
TypeScript
UI
Modal
Accessibility
State Management
Forms

Overview

You're working on a dashboard for managing various items. Users need an intuitive way to quickly edit an item's details without navigating to a new page. This problem focuses on building a robust and accessible modal dialog to facilitate editing, ensuring a smooth and user-friendly experience.

What you need to do / Task

Your primary task is to implement a reusable Modal component and integrate it into the provided App.tsx to allow editing of existing items.

  1. Create a Modal component: Design a generic, reusable Modal component that can accept children (for its content) and props for controlling its visibility and closing behavior.
  2. Integrate the Modal: In App.tsx, when a user clicks the "Edit" button for an item, the modal should open, displaying a form pre-populated with the selected item's details.
  3. Edit Form: The modal content should include form fields (e.g., text inputs for name and description, a select dropdown for status) that allow users to modify the item's properties.
  4. Save & Cancel Actions:
    • A "Save" button within the modal should update the item in the main list and then close the modal.
    • A "Cancel" button should close the modal without saving any changes.
  5. State Management: Manage the list of items and the state of the modal (open/closed, selected item) using React's local state.

UI/UX expectations

  • Overlay: The modal should appear as a centered overlay above the main application content, dimming the background.
  • Close Mechanism: A clear "X" button in the modal header should close the modal. Clicking outside the modal content area should also close it.
  • Keyboard Accessibility: Pressing the Escape key should close the modal.
  • Focus Management: When the modal opens, keyboard focus should be trapped within the modal, preventing users from tabbing to elements outside it. The initial focus should be on the first interactive element within the modal. When the modal closes, focus should return to the element that triggered its opening (the "Edit" button).
  • Form Layout: The form fields within the modal should be clearly labeled and laid out for readability.
  • Visual Feedback: Buttons should have clear visual states (e.g., hover, active).

Acceptance criteria

  • Clicking an "Edit" button opens the modal specific to that item.
  • The modal displays the selected item's current name, description, and status.
  • The modal can be closed by clicking the "X" button, clicking the "Cancel" button, clicking outside the modal content, or pressing the Escape key.
  • Modifying form fields and clicking "Save" updates the corresponding item in the main list displayed in App.tsx.
  • If the name field is empty upon "Save", an error message should be displayed, and the save action prevented.
  • Keyboard focus is correctly trapped within the modal when open and returns to the triggering element upon close.

Constraints

Constraints & assumptions

  • Frontend Only: All logic should be handled client-side using React. No backend integration is required.
  • Local State: Manage all application state using React's useState and useRef hooks as appropriate. No external state management libraries (e.g., Redux, Zustand) are permitted for this problem.
  • Styling: You may use basic CSS for layout and visual presentation, but the core task is functional implementation. Minimal styling is provided; you are expected to add necessary styles for the modal overlay and content.
  • Browser Environment: Assume a modern browser environment.
  • Time Budget: This problem is designed to be completed within approximately 45 minutes, focusing on core functionality and accessibility.

Out of scope

  • Complex form validation beyond the name field being required.
  • Deep UI library integration (e.g., Material UI, Ant Design components) — build the modal and form elements yourself.
  • Internationalization (i18n).
  • Routing or multi-page navigation.

What interviewers evaluate

  • Component Design: Ability to design a reusable, encapsulated Modal component.
  • React Fundamentals: Correct usage of state, props, and lifecycle effects (e.g., useEffect for focus management).
  • Accessibility (A11y): Implementation of keyboard navigation, focus trapping, and ARIA attributes for screen reader compatibility.
  • State Management: Effective management of local component state for form data and application data.
  • Code Quality: Readability, maintainability, and adherence to best practices.

Loading editor...