# Dependent Select Fields

## Objective

Load one select's options from another's choice without stale values slipping through.

A parent select that constrains a child select's options, loaded on demand, with the request ordering and invalidation handled properly.

## 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 selects whose valid options depend on another field and load the child options from the server when the parent changes.
2. Track requests so only the newest response is applied. A user changing the parent three times quickly must not end up with the first response's options.
3. Clear the child selection whenever the new option set no longer contains it, and say so rather than silently blanking the field.
4. Give the child a real disabled, loading, empty, and error state, and re-validate the pair on the server — never trust that the submitted child belongs to the submitted parent.
5. Do NOT preload every combination into the page. It bloats the payload and goes stale; fetch on demand and cache per parent value.

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

- Out-of-order responses must be discarded, not merged.
- An edit form whose saved child option is now inactive or absent must still display the saved value rather than showing an empty select.
- An empty option set needs its own message and a disabled control, not a select with one blank row.
- A failed load must offer a retry and must not leave the child enabled with nothing in it.
- Clearing the parent must clear and disable the child, not freeze the last options.
- Deep chains — country, region, city — must invalidate every level below the one that changed.

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

- [ ] Changing the parent reloads the child options and discards superseded responses.
- [ ] A child value invalidated by the parent change is cleared and the user is told.
- [ ] Loading, empty, and error states exist and disable the control appropriately.
- [ ] Saved-but-inactive child values still render on edit forms.
- [ ] The server rejects a child value that does not belong to the submitted parent.
- [ ] Every level below a changed level in a chain is invalidated.
- [ ] 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.
