# Form: cascading fields

SvForm dependent selects - a child list derives from the parent value via a function `options`, and `dependsOn` clears the child when the parent changes so a stale selection never lingers. Country -> State -> City.

Uses `@svgrid/grid` (MIT). Category: Layout. Live demo: https://svgrid.com/demos/405-form-cascading/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

Dependent selects in SvForm for Svelte 5: a child field's options is a function of the current values, so State lists only the states of the chosen Country and City only the cities of the chosen State, and dependsOn clears the child when its parent changes so a stale selection never lingers.

SvForm cascading (dependent) fields - a child list derives from the parent's value via a function `options`, and `dependsOn` clears the child when the parent changes so a stale selection never lingers. Country -> State -> City.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How do I derive a field's options from another field?

Give the field options: (values) => list. SvForm calls it whenever values change, so the State select always reflects the current Country.

### Why is dependsOn needed as well?

Without it a chosen state would stay selected after the country changed even though it is no longer in the list. dependsOn: ['country'] resets the child when the parent changes.

### Does this work for more than two levels?

Yes. City depends on State which depends on Country here; each level declares its own dependsOn.

## Related documentation

- [SvForm](https://svgrid.com/docs/help/ui-components/sv-form.md)

## Related articles

- [Avoiding Layout Thrash in Custom Grid Cells](https://svgrid.com/blog/avoiding-layout-thrash-custom-cells/) - Layout thrash from interleaved DOM reads and writes is the most common cause of scroll jank in grids with custom cells - here is how to find it and design your way out of it.
- [Column Definitions in SvGrid - Fields, Accessors, and Formatting](https://svgrid.com/blog/column-definitions-deep-dive/) - A practical look at how ColumnDef controls field binding, value resolution, sort behavior, and custom cell rendering in SvGrid.
- [Throttling Live Grid Updates to Animation Frames](https://svgrid.com/blog/throttle-live-updates-animation-frames/) - High-frequency WebSocket feeds will wreck grid performance if you apply every message immediately. One rAF buffer and a Map coalescer cuts thousands of renders down to 60 per second.

## Source code (405-form-cascading.svelte)

```svelte
<script lang="ts">
  /**
   * SvForm cascading (dependent) fields - a child list derives from the parent's
   * value via a function `options`, and `dependsOn` clears the child when the
   * parent changes so a stale selection never lingers. Country -> State -> City.
   */
  import { SvForm, SvToaster, toast, type FormField } from '@svgrid/grid'

  const countries = [
    { value: 'us', label: 'United States' },
    { value: 'ca', label: 'Canada' },
  ]
  const states: Record<string, { value: string; label: string }[]> = {
    us: [ { value: 'ca-s', label: 'California' }, { value: 'ny', label: 'New York' }, { value: 'tx', label: 'Texas' } ],
    ca: [ { value: 'on', label: 'Ontario' }, { value: 'bc', label: 'British Columbia' }, { value: 'qc', label: 'Quebec' } ],
  }
  const cities: Record<string, { value: string; label: string }[]> = {
    'ca-s': [ { value: 'la', label: 'Los Angeles' }, { value: 'sf', label: 'San Francisco' } ],
    ny: [ { value: 'nyc', label: 'New York City' }, { value: 'buf', label: 'Buffalo' } ],
    tx: [ { value: 'aus', label: 'Austin' }, { value: 'dal', label: 'Dallas' } ],
    on: [ { value: 'tor', label: 'Toronto' }, { value: 'ott', label: 'Ottawa' } ],
    bc: [ { value: 'van', label: 'Vancouver' }, { value: 'vic', label: 'Victoria' } ],
    qc: [ { value: 'mtl', label: 'Montreal' }, { value: 'qcc', label: 'Quebec City' } ],
  }

  const fields: FormField[] = [
    { name: 'country', label: 'Country', type: 'select', options: countries, required: true, placeholder: 'Pick a country' },
    { name: 'state', label: 'State / Province', type: 'select', dependsOn: 'country', required: true, placeholder: 'Pick a state',
      options: (v) => states[v.country] ?? [] },
    { name: 'city', label: 'City', type: 'select', dependsOn: 'state', required: true, placeholder: 'Pick a city',
      options: (v) => cities[v.state] ?? [] },
  ]

  function onSubmit(values: Record<string, unknown>) {
    toast.success(`Selected ${values.city} (${values.state}, ${values.country})`)
  }
</script>

<div class="wrap">
  <h2>Cascading location</h2>
  <p class="muted">Each list depends on the one above; changing a parent clears its children.</p>
  <SvForm {fields} columns={1} submitLabel="Save location" {onSubmit} />
</div>

<SvToaster position="bottom-right" />

<style>
  .wrap { padding: 22px; max-width: 420px; }
  h2 { margin: 0; font-size: 19px; font-weight: 700; }
  .muted { margin: 3px 0 16px; color: var(--sg-muted, #64748b); font-size: 13px; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/405-form-cascading.svelte)
