# SvGrid Studio · rich fields

Rich edit-form fields: an image UPLOAD field (SvFileInput - preview + file picker, stores a data URL with no backend, or a URL via an uploads handler) shown as an avatar in the grid, and a CASCADING dropdown (City computed from the chosen Country via dependentOptions, cleared when the country changes). Both are schema-driven on SvGridEditPanel.

Requires `@svgrid/enterprise` (and `@svgrid/grid`). Category: Studio. Live demo: https://svgrid.com/demos/198-studio-form-fields/

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

## About this example

Rich edit-form fields in the SvGrid Studio stack, both schema-driven on SvGridEditPanel. An upload field renders SvFileInput with a preview and file picker and stores an inline data URL with no backend, or pushes to storage when an uploads handler is given, and the grid shows the result as an avatar. A dependent dropdown computes its City options from the chosen Country through dependentOptions and clears when the country changes.

Data-app Studio · rich form fields - image upload + cascading dropdowns.

```text
Avatar  - an `upload` field renders SvFileInput (preview + file picker).
          With no upload handler it stores an inline data URL, so it works
          with no backend; pass `uploads` to push to storage instead.
City    - a DEPENDENT dropdown: its options are computed from the chosen
          Country (`dependentOptions`), and it clears if the country changes.
```

## Imports, features and API used

- Imports: `@svgrid/grid`, `@svgrid/enterprise`, `../shared/SvGridStudio.svelte`

## Frequently asked questions

### How is the uploaded image stored?

Without an uploads handler the field reads the file as a data URL and stores it in the row, which works offline. Pass uploads with a function that sends the file to storage and returns a URL to store that instead.

### How do I make one field's options depend on another?

Give SvGridEditPanel dependentOptions with a function per field: city: (row) => citiesFor(row.country). The panel re-evaluates it when the country changes and clears the city if it is no longer valid.

### How does the grid render the avatar?

The avatar column is mapped to a cell that renders an img from the stored URL, so the grid and the form show the same image.

## Related documentation

