From Chaos to Clarity: How I Refactored My Edit Modal into Maintainable, Scalable React Components
What Did I End Up With?
- EditSourceModal.tsx (the parent, controls modal logic and orchestration)
- AccordionItem.tsx (shows each payment/investment in an accordion, with fields)
- constants/fieldConfig.ts (PAYMENT_FIELDS and ITEM_FIELDS describe all editable fields)
- FieldInput.tsx (super basic, dumb and generic input/selector component)
The best part:
EditSourceModal.tsx – The Orchestrator
Tsx
Internal State
Tsx
- localSource: Holds what I’m currently editing.
- openItemAccordions: Which accordion(s) are open.
- errors: Error messages keyed by field.
useEffect for Reset
Tsx
useEffect for Scroll Lock
Tsx
Input/Edit Handlers
Tsx
- Decides if we’re editing "payments" or "items."
- Finds the one we're editing (via id), and updates just that field.
Tsx
- Updates a top-level field without touching others.
Validation logic
Tsx
- Checks that required fields are present (MVP-level, but ready to extend!).
Submit Handler
Tsx
The Source Fields
Tsx
Tsx
constants/fieldConfig.ts – All Fields Centralized
Tsx
FieldInput.tsx – A Simple, Dumb Field Component
Tsx
- Handles selects, checkboxes, text, numbers, dates—just unite data and logic.
- Shows an error if needed.
AccordionItem.tsx – All The Heavy Lifting, None of the Spaghetti
Tsx
- No matter what shape the data becomes, this will render/expand/collapse and edit each field correctly.
- One clear file—not hundreds of repetitive lines for every possible field.
Main Modal Use: Bringing It All Together
Tsx
Clean Modal Controls
Tsx
Final Reflections
This process cemented how much TypeScript + React + good planning = massive time and sanity savings. Don’t be afraid to modularize—even if it feels like “extra work.” It always pays off.
If you enjoyed this, found it useful, or have feedback, please share a comment! I’ll keep learning, building, and sharing—onward and upward!
#react #nextjs #typescript #refactor #frontend #webdevelopment #modal #forms #learningjourney #components #scalable #maintainable #ui #personalproject