Overview
TheAddNewItemModal component provides a user interface for adding new payment items to the system. It allows users to specify a payment name and select which months the payment applies to, with support for both one-time and recurring monthly payments.
Component Location
src/components/modals/AddNewItemModal.jsx
Props
| Prop | Type | Required | Description |
|---|---|---|---|
isOpen | boolean | Yes | Controls the modal visibility state |
onClose | function | Yes | Callback function to close the modal |
Features
Form Validation
The component implements client-side validation:- Name validation: Payment name cannot be empty
- Month selection validation: At least one month must be selected
- Real-time error clearing: Errors are cleared when the user starts typing
Month Selection Modes
The component supports two distinct modes:- Monthly (Recurring): When “Mensual” is selected, the payment applies to all months automatically
- Specific Months: Users can select individual months (Enero through Diciembre)
State Management
The component manages three key state values:Context Integration
The modal integrates with theItemsContext to add new items to the global state:
Usage Example
Workflow
- Open Modal: User clicks a trigger button to open the modal
- Enter Name: User types the payment name in the input field
- Select Months: User chooses either:
- “Mensual” checkbox for recurring monthly payments
- Individual month checkboxes for specific months
- Validation: On save, the component validates:
- Name is not empty
- At least one month is selected
- Save: If validation passes:
- Item is added to context via
addItem() - Modal closes automatically
- Form resets for next use
- Item is added to context via
- Cancel: User can close the modal without saving
Month Handling Logic
ThehandleMonthChange function manages month selection:
Error Handling
Errors are displayed below the form when validation fails:- “El nombre no puede estar vacío.” (Name cannot be empty)
- “Debes seleccionar al menos un mes.” (You must select at least one month)
UI Components
The modal uses Chakra UI components:Modal,ModalOverlay,ModalContent- Modal structureModalHeader,ModalBody,ModalFooter- Content sectionsModalCloseButton- Close button in headerFormControl,FormLabel,Input- Form elementsMenu,MenuButton,MenuList,MenuItem- Month selection dropdownCheckbox- Month selection checkboxesButton- Action buttons (Save/Close)
Accessibility
The component includes:- Form control validation states
- Disabled state for conflicting month selections
- Centered modal positioning (
isCentered) - Close button for keyboard navigation
- Scrollable month list for better usability
Data Flow
- Local component state captures form data
- Validation runs on save
addItem()is called from ItemsContext- Context processes the item (converts ‘mensual’ to boolean flag)
- Item is stored in localStorage via ItemsContext effect