- [Edit forms & validation](https://svgrid.com/docs/enterprise/studio/edit-forms.md)

## Related articles

- [SvGrid Studio Tips and Tricks: Build Svelte Data Apps, Fast](https://svgrid.com/blog/svgrid-studio-tips-and-tricks/) - Practical SvGrid Studio tips - AI app generation, a designer that emits real SvelteKit, schema-driven edit forms, code-behind with ctx.grid, production auth/data/deploy toggles, and real data-source binding - each with a snippet.
- [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.
- [Undo/Redo for Grid Edits in SvGrid](https://svgrid.com/blog/undo-redo-grid-edits/) - Wire Ctrl+Z and Ctrl+Y into an editable SvGrid in minutes - the history stack is built in, you just need to surface it correctly.

## Source code (198-studio-form-fields.svelte)

```svelte
<script lang="ts">
  /**
   * Data-app Studio · rich form fields - image upload + cascading dropdowns.
   *
   *   Avatar  - an `upload` field renders SvFileInput (preview + file picker).
   *             With no upload handler it stores an inline data URL, so it works
   *             with no backend; pass `uploads` to push to storage instead.
   *   City    - a DEPENDENT dropdown: its options are computed from the chosen
   *             Country (`dependentOptions`), and it clears if the country changes.
   */
  import { SvGrid, createServerDataSource, renderSnippet, type ServerState } from '@svgrid/grid'
  import { SvGridEditPanel, createInMemoryDataSource, schemaToColumns, type EntitySchema } from '@svgrid/enterprise'
  import SvGridStudio from '../shared/SvGridStudio.svelte'

  type Member = { id: string; name: string; avatar: string; country: string; city: string }

  const schema: EntitySchema<Member> = {
    name: 'members', label: 'Team member', idField: 'id',
    fields: [
      { field: 'id', type: 'text', primaryKey: true, readonly: true, hidden: { form: true } },
      { field: 'avatar', type: 'text', label: 'Avatar', upload: { image: true, accept: 'image/*' } },
      { field: 'name', type: 'text', required: true, minLength: 2 },
      { field: 'country', type: 'enum', options: [
        { value: 'US', label: 'United States' }, { value: 'DE', label: 'Germany' }, { value: 'JP', label: 'Japan' },
      ] },
      { field: 'city', type: 'text' }, // form control comes from dependentOptions below
    ],
  }

  const citiesByCountry: Record<string, string[]> = {
    US: ['New York', 'San Francisco', 'Los Angeles'],
    DE: ['Berlin', 'Munich', 'Hamburg'],
    JP: ['Tokyo', 'Osaka', 'Kyoto'],
  }
  const cityOptions = (values: Partial<Member>) =>
    (citiesByCountry[values.country ?? ''] ?? []).map((c) => ({ value: c, label: c }))

  const seed: Member[] = [
    { id: 'm1', name: 'Ada Lovelace', avatar: '', country: 'US', city: 'New York' },
    { id: 'm2', name: 'Alan Turing', avatar: '', country: 'DE', city: 'Berlin' },
    { id: 'm3', name: 'Grace Hopper', avatar: '', country: 'JP', city: 'Tokyo' },
  ]

  const columns = schemaToColumns(schema)
  const source = createInMemoryDataSource(seed, schema)

  let view = $state<ServerState<Member>>({
    rows: [], total: 0, loading: false, saving: false, error: null,
    pageIndex: 0, pageSize: 10, pageCount: 1, sortModel: [], filterModel: {},
  })
  const controller = createServerDataSource(source, {
    pageSize: 10, optimistic: true, getRowId: (r) => r.id, onChange: (s) => (view = s),
  })
  controller.refresh()

  let editing = $state<Member | null | undefined>(undefined)
  let genId = 4
  async function save({ mode, id, values }: { mode: 'create' | 'edit'; id: string | null; values: Partial<Member> }) {
    if (mode === 'create') { await controller.createRow({ id: `m${genId++}`, ...values }); controller.setPage(view.pageCount - 1) }
    else if (id) { await controller.updateRow(id, values) }
    editing = undefined
  }
</script>

{#snippet AvatarCell(props: { row: Member })}
  {#if props.row.avatar}
    <img class="ff-avatar" src={props.row.avatar} alt="" />
  {:else}
    <span class="ff-avatar ff-avatar--empty">{props.row.name?.[0] ?? '?'}</span>
  {/if}
{/snippet}

<SvGridStudio
  title="Rich form fields"
  subtitle="Double-click a member: upload an <strong>avatar</strong> (image field) and pick a <strong>City</strong> that cascades from the Country."
>
  {#snippet toolbar()}
    <button class="st-btn st-btn--primary" onclick={() => (editing = null)}>+ New member</button>
  {/snippet}

  <SvGrid responsive={true}
      columnResize
    data={view.rows}
    columns={columns.map((c) => (c.field === 'avatar' ? { ...c, cell: (ctx) => renderSnippet(AvatarCell, { row: ctx.row.original as Member }) } : c))}
    loading={view.loading}
    fitColumns
    onRowDoubleClick={(e) => (editing = e.row)}
    containerHeight={280}
  />

  {#if editing !== undefined}
    <SvGridEditPanel
      {schema}
      row={editing}
      presentation="modal" persistKey="studio"
      dependentOptions={{ city: cityOptions }}
      onSubmit={save}
      onCancel={() => (editing = undefined)}
    />
  {/if}
</SvGridStudio>

<style>
  :global(.ff-avatar) {
    width: 30px; height: 30px; border-radius: 50%; object-fit: cover; display: inline-flex;
    align-items: center; justify-content: center; font-size: 12px; font-weight: 600;
    background: var(--sg-accent, #2563eb); color: var(--sg-on-accent, #fff); border: 1px solid var(--sg-border, #ddd);
  }
</style>
```

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