# Form State Isolation

## Objective

Keep long forms responsive by narrowing what re-renders when one field changes.

Section-scoped form state with field-level subscriptions, so typing in one input does not re-render the entire form.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find the forms that visibly lag — the settings pages, the multi-section record editors, the pricing or configuration builders — and measure which of them re-render every field on every keystroke before changing anything.
2. Give each field its own subscription to its own value and its own error, so a component re-renders when the thing it displays changes and not before.
3. Keep cross-field rules — totals, date ranges, one-of-these-is-required, conditional sections — subscribed to the specific fields they depend on, and evaluate them against the whole form's current values rather than a stale snapshot taken when the section mounted.
4. Preserve section values and dirty state when a section unmounts because it collapsed, scrolled out of a virtualized list, or moved behind a step. The form owns the values; the section only displays them.
5. The warning shown when a user tries to leave with unsaved work belongs to Unsaved Changes Guard. This feature owns which values count as dirty and how they survive unmounting; that feature owns intercepting navigation. Do not implement a second navigation prompt here.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Cross-field validation must stay accurate under isolation. A rule that only re-runs when its own field changes will keep showing an end-date error after the start date was corrected, so rules must be re-evaluated when any of their inputs move.
- A collapsed, hidden, or scrolled-away section must keep its values and its dirty flag. Losing them on unmount silently discards work the user believes is entered, and it is worse than a visible error because nothing tells them.
- Fields hidden by a conditional must not contribute stale values to validation or to the payload. Decide explicitly whether hiding clears the value or merely excludes it, and apply the same rule everywhere.
- Partial saves need to know exactly which fields the user changed, so a section-level save sends only that section and does not overwrite a colleague's concurrent edit to a field the user never touched.
- Server-side validation errors arrive keyed to fields that may be inside a collapsed section, so the form must open the section, scroll to the field, and move focus rather than showing a generic failure at the top.
- Arrays of repeated rows must keep identity across reorders and deletions; keying rows by index makes the wrong row inherit another row's error and dirty state.
- An autosave or a background refresh landing mid-edit must not replace a field the user is currently typing in.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Typing in one field re-renders that field and its dependent rules, not the whole form.
- [ ] Cross-field validation produces the same result under isolation as it did when the form re-rendered wholesale.
- [ ] Collapsing, hiding, or scrolling a section away preserves its values and its dirty state.
- [ ] Conditionally hidden fields follow one documented rule for whether their values are cleared or merely excluded.
- [ ] A partial save submits only the fields the user actually changed.
- [ ] A server validation error reveals, scrolls to, and focuses the offending field wherever it sits.
- [ ] Dirty tracking is exposed for the unsaved-changes guard rather than duplicated by it.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
