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.
- 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.
- 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.
- 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.
- 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.
- 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.