Overview
In many business applications, users need a dynamic and intuitive way to manage data. For an e-commerce platform, efficient inventory management is crucial. This problem focuses on building a core component for such a system: an editable data grid that allows users to update product stock quantities directly within the table.
This exercise will evaluate your ability to design and implement interactive UI components, manage complex component state, and handle user input with appropriate validation, all within a reasonable time budget.
What you need to do / Task
Your primary task is to develop a React component that displays a list of products in a table format, allowing users to edit the 'Stock' quantity for each product.
- Render Product Data: Display the provided mock product data in a clean, readable table. Each row should show Product Name, SKU, Stock, and Price.
- Enable In-Place Editing: Make the 'Stock' column editable. When a user interacts with a 'Stock' cell (e.g., clicks on it or focuses it), it should transform into an input field displaying the current stock value.
- Row-Specific Actions: When a row is in edit mode for its 'Stock' field, two action buttons, 'Save' and 'Cancel', should appear for that specific row. These buttons should only be visible when the row is being edited.
- Input Validation: Implement basic validation for the 'Stock' input field. It must accept only non-negative integer values. Invalid input should prevent saving and optionally provide visual feedback.
- Persist Changes: Clicking 'Save' should update the product's stock quantity in the component's state and revert the row to view mode.
- Discard Changes: Clicking 'Cancel' should discard any unsaved changes for that row and revert it to view mode, restoring the original stock value.
- Visual Feedback for Unsaved Changes: Any row where the 'Stock' value has been changed in the input field but not yet 'Saved' (i.e., the 'Save' button is visible) should have a distinct visual indicator (e.g., a different background color or border) to highlight that it has pending modifications.
UI/UX expectations
- Layout: A clear, well-structured HTML
<table> with distinct headers and rows.
- Interaction: Clicking a 'Stock' cell or an 'Edit' button (if you choose to add one, though not strictly required) should seamlessly transition the cell into an
<input type="number"> field.
- State Visibility: Clearly differentiate between the view state (displaying static text) and the edit state (displaying an input field and action buttons).
- Buttons: 'Save' and 'Cancel' buttons should be clearly labeled and functional.
- Accessibility (Basic): Ensure the input field is focusable and allows keyboard input.
Acceptance criteria
- [ ] The initial product data is correctly displayed in a table.
- [ ] Clicking on a 'Stock' cell makes it editable, showing an input field.
- [ ] The input field is pre-populated with the current stock value.
- [ ] 'Save' and 'Cancel' buttons appear only when a row is in edit mode.
- [ ] Entering a non-integer or negative value into the 'Stock' input prevents saving.
- [ ] Clicking 'Save' updates the displayed stock value and removes edit controls.
- [ ] Clicking 'Cancel' discards changes and reverts the row to its original state, removing edit controls.
- [ ] Rows with pending, unsaved changes are visually highlighted.